Skip to content

Commit

Permalink
feat: introduce Mabo project files
Browse files Browse the repository at this point in the history
Adding dedicated `Mabo.toml` files allows to define the root of projects
as well as kicking off the start of project metadata and future
packaging.

This can later extend into an ecosystem that allows to distribute schema
file collections and consume them through a dependency management
system.
  • Loading branch information
dnaka91 committed Jan 7, 2024
1 parent b18a4bd commit b5798cf
Show file tree
Hide file tree
Showing 19 changed files with 543 additions and 76 deletions.
106 changes: 96 additions & 10 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ mimalloc = "0.1.39"
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
proc-macro2 = { version = "1.0.75", default-features = false }
quote = { version = "1.0.35", default-features = false }
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.111"
syn = "2.0.47"
thiserror = "1.0.56"

Expand Down
2 changes: 1 addition & 1 deletion crates/mabo-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ repository.workspace = true
license.workspace = true

[dependencies]
glob.workspace = true
miette = { workspace = true, features = ["fancy-no-backtrace"] }
prettyplease = "0.2.16"
proc-macro2.workspace = true
quote.workspace = true
mabo-compiler = { path = "../mabo-compiler" }
mabo-parser = { path = "../mabo-parser" }
mabo-project = { path = "../mabo-project" }
syn.workspace = true
thiserror.workspace = true

Expand Down
53 changes: 22 additions & 31 deletions crates/mabo-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Code generator crate for Rust projects that can be used in `build.rs` build scripts.

use std::{convert::AsRef, env, fmt::Debug, fs, path::PathBuf};
use std::{env, fmt::Debug, fs, path::PathBuf};

use mabo_parser::Schema;
use miette::Report;
Expand All @@ -19,25 +19,12 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
/// Errors that can happen when generating Rust source code from Mabo schema files.
#[derive(Error)]
pub enum Error {
/// Failed to load the Mabo project.
#[error("failed to load the Mabo project")]
LoadProject(#[source] mabo_project::Error),
/// The required OUT_DIR env var doesn't exist.
#[error("missing OUT_DIR environment variable")]
NoOutDir,
/// One of the user-provided glob patterns is invalid.
#[error("failed to parse the glob pattern {glob:?}")]
Pattern {
/// Source error of the problem.
#[source]
source: glob::PatternError,
/// The problematic pattern.
glob: String,
},
/// Failed to iterate over the matching files for a pattern.
#[error("failed to read files of a glob pattern")]
Glob {
/// Source error of the problem.
#[source]
source: glob::GlobError,
},
/// The file name resulting from a glob pattern didn't produce a usable file path.
#[error("failed to get the file name from a found file path")]
NoFileName,
Expand Down Expand Up @@ -100,7 +87,17 @@ pub enum Error {

impl Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self}")
use std::error::Error;

write!(f, "{self}")?;

let mut source = self.source();
while let Some(inner) = source {
write!(f, "\n-> {inner}")?;
source = inner.source();
}

Ok(())
}
}

Expand Down Expand Up @@ -141,9 +138,10 @@ impl Compiler {
///
/// Will return an `Err` if any of the various cases happen, which are described in the
/// [`Error`] type.
pub fn compile(&self, schemas: &[impl AsRef<str>]) -> Result<()> {
pub fn compile(&self, manifest_dir: &str) -> Result<()> {
init_miette();

let project = mabo_project::load(manifest_dir).map_err(Error::LoadProject)?;
let out_dir = PathBuf::from(env::var_os("OUT_DIR").ok_or(Error::NoOutDir)?).join("mabo");

fs::create_dir_all(&out_dir).map_err(|source| Error::Create {
Expand All @@ -154,20 +152,13 @@ impl Compiler {
let mut inputs = Vec::new();
let mut validated = Vec::new();

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

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

inputs.push((path, input));
}
inputs.push((path, input));
}

for (path, input) in &inputs {
Expand Down
4 changes: 2 additions & 2 deletions crates/mabo-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ license.workspace = true
miette.workspace = true
owo-colors.workspace = true
schemars = { version = "0.8.16", optional = true }
serde = { version = "1.0.194", features = ["derive"], optional = true }
serde_json = { version = "1.0.110", optional = true }
serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
mabo-parser = { path = "../mabo-parser" }
thiserror.workspace = true

Expand Down
6 changes: 3 additions & 3 deletions crates/mabo-lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ license.workspace = true
anyhow.workspace = true
clap.workspace = true
directories = "5.0.1"
ignore = "0.4.21"
line-index = "0.1.1"
log = { version = "0.4.20", features = ["kv_unstable_std", "std"] }
lsp-server = "0.7.6"
lsp-types = { version = "0.95.0", features = ["proposed"] }
ouroboros = "0.18.2"
parking_lot = "0.12.1"
ropey = "1.6.1"
serde = { version = "1.0.194", features = ["derive"] }
serde_json = "1.0.110"
serde.workspace = true
serde_json.workspace = true
mabo-compiler = { path = "../mabo-compiler" }
mabo-meta = { path = "../mabo-meta" }
mabo-parser = { path = "../mabo-parser" }
mabo-project = { path = "../mabo-project" }
time = { version = "0.3.31", features = ["formatting", "local-offset", "macros"] }

[lints]
Expand Down
Loading

0 comments on commit b5798cf

Please sign in to comment.