Skip to content

Commit

Permalink
feat: enable glob parsing for CLI path arguments
Browse files Browse the repository at this point in the history
In addition to only accepting a list of files for the `check` and
`format` command, the input is now treated as glob pattern.
  • Loading branch information
dnaka91 committed Oct 24, 2023
1 parent 4d8a01a commit b44bc1b
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions book/src/guide/creating/basic.stef
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ struct User {
/// Current age in years.
age: u16 @2,
}

2 changes: 1 addition & 1 deletion book/src/guide/examples/01.stef
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use other::schema::DateTime;

struct User {
first_name: string @1,
last_name: string @2,
Expand All @@ -16,3 +15,4 @@ mod user {
NonBinary(string @1) @3,
}
}

1 change: 1 addition & 0 deletions book/src/schema/arrays/basic.stef
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
struct Sample {
options: [bool; 5] @1,
}

8 changes: 2 additions & 6 deletions book/src/schema/constants/basic.stef
Original file line number Diff line number Diff line change
@@ -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];
1 change: 1 addition & 0 deletions book/src/schema/enums/advanced.stef
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ enum Sample {
field2: bool @2,
} @2,
}

1 change: 1 addition & 0 deletions book/src/schema/enums/basic.stef
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ enum Sample {
Variant1 @1,
Variant2 @2,
}

1 change: 1 addition & 0 deletions book/src/schema/modules/basic.stef
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod sample {
struct Sample
}

1 change: 1 addition & 0 deletions book/src/schema/modules/nesting.stef
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ mod a {
}
}
}

1 change: 1 addition & 0 deletions book/src/schema/structs/generics.stef
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ struct Pair<K, V> {
key: K @1,
value: V @2,
}

1 change: 1 addition & 0 deletions book/src/schema/structs/named.stef
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ struct Sample {
field1: u32 @1,
field2: u16 @2,
}

1 change: 1 addition & 0 deletions book/src/schema/structs/unit.stef
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
struct Sample

1 change: 1 addition & 0 deletions book/src/schema/structs/unnamed.stef
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
struct Sample(u32 @1, u16 @2)

1 change: 1 addition & 0 deletions book/src/schema/tuples/basic.stef
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
struct Sample {
size: (u32, u32) @1,
}

2 changes: 1 addition & 1 deletion crates/stef-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/stef-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
4 changes: 2 additions & 2 deletions crates/stef-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub enum Command {
},
Check {
#[arg(num_args(1..))]
files: Vec<PathBuf>,
files: Vec<String>,
},
Format {
#[arg(num_args(1..))]
files: Vec<PathBuf>,
files: Vec<String>,
},
}

Expand Down
56 changes: 34 additions & 22 deletions crates/stef-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,35 +40,47 @@ fn main() -> ExitCode {
ExitCode::SUCCESS
}

fn check(files: Vec<PathBuf>) -> Result<()> {
for file in files {
let buf = fs::read_to_string(&file)
fn check(patterns: Vec<String>) -> 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<PathBuf>) -> 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<String>) -> 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()?;
}
}

Expand Down

0 comments on commit b44bc1b

Please sign in to comment.