diff --git a/Cargo.lock b/Cargo.lock index 9dac4a7..10d8c2d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -711,6 +711,7 @@ version = "0.1.0" dependencies = [ "clap", "color-eyre", + "glob", "miette", "mimalloc", "stef-parser", diff --git a/Cargo.toml b/Cargo.toml index 97d87f0..a9edf24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ license = "MIT" [workspace.dependencies] color-eyre = { version = "0.6.2", default-features = false } +glob = "0.3.1" indoc = "2.0.4" insta = { version = "1.34.0", features = ["glob"] } miette = "5.10.0" diff --git a/book/src/guide/creating/basic.stef b/book/src/guide/creating/basic.stef index ecd55ac..7d6a9c4 100644 --- a/book/src/guide/creating/basic.stef +++ b/book/src/guide/creating/basic.stef @@ -5,3 +5,4 @@ struct User { /// Current age in years. age: u16 @2, } + diff --git a/book/src/guide/examples/01.stef b/book/src/guide/examples/01.stef index c72a71f..0bae770 100644 --- a/book/src/guide/examples/01.stef +++ b/book/src/guide/examples/01.stef @@ -1,5 +1,4 @@ use other::schema::DateTime; - struct User { first_name: string @1, last_name: string @2, @@ -16,3 +15,4 @@ mod user { NonBinary(string @1) @3, } } + diff --git a/book/src/schema/arrays/basic.stef b/book/src/schema/arrays/basic.stef index 7c3b7a0..ea98119 100644 --- a/book/src/schema/arrays/basic.stef +++ b/book/src/schema/arrays/basic.stef @@ -1,3 +1,4 @@ struct Sample { options: [bool; 5] @1, } + diff --git a/book/src/schema/constants/basic.stef b/book/src/schema/constants/basic.stef index b099acd..0b15b36 100644 --- a/book/src/schema/constants/basic.stef +++ b/book/src/schema/constants/basic.stef @@ -1,19 +1,15 @@ const VALUE_BOOL: bool = true; - const VALUE_U8: u8 = 1; const VALUE_U16: u16 = 2; const VALUE_U32: u32 = 3; const VALUE_U64: u64 = 4; const VALUE_U128: u128 = 5; - const VALUE_I8: i8 = -1; const VALUE_I16: i16 = -2; const VALUE_I32: i32 = -3; const VALUE_I64: i64 = -4; const VALUE_I128: i128 = -5; - -const VALUE_F32: f32 = 1.0; -const VALUE_F64: f64 = 2.0; - +const VALUE_F32: f32 = 1; +const VALUE_F64: f64 = 2; const VALUE_STR: &string = "abc"; const VALUE_BYTES: &bytes = [1, 2, 3]; diff --git a/book/src/schema/enums/advanced.stef b/book/src/schema/enums/advanced.stef index e772389..8ac6ec4 100644 --- a/book/src/schema/enums/advanced.stef +++ b/book/src/schema/enums/advanced.stef @@ -5,3 +5,4 @@ enum Sample { field2: bool @2, } @2, } + diff --git a/book/src/schema/enums/basic.stef b/book/src/schema/enums/basic.stef index 84f6831..9566112 100644 --- a/book/src/schema/enums/basic.stef +++ b/book/src/schema/enums/basic.stef @@ -2,3 +2,4 @@ enum Sample { Variant1 @1, Variant2 @2, } + diff --git a/book/src/schema/modules/basic.stef b/book/src/schema/modules/basic.stef index 9a8bb6d..8cdc584 100644 --- a/book/src/schema/modules/basic.stef +++ b/book/src/schema/modules/basic.stef @@ -1,3 +1,4 @@ mod sample { struct Sample } + diff --git a/book/src/schema/modules/nesting.stef b/book/src/schema/modules/nesting.stef index e55e1be..d4ddd02 100644 --- a/book/src/schema/modules/nesting.stef +++ b/book/src/schema/modules/nesting.stef @@ -5,3 +5,4 @@ mod a { } } } + diff --git a/book/src/schema/structs/generics.stef b/book/src/schema/structs/generics.stef index 6ff6d68..a692a0e 100644 --- a/book/src/schema/structs/generics.stef +++ b/book/src/schema/structs/generics.stef @@ -2,3 +2,4 @@ struct Pair { key: K @1, value: V @2, } + diff --git a/book/src/schema/structs/named.stef b/book/src/schema/structs/named.stef index 50e1e04..0ea0ec6 100644 --- a/book/src/schema/structs/named.stef +++ b/book/src/schema/structs/named.stef @@ -2,3 +2,4 @@ struct Sample { field1: u32 @1, field2: u16 @2, } + diff --git a/book/src/schema/structs/unit.stef b/book/src/schema/structs/unit.stef index 6a88dfb..f325755 100644 --- a/book/src/schema/structs/unit.stef +++ b/book/src/schema/structs/unit.stef @@ -1 +1,2 @@ struct Sample + diff --git a/book/src/schema/structs/unnamed.stef b/book/src/schema/structs/unnamed.stef index 033a29c..91645db 100644 --- a/book/src/schema/structs/unnamed.stef +++ b/book/src/schema/structs/unnamed.stef @@ -1 +1,2 @@ struct Sample(u32 @1, u16 @2) + diff --git a/book/src/schema/tuples/basic.stef b/book/src/schema/tuples/basic.stef index ba0ce28..a6a2e11 100644 --- a/book/src/schema/tuples/basic.stef +++ b/book/src/schema/tuples/basic.stef @@ -1,3 +1,4 @@ struct Sample { size: (u32, u32) @1, } + diff --git a/crates/stef-build/Cargo.toml b/crates/stef-build/Cargo.toml index 3ad32f1..9bdab45 100644 --- a/crates/stef-build/Cargo.toml +++ b/crates/stef-build/Cargo.toml @@ -10,7 +10,7 @@ repository.workspace = true license.workspace = true [dependencies] -glob = "0.3.1" +glob.workspace = true miette = { workspace = true, features = ["fancy-no-backtrace"] } prettyplease = "0.2.15" proc-macro2.workspace = true diff --git a/crates/stef-cli/Cargo.toml b/crates/stef-cli/Cargo.toml index 1712c74..b418934 100644 --- a/crates/stef-cli/Cargo.toml +++ b/crates/stef-cli/Cargo.toml @@ -12,6 +12,7 @@ license.workspace = true [dependencies] clap = { version = "4.4.6", features = ["derive", "wrap_help"] } color-eyre.workspace = true +glob.workspace = true miette = { workspace = true, features = ["fancy-no-backtrace"] } mimalloc.workspace = true stef-parser = { path = "../stef-parser" } diff --git a/crates/stef-cli/src/cli.rs b/crates/stef-cli/src/cli.rs index cbb51ae..e34a597 100644 --- a/crates/stef-cli/src/cli.rs +++ b/crates/stef-cli/src/cli.rs @@ -16,11 +16,11 @@ pub enum Command { }, Check { #[arg(num_args(1..))] - files: Vec, + files: Vec, }, Format { #[arg(num_args(1..))] - files: Vec, + files: Vec, }, } diff --git a/crates/stef-cli/src/main.rs b/crates/stef-cli/src/main.rs index 33c188e..0fe3f87 100644 --- a/crates/stef-cli/src/main.rs +++ b/crates/stef-cli/src/main.rs @@ -3,7 +3,7 @@ #![warn(clippy::pedantic)] #![allow(clippy::missing_errors_doc)] -use std::{fs, path::PathBuf, process::ExitCode}; +use std::{fs, process::ExitCode}; use miette::{Context, IntoDiagnostic, Result}; use stef_parser::Schema; @@ -40,35 +40,47 @@ fn main() -> ExitCode { ExitCode::SUCCESS } -fn check(files: Vec) -> Result<()> { - for file in files { - let buf = fs::read_to_string(&file) +fn check(patterns: Vec) -> Result<()> { + for pattern in patterns { + for entry in glob::glob(&pattern) .into_diagnostic() - .wrap_err_with(|| format!("Failed reading {file:?}"))?; - - if let Err(e) = Schema::parse(&buf).wrap_err("Failed parsing schema file") { - eprintln!("{e:?}"); + .wrap_err("Failed parsing glob pattern")? + { + let entry = entry.into_diagnostic().wrap_err("Failed reading entry")?; + let buf = fs::read_to_string(&entry) + .into_diagnostic() + .wrap_err_with(|| format!("Failed reading {entry:?}"))?; + + if let Err(e) = Schema::parse(&buf).wrap_err("Failed parsing schema file") { + eprintln!("{e:?}"); + } } } Ok(()) } -fn format(files: Vec) -> Result<()> { - for file in files { - let buf = fs::read_to_string(&file).into_diagnostic()?; - let schema = match Schema::parse(&buf).wrap_err("Failed parsing schema file") { - Ok(schema) => schema, - Err(e) => { - eprintln!("{e:?}"); - continue; +fn format(patterns: Vec) -> Result<()> { + for pattern in patterns { + for entry in glob::glob(&pattern) + .into_diagnostic() + .wrap_err("Failed parsing glob pattern")? + { + let entry = entry.into_diagnostic().wrap_err("Failed reading entry")?; + let buf = fs::read_to_string(&entry).into_diagnostic()?; + let schema = match Schema::parse(&buf).wrap_err("Failed parsing schema file") { + Ok(schema) => schema, + Err(e) => { + eprintln!("{e:?}"); + continue; + } + }; + + let formatted = schema.to_string(); + + if buf != formatted { + fs::write(entry, &formatted).into_diagnostic()?; } - }; - - let formatted = schema.to_string(); - - if buf != formatted { - fs::write(file, &buf).into_diagnostic()?; } }