summaryrefslogtreecommitdiff
path: root/src/func/mod.rs
blob: 9c29caf243420347588bf43169cfb55022b0ae72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//! Dynamic typesetting functions.

use std::any::Any;
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};

use self::prelude::*;

#[macro_use]
pub mod helpers;

/// Useful imports for creating your own functions.
pub mod prelude {
    pub use crate::func::{Command, CommandList, Function};
    pub use crate::layout::{layout_tree, Layout, MultiLayout, LayoutContext, LayoutSpace};
    pub use crate::layout::{LayoutAxes, AlignedAxis, Axis, Alignment};
    pub use crate::layout::{LayoutError, LayoutResult};
    pub use crate::syntax::{SyntaxTree, FuncHeader, FuncArgs, Expression, Spanned, Span};
    pub use crate::syntax::{parse, ParseContext, ParseError, ParseResult};
    pub use crate::size::{Size, Size2D, SizeBox};
    pub use crate::style::{PageStyle, TextStyle};
    pub use super::helpers::*;
    pub use Command::*;
}

/// Typesetting function types.
///
/// These types have to be able to parse themselves from a string and build
/// a list of layouting commands corresponding to the parsed source.
///
/// This trait is a supertrait of `FunctionBounds` for technical reasons.  The
/// trait `FunctionBounds` is automatically implemented for types which can
/// be used as functions, that is, all types which fulfill the bounds `Debug + PartialEq +
/// 'static`.
pub trait Function: FunctionBounds {
    /// Parse the header and body into this function given a context.
    fn parse(header: &FuncHeader, body: Option<&str>, ctx: ParseContext) -> ParseResult<Self>
    where Self: Sized;

    /// Layout this function given a context.
    ///
    /// Returns optionally the resulting layout and a new context if changes to
    /// the context should be made.
    fn layout(&self, ctx: LayoutContext) -> LayoutResult<CommandList>;
}

impl dyn Function {
    /// Downcast a dynamic function to a concrete function type.
    pub fn downcast<F>(&self) -> Option<&F> where F: Function + 'static {
        self.help_cast_as_any().downcast_ref::<F>()
    }
}

impl PartialEq for dyn Function {
    fn eq(&self, other: &dyn Function) -> bool {
        self.help_eq(other)
    }
}

/// A helper trait that describes requirements for types that can implement
/// [`Function`].
///
/// Automatically implemented for all types which fulfill to the bounds `Debug +
/// PartialEq + 'static`. There should be no need to implement this manually.
pub trait FunctionBounds: Debug {
    /// Cast self into `Any`.
    fn help_cast_as_any(&self) -> &dyn Any;

    /// Compare self with another function.
    fn help_eq(&self, other: &dyn Function) -> bool;
}

impl<T> FunctionBounds for T
where T: Debug + PartialEq + 'static
{
    fn help_cast_as_any(&self) -> &dyn Any {
        self
    }

    fn help_eq(&self, other: &dyn Function) -> bool {
        if let Some(other) = other.help_cast_as_any().downcast_ref::<Self>() {
            self == other
        } else {
            false
        }
    }
}

/// Commands requested for execution by functions.
#[derive(Debug)]
pub enum Command<'a> {
    LayoutTree(&'a SyntaxTree),

    Add(Layout),
    AddMultiple(MultiLayout),

    AddPrimarySpace(Size),
    AddSecondarySpace(Size),

    FinishLine,
    FinishRun,
    FinishSpace,

    BreakParagraph,

    SetTextStyle(TextStyle),
    SetPageStyle(PageStyle),

    SetAxes(LayoutAxes),
}

/// A sequence of commands requested for execution by a function.
#[derive(Debug)]
pub struct CommandList<'a> {
    pub commands: Vec<Command<'a>>,
}

impl<'a> CommandList<'a> {
    /// Create an empty command list.
    pub fn new() -> CommandList<'a> {
        CommandList { commands: vec![] }
    }

    /// Create a command list with commands from a vector.
    pub fn from_vec(commands: Vec<Command<'a>>) -> CommandList<'a> {
        CommandList { commands }
    }

    /// Add a command to the sequence.
    pub fn add(&mut self, command: Command<'a>) {
        self.commands.push(command);
    }

    /// Whether there are any commands in this sequence.
    pub fn is_empty(&self) -> bool {
        self.commands.is_empty()
    }
}

impl<'a> IntoIterator for CommandList<'a> {
    type Item = Command<'a>;
    type IntoIter = std::vec::IntoIter<Command<'a>>;

    fn into_iter(self) -> Self::IntoIter {
        self.commands.into_iter()
    }
}

impl<'a> IntoIterator for &'a CommandList<'a> {
    type Item = &'a Command<'a>;
    type IntoIter = std::slice::Iter<'a, Command<'a>>;

    fn into_iter(self) -> Self::IntoIter {
        self.commands.iter()
    }
}

/// Create a list of commands.
#[macro_export]
macro_rules! commands {
    ($($x:expr),*$(,)*) => (
        $crate::func::CommandList::from_vec(vec![$($x,)*])
    );
}

/// A map from identifiers to functions.
pub struct Scope {
    parsers: HashMap<String, Box<ParseFunc>>,
}

/// A function which parses a function invocation into a function type.
type ParseFunc = dyn Fn(&FuncHeader, Option<&str>, ParseContext) -> ParseResult<Box<dyn Function>>;

impl Scope {
    /// Create a new empty scope.
    pub fn new() -> Scope {
        Scope {
            parsers: HashMap::new(),
        }
    }

    /// Create a new scope with the standard functions contained.
    pub fn with_std() -> Scope {
        crate::library::std()
    }

    /// Add a function type to the scope giving it a name.
    pub fn add<F: Function + 'static>(&mut self, name: &str) {
        self.parsers.insert(
            name.to_owned(),
            Box::new(|h, b, c| F::parse(h, b, c).map(|func| Box::new(func) as Box<dyn Function>)),
        );
    }

    /// Return the parser with the given name if there is one.
    pub(crate) fn get_parser(&self, name: &str) -> Option<&ParseFunc> {
        self.parsers.get(name).map(|x| &**x)
    }
}

impl Debug for Scope {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "Scope ")?;
        write!(f, "{:?}", self.parsers.keys())
    }
}