Debian Codegraph @

src/bin/update_fixtures.rs

raw symbols SCIP
1 //! Run the `update_fixtures` binary after updating any of the `benches` `.ftl` fixtures.
2 //! This will update the `.json` files used in reference tests.
3 //!
4 //! This file must be run from `{PROJECT_ROOT}/fluent-syntax`
5 //!
6 //! ```sh
7 //! cargo run --bin update_fixtures --features="json"
8 //! ```
9 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/;
10 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/;
11
12 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().;
13
14 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#> {
15 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(path)
16 }
17
18 fn write_filerust-analyzer cargo fluent-syntax 0.12.0 write_file().(path: &str, source: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), 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#> {
19 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/write().(path, source)
20 }
21
22 fn mainrust-analyzer cargo fluent-syntax 0.12.0 main().() {
23 let samples = &["menubar", "preferences", "simple"];
24 let contexts = &["browser", "preferences"];
25
26 for sample in samples {
27 let path = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("./benches/{}.ftl", sample);
28 let source = read_filerust-analyzer cargo fluent-syntax 0.12.0 read_file().(&path).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().(
29 "Could not read the benches file. Are you running this from the correct directory? It must be run from `{PROJECT_ROOT}/fluent-syntax`",
30 );
31 let ast = parserust-analyzer cargo fluent-syntax 0.12.0 parser/parse().(source).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
32 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().();
33 let new_path = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("./tests/fixtures/benches/{}.json", sample);
34 write_filerust-analyzer cargo fluent-syntax 0.12.0 write_file().(&new_path, &target_json).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
35 }
36
37 for test in contexts {
38 let paths = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_dirrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_dir().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("./benches/contexts/{}", test)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
39 for path in paths.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/impl#[I][IntoIterator]into_iter().() {
40 let p = path.unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().().pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[DirEntry]path().();
41 let file_name = p.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
42 let path = p.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_str().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
43 let source = read_filerust-analyzer cargo fluent-syntax 0.12.0 read_file().(path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
44 let ast = parserust-analyzer cargo fluent-syntax 0.12.0 parser/parse().(source).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
45 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().();
46 let new_path = formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
47 "./tests/fixtures/benches/contexts/{}/{}",
48 test,
49 file_name.replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(".ftl", ".json")
50 );
51 write_filerust-analyzer cargo fluent-syntax 0.12.0 write_file().(&new_path, &target_json).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
52 }
53 }
54 }