Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions bin-github/src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl GitHub {
println!("Local, Skipping commit for {command}");
return Ok(None);
}
command!(["checkout", "dist"]);
command!(["checkout", "dist-f"]);
command!([
"add",
format!("commands/{}.yml", urlencoding::encode(command)).as_str()
Expand All @@ -76,14 +76,14 @@ impl GitHub {
println!("Local, Skipping commit for {ns}::{handler}");
return Ok(None);
}
command!(["checkout", "dist"]);
command!(["checkout", "dist-f"]);
command!(["add", format!("events/{ns}/{handler}.yml").as_str()]);
command!([
"commit",
"-m",
format!("Update Event `{ns}::{handler}`").as_str()
]);
command!(["push", "origin", "dist"]);
command!(["push", "origin", "dist-f"]);
Ok(None)
}

Expand All @@ -92,10 +92,10 @@ impl GitHub {
println!("Local, Skipping commit for version");
return;
}
command!(["checkout", "dist"]);
command!(["checkout", "dist-f"]);
command!(["add", "version.txt"]);
command!(["commit", "-m", "Update version"]);
command!(["push", "origin", "dist"]);
command!(["push", "origin", "dist-f"]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions clients/rust/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ pub fn main() {
}
let repo = Repository::open(&tmp).unwrap_or_else(|_| {
git2::build::RepoBuilder::new()
.branch("dist")
.branch("dist-f")
.clone("https://github.com/acemod/arma3-wiki", &tmp)
.map_err(|e| format!("Failed to clone repository: {e}"))
.unwrap()
});
repo.find_remote("origin")
.and_then(|mut r| r.fetch(&["dist"], None, None))
.and_then(|mut r| r.fetch(&["dist-f"], None, None))
.map_err(|e| format!("Failed to fetch remote: {e}"))
.unwrap();
let fetch_head = repo
Expand Down
42 changes: 42 additions & 0 deletions clients/rust/src/functions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::{collections::HashMap, fs::File};

use crate::model::Function;

/// Packages of functions (e.g. from different mods)
pub struct Functions {
functions: HashMap<String, Vec<Function>>,
}

impl Functions {
#[must_use]
pub const fn new(functions: HashMap<String, Vec<Function>>) -> Self {
Self { functions }
}
#[must_use]
pub fn get(&self, name: &str) -> Option<&Vec<Function>> {
self.functions.get(&name.to_lowercase())
}
pub fn iter(&self) -> impl Iterator<Item = (&String, &Vec<Function>)> {
self.functions.iter()
}
/// Reads functions from a YAML file.
/// # Errors
/// Returns an error string if the file cannot be read or parsed as YAML.
pub fn from_file(file: File) -> Result<Vec<Function>, String> {
let functions: Vec<Function> = serde_yaml::from_reader(file).map_err(|e| format!("{e}"))?;
Ok(functions)
}
/// Reads functions from a YAML string.
/// # Errors
/// Returns an error string if the string cannot be read or parsed as YAML.
pub fn from_string(data: &str) -> Result<Vec<Function>, String> {
let functions: Vec<Function> = serde_yaml::from_str(data).map_err(|e| format!("{e}"))?;
Ok(functions)
}
/// Converts a vector of functions to a YAML string.
/// # Errors
/// Returns an error string if serialization to YAML fails.
pub fn to_string(functions: &Vec<Function>) -> Result<String, String> {
serde_yaml::to_string(functions).map_err(|e| format!("{e}"))
}
}
54 changes: 51 additions & 3 deletions clients/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ use std::{
};

use commands::Commands;
use functions::Functions;
use git2::Repository;
use model::{Command, EventHandlerNamespace, ParsedEventHandler, Version};
use model::{Command, EventHandlerNamespace, Function, ParsedEventHandler, Version};
use rust_embed::RustEmbed;

pub mod commands;
pub mod functions;
pub mod model;

#[derive(RustEmbed)]
Expand All @@ -22,6 +24,8 @@ pub struct Wiki {
custom: Vec<String>,
/// Whether the wiki was just updated.
updated: bool,
/// Function packages
functions: Functions,
}

impl Wiki {
Expand All @@ -45,6 +49,11 @@ impl Wiki {
self.updated
}

#[must_use]
pub const fn functions(&self) -> &Functions {
&self.functions
}

#[must_use]
pub fn load(force_pull: bool) -> Self {
#[cfg(feature = "remote")]
Expand Down Expand Up @@ -130,7 +139,7 @@ impl Wiki {
repo
} else {
git2::build::RepoBuilder::new()
.branch("dist")
.branch("dist-f")
.clone("https://github.com/acemod/arma3-wiki", &appdata)
.map_err(|e| format!("Failed to clone repository: {e}"))?
};
Expand Down Expand Up @@ -166,6 +175,25 @@ impl Wiki {
}
event_handlers.insert(*ns, handlers);
}
let mut functions = HashMap::new();
if let Ok(func_dir) = std::fs::read_dir(appdata.join("functions")) {
for entry in func_dir {
let Ok(entry) = entry else {
continue;
};
let path = entry.path();
let file_stem = path.file_stem().unwrap_or_default().to_string_lossy();
if !path.is_file() || file_stem.is_empty() {
continue;
}
let file = std::fs::File::open(&path)
.map_err(|e| format!("Failed to open function file: {e}"))?;
let file_funcs: Vec<Function> = Functions::from_file(file).map_err(|e| {
format!("Failed to parse function file {}: {e}", path.display())
})?;
functions.insert(file_stem.to_lowercase(), file_funcs);
}
}
Ok(Self {
version: Version::from_wiki(
std::fs::read_to_string(appdata.join("version.txt"))
Expand All @@ -177,6 +205,7 @@ impl Wiki {
event_handlers,
updated,
custom: Vec::new(),
functions: Functions::new(functions),
})
}

Expand All @@ -188,6 +217,7 @@ impl Wiki {
pub fn load_dist() -> Self {
let mut commands = HashMap::new();
let mut event_handlers = HashMap::new();
let mut functions = HashMap::new();
for entry in Asset::iter() {
let path = entry.as_ref();
if path.starts_with("commands/")
Expand All @@ -213,6 +243,23 @@ impl Wiki {
.or_insert_with(Vec::new)
.push(handler);
}
} else if path.starts_with("functions/") {
let v_path = std::path::Path::new(path);
let file_stem = v_path.file_stem().unwrap_or_default().to_string_lossy();
if file_stem.is_empty()
|| !v_path
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case("yml"))
{
continue;
}
let data = Asset::get(path).unwrap().data;
let file_funcs: Vec<Function> = Functions::from_string(
std::str::from_utf8(&data).unwrap()
).unwrap_or_else(|e| {
panic!("Failed to parse function file {path}: {e}")
});
functions.insert(file_stem.to_lowercase(), file_funcs);
}
}
Self {
Expand All @@ -226,12 +273,13 @@ impl Wiki {
event_handlers,
updated: false,
custom: Vec::new(),
functions: Functions::new(functions),
}
}

fn update_git(repo: &Repository) -> Result<(), String> {
repo.find_remote("origin")
.and_then(|mut r| r.fetch(&["dist"], None, None))
.and_then(|mut r| r.fetch(&["dist-f"], None, None))
.map_err(|e| format!("Failed to fetch remote: {e}"))?;
let fetch_head = repo
.find_reference("FETCH_HEAD")
Expand Down
2 changes: 2 additions & 0 deletions clients/rust/src/model/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};

#[allow(unused_imports, reason = "only used in wiki feature")]
use super::{Locality, ParseError, Since, Syntax};

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -328,6 +329,7 @@ impl Command {
Ok((command, errors))
}

#[allow(dead_code, reason = "only used in wiki feature")]
fn get_cmd_name(name: &str) -> &str {
match name {
"!_a" => "!",
Expand Down
54 changes: 54 additions & 0 deletions clients/rust/src/model/function.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use super::{Param, Value};

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct Function {
/// Function name, if known
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
/// Return type, if known
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
ret: Option<Value>,
#[serde(default)]
params: Vec<Param>,
#[serde(default)]
example: String,
}
impl Function {
#[must_use]
pub const fn new(
name: Option<String>,
ret: Option<Value>,
params: Vec<Param>,
example: String,
) -> Self {
Self {
name,
ret,
params,
example,
}
}
#[must_use]
pub const fn name(&self) -> Option<&String> {
self.name.as_ref()
}
#[must_use]
pub const fn ret(&self) -> Option<&Value> {
self.ret.as_ref()
}
#[must_use]
pub fn params(&self) -> &[Param] {
&self.params
}
#[must_use]
pub fn param_get(&self, index: usize) -> Option<&Param> {
self.params.get(index)
}
#[must_use]
pub fn example(&self) -> &str {
&self.example
}
}
2 changes: 2 additions & 0 deletions clients/rust/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod call;
mod command;
mod event_handler;
mod function;
mod locality;
mod param;
mod since;
Expand All @@ -11,6 +12,7 @@ mod version;
pub use call::{Arg, Call};
pub use command::Command;
pub use event_handler::{EventHandler, EventHandlerNamespace, ParsedEventHandler};
pub use function::Function;
pub use locality::Locality;
pub use param::Param;
pub use since::Since;
Expand Down
2 changes: 2 additions & 0 deletions clients/rust/src/model/param.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use serde::{Deserialize, Serialize};

#[allow(unused_imports, reason = "only used in wiki feature")]
use crate::model::Version;

#[allow(unused_imports, reason = "only used in wiki feature")]
use super::{ParseError, Since, Value};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
Expand Down
1 change: 1 addition & 0 deletions clients/rust/src/model/syntax.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize};

#[allow(unused_imports, reason = "only used in wiki feature")]
use super::{Call, Locality, Param, ParseError, Since, Value};

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions clients/rust/src/model/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub enum Value {
}

// regex once cell
#[allow(dead_code, reason = "only used in wiki feature")]
static REGEX_TYPE: OnceLock<Regex> = OnceLock::new();
// static REGEX_ARRAY_IN_FORMAT: OnceLock<Regex> = OnceLock::new();

Expand Down