diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-12-14 10:09:44 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-12-14 10:09:44 +0100 |
| commit | 9ba4d2c134479aad876a0e2ac4cd1622a353109e (patch) | |
| tree | a94e0e6ae53a1ba440e869fca26cc2ea0b179057 /src/model/func.rs | |
| parent | 4c73456fc1f5df8ebb3a89d9db657c3c54624d66 (diff) | |
New macro setup
Diffstat (limited to 'src/model/func.rs')
| -rw-r--r-- | src/model/func.rs | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/model/func.rs b/src/model/func.rs index 60f36bd4..0261b5e2 100644 --- a/src/model/func.rs +++ b/src/model/func.rs @@ -30,16 +30,22 @@ enum Repr { } impl Func { + /// Create a new function from a type that can be turned into a function. + pub fn from_type<T: FuncType>(name: &'static str) -> Self { + T::create_func(name) + } + /// Create a new function from a native rust function. pub fn from_fn( name: &'static str, func: fn(&Vm, &mut Args) -> SourceResult<Value>, + doc: &'static str, ) -> Self { - Self(Arc::new(Repr::Native(Native { name, func, set: None, node: None }))) + Self(Arc::new(Repr::Native(Native { name, func, set: None, node: None, doc }))) } /// Create a new function from a native rust node. - pub fn from_node<T: Node>(name: &'static str) -> Self { + pub fn from_node<T: Node>(name: &'static str, doc: &'static str) -> Self { Self(Arc::new(Repr::Native(Native { name, func: |ctx, args| { @@ -49,6 +55,7 @@ impl Func { }, set: Some(|args| T::set(args, false)), node: Some(NodeId::of::<T>()), + doc, }))) } @@ -66,6 +73,15 @@ impl Func { } } + /// Documentation for the function. + pub fn doc(&self) -> Option<&str> { + match self.0.as_ref() { + Repr::Native(native) => Some(native.doc), + Repr::With(func, _) => func.doc(), + _ => None, + } + } + /// The number of positional arguments this function takes, if known. pub fn argc(&self) -> Option<usize> { match self.0.as_ref() { @@ -159,16 +175,24 @@ impl PartialEq for Func { } } +/// Types that can be turned into functions. +pub trait FuncType { + /// Create a function with the given name from this type. + fn create_func(name: &'static str) -> Func; +} + /// A function defined by a native rust function or node. struct Native { /// The name of the function. - pub name: &'static str, + name: &'static str, /// The function pointer. - pub func: fn(&Vm, &mut Args) -> SourceResult<Value>, + func: fn(&Vm, &mut Args) -> SourceResult<Value>, /// The set rule. - pub set: Option<fn(&mut Args) -> SourceResult<StyleMap>>, + set: Option<fn(&mut Args) -> SourceResult<StyleMap>>, /// The id of the node to customize with this function's show rule. - pub node: Option<NodeId>, + node: Option<NodeId>, + /// Documentation of the function. + doc: &'static str, } impl Hash for Native { @@ -177,6 +201,7 @@ impl Hash for Native { (self.func as usize).hash(state); self.set.map(|set| set as usize).hash(state); self.node.hash(state); + self.doc.hash(state); } } |
