Skip to content

Commit

Permalink
feat: primitive type docs
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Nov 12, 2024
1 parent 51fe3e1 commit 6b88d44
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 112 deletions.
101 changes: 91 additions & 10 deletions core/rust.templating.docgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,106 @@
//
use std::sync::Arc;

/// A primitive type in the Anti-Raid Luau API.
/// E.g. u64, string, etc.
///
/// These are typedefs to primitive Luau types
#[derive(Default, Debug, serde::Serialize, Clone)]
/// The root of the documentation.
pub struct Docs {
pub plugins: Vec<Plugin>,
pub struct Primitive {
pub name: String,
pub lua_type: String,
pub description: String,
pub constraints: Vec<PrimitiveConstraint>,
}

// Docs builder code
impl Docs {
pub fn add_plugin(self, plugin: Plugin) -> Self {
let mut d = self;
d.plugins.push(plugin);
d
// Primitive builder code
impl Primitive {
pub fn name(self, name: &str) -> Self {
let mut p = self;
p.name = name.to_string();
p
}

pub fn lua_type(self, lua_type: &str) -> Self {
let mut p = self;
p.lua_type = lua_type.to_string();
p
}

pub fn build(self) -> Docs {
pub fn description(self, description: &str) -> Self {
let mut p = self;
p.description = description.to_string();
p
}

pub fn add_constraint(self, name: &str, description: &str, accepted_values: &str) -> Self {
let mut p = self;
p.constraints.push(PrimitiveConstraint {
name: name.to_string(),
description: description.to_string(),
accepted_values: accepted_values.to_string(),
});

p
}

pub fn build(self) -> Primitive {
self
}
}

impl Primitive {
pub fn type_definition(&self) -> String {
format!("type {} = {}", self.name, self.lua_type)
}
}

/// A primitive constraint
#[derive(Default, Debug, serde::Serialize, Clone)]
pub struct PrimitiveConstraint {
pub name: String,
pub description: String,
pub accepted_values: String,
}

/// A special helper types for building a list of primitives.
pub struct PrimitiveListBuilder {
primitives: Vec<Primitive>,
}

impl Default for PrimitiveListBuilder {
fn default() -> Self {
PrimitiveListBuilder {
primitives: Vec::new(),
}
}
}

impl PrimitiveListBuilder {
pub fn add(
self,
name: &str,
lua_type: &str,
description: &str,
p_fn: impl Fn(Primitive) -> Primitive,
) -> Self {
let mut p = self;

let new_primitive = Primitive::default()
.name(name)
.lua_type(lua_type)
.description(description);

p.primitives.push(p_fn(new_primitive));

p
}

pub fn build(self) -> Vec<Primitive> {
self.primitives
}
}

/// A plugin in the Anti-Raid Luau API.
#[derive(Default, Debug, serde::Serialize, Clone)]
pub struct Plugin {
Expand Down
1 change: 1 addition & 0 deletions core/rust.templating/src/lang_lua/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Work in progress
pub(crate) mod multioption;
mod perthreadpanichook;
pub mod primitives_docs;
pub mod samples;
pub(crate) mod state;

Expand Down
89 changes: 89 additions & 0 deletions core/rust.templating/src/lang_lua/primitives_docs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
pub fn document_primitives() -> Vec<templating_docgen::Primitive> {
templating_docgen::PrimitiveListBuilder::default()
.add("u8", "number", "An unsigned 8-bit integer. **Note: u8 arrays (`{u8}`) are often used to represent an array of bytes in AntiRaid**", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("0-{}", u8::MAX),
)
})
.add("u16", "number", "An unsigned 16-bit integer.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("0-{}", u16::MAX),
)
})
.add("u32", "number", "An unsigned 32-bit integer.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("0-{}", u32::MAX),
)
})
.add("u64", "number", "An unsigned 64-bit integer. **Note that most, if not all, cases of `i64` in the actual API are either `string` or the `I64` custom type from typesext**", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("0-{}", u64::MAX),
)
})
.add("i8", "number", "A signed 8-bit integer.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("{}-{}", i8::MIN, i8::MAX),
)
})
.add("i16", "number", "A signed 16-bit integer.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("{}-{}", i16::MIN, i16::MAX),
)
})
.add("i32", "number", "A signed 32-bit integer.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("{}-{}", i32::MIN, i32::MAX),
)
})
.add("i64", "number", "A signed 64-bit integer. **Note that most, if not all, cases of `i64` in the actual API are either `string` or the `I64` custom type from typesext**", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
&format!("{}-{}", i64::MIN, i64::MAX),
)
})
.add("f32", "number", "A 32-bit floating point number.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
"IEEE 754 single-precision floating point",
)
})
.add("f64", "number", "A 64-bit floating point number.", |p| {
p.add_constraint(
"range",
"The range of values this number can take on",
"IEEE 754 double-precision floating point",
)
})
.add("bool", "boolean", "A boolean value.", |p| p)
.add("char", "string", "A single Unicode character.", |p| {
p.add_constraint(
"length",
"The length of the string",
"1",
)
})
.add("string", "string", "A UTF-8 encoded string.", |p| {
p.add_constraint(
"encoding",
"Accepted character encoding",
"UTF-8 *only*",
)
})
.build()
}
1 change: 1 addition & 0 deletions core/rust.templating/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod atomicinstant;
pub mod core;

mod lang_lua;
pub use lang_lua::primitives_docs;
pub use lang_lua::samples;
pub use lang_lua::state::LuaKVConstraints;
pub use lang_lua::PLUGINS;
Expand Down
Loading

0 comments on commit 6b88d44

Please sign in to comment.