From d8f5e439acc25afb096820b66fcf97bae34d3e91 Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Sat, 14 Oct 2023 18:29:02 +0900 Subject: [PATCH] feat: accept glob patterns for schema input files --- Cargo.lock | 39 ++++++++++++++----------- crates/stef-build/Cargo.toml | 1 + crates/stef-build/src/lib.rs | 56 +++++++++++++++++++++++------------- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13cb41d..ddd2d6b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -295,6 +295,12 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" +[[package]] +name = "glob" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" + [[package]] name = "globset" version = "0.4.13" @@ -403,18 +409,18 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" [[package]] name = "linkme" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ad5707f9d2423042c4321155b22d5257c2f140e3f30d0a42dce882a6010010d" +checksum = "91ed2ee9464ff9707af8e9ad834cffa4802f072caad90639c583dd3c62e6e608" dependencies = [ "linkme-impl", ] [[package]] name = "linkme-impl" -version = "0.3.16" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b43a5344be08996a47cb02b1ddc737119578a1cd01ae60d91541864df5926db9" +checksum = "ba125974b109d512fccbc6c0244e7580143e460895dfd6ea7f8bbb692fd94396" dependencies = [ "proc-macro2", "quote", @@ -590,15 +596,15 @@ dependencies = [ [[package]] name = "regex-lite" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83aa2b788f84cf0e43e6ae57bb0fcdbbe7604414a7d2bf997a311b062a6f4291" +checksum = "9a6ebcd15653947e6140f59a9811a06ed061d18a5c35dfca2e2e4c5525696878" [[package]] name = "regex-syntax" -version = "0.8.0" +version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3cbb081b9784b07cceb8824c8583f86db4814d172ab043f3c23f7dc600bf83d" +checksum = "56d84fdd47036b038fc80dd333d10b6aab10d5d31f4a366e20014def75328d33" [[package]] name = "rustc-demangle" @@ -608,9 +614,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.18" +version = "0.38.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a74ee2d7c2581cd139b42447d7d9389b889bdaad3a73f1ebb16f2a3237bb19c" +checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" dependencies = [ "bitflags", "errno", @@ -630,18 +636,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.188" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +checksum = "8e422a44e74ad4001bdc8eede9a4570ab52f71190e9c076d14369f38b9200537" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.188" +version = "1.0.189" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +checksum = "1e48d1f918009ce3145511378cf68d613e3b3d9137d67272562080d68a2b32d5" dependencies = [ "proc-macro2", "quote", @@ -683,6 +689,7 @@ dependencies = [ name = "stef-build" version = "0.1.0" dependencies = [ + "glob", "indoc", "insta", "miette", @@ -1048,9 +1055,9 @@ checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" [[package]] name = "winnow" -version = "0.5.16" +version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "037711d82167854aff2018dfd193aa0fef5370f456732f0d5a0c59b0f1b4b907" +checksum = "a3b801d0e0a6726477cc207f60162da452f3a95adb368399bef20a946e06f65c" dependencies = [ "memchr", ] diff --git a/crates/stef-build/Cargo.toml b/crates/stef-build/Cargo.toml index 59eb8ce..d804589 100644 --- a/crates/stef-build/Cargo.toml +++ b/crates/stef-build/Cargo.toml @@ -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 diff --git a/crates/stef-build/src/lib.rs b/crates/stef-build/src/lib.rs index 22b4328..329a820 100644 --- a/crates/stef-build/src/lib.rs +++ b/crates/stef-build/src/lib.rs @@ -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; @@ -16,6 +16,17 @@ type Result = std::result::Result; #[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] @@ -26,32 +37,37 @@ pub enum Error { Parse { message: String, file: PathBuf }, } -pub fn compile(schemas: &[impl AsRef], _includes: &[impl AsRef]) -> Result<()> { +pub fn compile(schemas: &[impl AsRef], _includes: &[impl AsRef]) -> 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(())