1 use fluent_syntax::ast; 2 use fluent_syntax::parser::Parser; 3 use fluent_syntax::parser::ParserError; 4 use ouroboros::self_referencing; 5 6 #[self_referencing] 7 #[derive(Debug)] 8 pub struct InnerFluentResource { 9 string: String, 10 #[borrows(string)] 11 ast: ast::Resource<&'this str>, 12 } 13 14 /// A resource containing a list of localization messages. 15 #[derive(Debug)] 16 pub struct FluentResource(InnerFluentResource); 17 18 impl FluentResource { try_new(source: String) -> Result<Self, (Self, Vec<ParserError>)>19 pub fn try_new(source: String) -> Result<Self, (Self, Vec<ParserError>)> { 20 let mut errors = None; 21 22 let res = InnerFluentResourceBuilder { 23 string: source, 24 ast_builder: |string: &str| match Parser::new(string).parse() { 25 Ok(ast) => ast, 26 Err((ast, err)) => { 27 errors = Some(err); 28 ast 29 } 30 }, 31 } 32 .build(); 33 34 if let Some(errors) = errors { 35 Err((Self(res), errors)) 36 } else { 37 Ok(Self(res)) 38 } 39 } 40 ast(&self) -> &ast::Resource<&str>41 pub fn ast(&self) -> &ast::Resource<&str> { 42 self.0.borrow_ast() 43 } 44 } 45