| 1 | //! This is a simple CLI utility to take in an FTL file and output the AST. |
| 2 | //! |
| 3 | //! ## View the `Debug` representation: |
| 4 | //! |
| 5 | //! From the root directory of the `fluent-rs` repo: |
| 6 | //! |
| 7 | //! ```sh |
| 8 | //! cargo run --bin parser -- ./fluent-syntax/tests/fixtures/literal_expressions.ftl |
| 9 | //! ``` |
| 10 | //! |
| 11 | //! ## View the `json` representation: |
| 12 | //! |
| 13 | //! ```sh |
| 14 | //! cargo run --bin parser --features json -- ./fluent-syntax/tests/fixtures/literal_expressions.ftl |
| 15 | //! ``` |
| 16 | |
| 17 | use fluent_syntaxrust-analyzer cargo fluent-syntax 0.12.0 crate/::parserrust-analyzer cargo fluent-syntax 0.12.0 parser/::parserust-analyzer cargo fluent-syntax 0.12.0 parser/parse().; |
| 18 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/; |
| 19 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#; |
| 20 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 21 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#; |
| 22 | |
| 23 | fn read_filerust-analyzer cargo fluent-syntax 0.12.0 read_file().(path: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#> { |
| 24 | let mut f = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 25 | let mut s = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().(); |
| 26 | f.read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(&mut s)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 27 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(s) |
| 28 | } |
| 29 | |
| 30 | fn mainrust-analyzer cargo fluent-syntax 0.12.0 main().() { |
| 31 | let args: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/args().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 32 | let source = read_filerust-analyzer cargo fluent-syntax 0.12.0 read_file().(args.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(1).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Pass a file path as the first argument")) |
| 33 | .expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("Failed to fetch file"); |
| 34 | |
| 35 | let (ast, errors) = match parserust-analyzer cargo fluent-syntax 0.12.0 parser/parse().(source.as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().()) { |
| 36 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ast) => (ast, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#), |
| 37 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#((ast, err)) => (ast, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(err)), |
| 38 | }; |
| 39 | |
| 40 | #[cfg(feature = "json")] |
| 41 | { |
| 42 | let target_json = serde_jsonrust-analyzer cargo serde_json 1.0.140 crate/::to_string_prettyrust-analyzer cargo serde_json 1.0.140 ser/to_string_pretty().(&ast).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 43 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", target_json); |
| 44 | } |
| 45 | #[cfg(not(feature = "json"))] |
| 46 | { |
| 47 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#; |
| 48 | let mut result = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::new(); |
| 49 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(result, "{:#?}", ast).unwrap(); |
| 50 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{}", result); |
| 51 | } |
| 52 | |
| 53 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(errors) = errors { |
| 54 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("\n======== Errors ========== \n"); |
| 55 | for err in errors { |
| 56 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("Err: {:#?}", err); |
| 57 | } |
| 58 | } |
| 59 | } |