diff options
Diffstat (limited to 'crates/tools/src/lib.rs')
-rw-r--r-- | crates/tools/src/lib.rs | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/crates/tools/src/lib.rs b/crates/tools/src/lib.rs index 63ede5315..444745be5 100644 --- a/crates/tools/src/lib.rs +++ b/crates/tools/src/lib.rs | |||
@@ -1,13 +1,15 @@ | |||
1 | extern crate itertools; | 1 | extern crate itertools; |
2 | #[macro_use] | ||
3 | extern crate failure; | 2 | extern crate failure; |
3 | extern crate teraron; | ||
4 | 4 | ||
5 | use itertools::Itertools; | ||
6 | use std::{ | 5 | use std::{ |
7 | fs, | ||
8 | path::{Path, PathBuf}, | 6 | path::{Path, PathBuf}, |
9 | }; | 7 | }; |
10 | 8 | ||
9 | use itertools::Itertools; | ||
10 | |||
11 | pub use teraron::{Mode, Verify, Overwrite}; | ||
12 | |||
11 | pub type Result<T> = ::std::result::Result<T, failure::Error>; | 13 | pub type Result<T> = ::std::result::Result<T, failure::Error>; |
12 | 14 | ||
13 | pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; | 15 | pub const GRAMMAR: &str = "ra_syntax/src/grammar.ron"; |
@@ -54,22 +56,23 @@ pub fn collect_tests(s: &str) -> Vec<(usize, Test)> { | |||
54 | res | 56 | res |
55 | } | 57 | } |
56 | 58 | ||
57 | pub fn update(path: &Path, contents: &str, verify: bool) -> Result<()> { | 59 | pub fn generate(mode: Mode) -> Result<()> { |
58 | match fs::read_to_string(path) { | 60 | let grammar = project_root().join(GRAMMAR); |
59 | Ok(ref old_contents) if old_contents == contents => { | 61 | let syntax_kinds = project_root().join(SYNTAX_KINDS); |
60 | return Ok(()); | 62 | let ast = project_root().join(AST); |
61 | } | 63 | teraron::generate( |
62 | _ => (), | 64 | &syntax_kinds, |
63 | } | 65 | &grammar, |
64 | if verify { | 66 | mode, |
65 | bail!("`{}` is not up-to-date", path.display()); | 67 | )?; |
66 | } | 68 | teraron::generate( |
67 | eprintln!("updating {}", path.display()); | 69 | &ast, |
68 | fs::write(path, contents)?; | 70 | &grammar, |
71 | mode, | ||
72 | )?; | ||
69 | Ok(()) | 73 | Ok(()) |
70 | } | 74 | } |
71 | 75 | ||
72 | |||
73 | pub fn project_root() -> PathBuf { | 76 | pub fn project_root() -> PathBuf { |
74 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) | 77 | Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) |
75 | .parent() | 78 | .parent() |