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
use fluent_syntax::ast;
use fluent_syntax::parser::Parser;
use fluent_syntax::parser::ParserError;
use ouroboros::self_referencing;

#[self_referencing]
#[derive(Debug)]
pub struct InnerFluentResource {
    string: String,
    #[borrows(string)]
    ast: ast::Resource<&'this str>,
}

/// A resource containing a list of localization messages.
#[derive(Debug)]
pub struct FluentResource(InnerFluentResource);

impl FluentResource {
    pub fn try_new(source: String) -> Result<Self, (Self, Vec<ParserError>)> {
        let mut errors = None;

        let res = InnerFluentResourceBuilder {
            string: source,
            ast_builder: |string: &str| match Parser::new(string).parse_runtime() {
                Ok(ast) => ast,
                Err((ast, err)) => {
                    errors = Some(err);
                    ast
                }
            },
        }
        .build();

        if let Some(errors) = errors {
            Err((Self(res), errors))
        } else {
            Ok(Self(res))
        }
    }

    pub fn ast(&self) -> &ast::Resource<&str> {
        self.0.borrow_ast()
    }
}