diff options
| author | Laurenz <laurmaedje@gmail.com> | 2019-12-05 19:48:37 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2019-12-05 19:48:37 +0100 |
| commit | 72a9631b038d1a60e4e4a78e92cd69e6f8ce4316 (patch) | |
| tree | 17614efc2e21dd0b8caa24beaaaee7c40c150281 /src/func/mod.rs | |
| parent | f72b1505bebf8d2fe1a60d386a3a3c3b67d4f903 (diff) | |
Move arg parser into `FuncArgs` and create (incomplete) consistent map ðŸ§
Diffstat (limited to 'src/func/mod.rs')
| -rw-r--r-- | src/func/mod.rs | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/src/func/mod.rs b/src/func/mod.rs index 31e31592..7ee7d779 100644 --- a/src/func/mod.rs +++ b/src/func/mod.rs @@ -8,17 +8,12 @@ use self::prelude::*; #[macro_use] pub mod macros; -pub mod args; +pub mod map; /// Useful imports for creating your own functions. pub mod prelude { - pub use Command::*; - pub use super::args::*; - pub use super::{Scope, ParseFunc, LayoutFunc, Command, Commands}; - pub use crate::syntax::{SyntaxTree, FuncHeader, FuncArgs, Expression, Spanned, Span}; - pub use crate::syntax::{parse, ParseContext, ParseResult}; - pub use crate::size::{Size, Size2D, SizeBox}; - pub use crate::style::{PageStyle, TextStyle}; + pub use super::map::ConsistentMap; + pub use crate::func::{Scope, ParseFunc, LayoutFunc, Command, Commands}; pub use crate::layout::{ layout_tree, Layout, MultiLayout, LayoutContext, LayoutSpace, LayoutSpaces, @@ -26,16 +21,25 @@ pub mod prelude { LayoutAlignment, Alignment, SpacingKind, LayoutResult, }; + pub use crate::syntax::{ + parse, ParseContext, ParseResult, + SyntaxTree, FuncCall, FuncArgs, PosArg, KeyArg, + Expression, Ident, ExpressionKind, + Spanned, Span + }; + pub use crate::size::{Size, Size2D, SizeBox, ScaleSize, FSize, PSize}; + pub use crate::style::{LayoutStyle, PageStyle, TextStyle}; + pub use Command::*; } /// Types representing functions that are parsed from source code. pub trait ParseFunc { - type Meta; + type Meta: Clone; /// Parse the header and body into this function given a context. fn parse( - header: &FuncHeader, - body: Option<&str>, + args: FuncArgs, + body: Option<Spanned<&str>>, ctx: ParseContext, metadata: Self::Meta, ) -> ParseResult<Self> where Self: Sized; @@ -126,8 +130,8 @@ pub struct Scope { /// A function which parses the source of a function into a function type which /// implements [`LayoutFunc`]. type Parser = dyn Fn( - &FuncHeader, - Option<&str>, + FuncArgs, + Option<Spanned<&str>>, ParseContext ) -> ParseResult<Box<dyn LayoutFunc>>; @@ -153,11 +157,11 @@ impl Scope { /// Add a parseable type with additional metadata that is given to the /// parser (other than the default of `()`). pub fn add_with_metadata<F, T>(&mut self, name: &str, metadata: T) - where F: ParseFunc<Meta=T> + LayoutFunc + 'static, T: 'static { + where F: ParseFunc<Meta=T> + LayoutFunc + 'static, T: 'static + Clone { self.parsers.insert( name.to_owned(), - Box::new(|h, b, c| { - F::parse(h, b, c, metadata) + Box::new(move |a, b, c| { + F::parse(a, b, c, metadata.clone()) .map(|f| Box::new(f) as Box<dyn LayoutFunc>) }) ); |
