Skip to content

Commit

Permalink
feat: accept glob patterns for schema input files
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 14, 2023
1 parent 8f7e85e commit d8f5e43
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
39 changes: 23 additions & 16 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 crates/stef-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository.workspace = true
license.workspace = true

[dependencies]
glob = "0.3.1"
miette.workspace = true
prettyplease = "0.2.15"
proc-macro2.workspace = true
Expand Down
56 changes: 36 additions & 20 deletions crates/stef-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![deny(rust_2018_idioms, clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]

use std::path::{Path, PathBuf};
use std::{path::{Path, PathBuf}, convert::AsRef};

use stef_parser::Schema;
use thiserror::Error;
Expand All @@ -16,6 +16,17 @@ type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, Error)]
pub enum Error {
#[error("failed to parse the glob pattern {glob:?}")]
Pattern {
#[source]
source: glob::PatternError,
glob: String,
},
#[error("failed to read files of a glob pattern")]
Glob {
#[source]
source: glob::GlobError,
},
#[error("failed reading schema file at {file:?}")]
Read {
#[source]
Expand All @@ -26,32 +37,37 @@ pub enum Error {
Parse { message: String, file: PathBuf },
}

pub fn compile(schemas: &[impl AsRef<Path>], _includes: &[impl AsRef<Path>]) -> Result<()> {
pub fn compile(schemas: &[impl AsRef<str>], _includes: &[impl AsRef<Path>]) -> Result<()> {
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());

for schema in schemas {
let path = schema.as_ref();

let input = std::fs::read_to_string(path).map_err(|e| Error::Read {
for schema in schemas.iter().map(AsRef::as_ref) {
for schema in glob::glob(schema).map_err(|e| Error::Pattern {
source: e,
file: path.to_owned(),
})?;
glob: schema.to_owned(),
})? {
let path = schema.map_err(|e| Error::Glob { source: e })?;

let input = std::fs::read_to_string(&path).map_err(|e| Error::Read {
source: e,
file: path.clone(),
})?;

let schema = Schema::parse(&input).map_err(|e| Error::Parse {
message: e.to_string(),
file: path.to_owned(),
})?;
let code = definition::compile_schema(&schema);
let code = prettyplease::unparse(&syn::parse2(code).unwrap());
let schema = Schema::parse(&input).map_err(|e| Error::Parse {
message: e.to_string(),
file: path.clone(),
})?;
let code = definition::compile_schema(&schema);
let code = prettyplease::unparse(&syn::parse2(code).unwrap());

println!("{code}");
println!("{code}");

let out_file = out_dir.join(format!(
"{}.rs",
path.file_stem().unwrap().to_str().unwrap()
));
let out_file = out_dir.join(format!(
"{}.rs",
path.file_stem().unwrap().to_str().unwrap()
));

std::fs::write(out_file, code).unwrap();
std::fs::write(out_file, code).unwrap();
}
}

Ok(())
Expand Down

0 comments on commit d8f5e43

Please sign in to comment.