rustemo/
parser.rs

1use std::path::Path;
2
3use crate::{context::Context, error::Result, input::Input};
4
5/// The trait implemented by all Rustemo parsers.
6pub trait Parser<'i, I, C, S, TK>
7where
8    I: Input + ?Sized,
9    C: Context<'i, I, S, TK>,
10    S: State,
11{
12    type Output;
13
14    /// Parse the given input and produce the result. The output type is set by
15    /// the parser implementers and it is usually defined by the builder if the
16    /// building is done during the parse process.
17    fn parse(&self, input: &'i I) -> Result<Self::Output>;
18
19    /// Parse with the given context which has information about the current
20    /// parsing state (e.g. position, location). Used in situation when we need
21    /// to continue parsing from a specific state, like in parsing the layout
22    /// from the current location.
23    fn parse_with_context(&self, context: &mut C, input: &'i I) -> Result<Self::Output>;
24
25    /// A convenience method for loading the content from the given file and
26    /// calling `parse`. The parser will own the content being parsed and thus
27    /// has to outlive `Self::Output` if it borrows from the content loaded from
28    /// the file.
29    fn parse_file<'a, F: AsRef<Path>>(&'a mut self, file: F) -> Result<Self::Output>
30    where
31        'a: 'i;
32}
33
34/// This trait must be implemented by the parser state type.
35pub trait State: Default + Copy {
36    /// Returns the default layout state.
37    fn default_layout() -> Option<Self>;
38}