Skip to content

Commit

Permalink
feat: integrate syn::Error
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
drahnr committed Feb 24, 2022
1 parent 084cb55 commit e90965d
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 52 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ fs-err = "2"
proc-macro2 = "1"
quote = "1"
blake2 = "0.10"
syn = { version = "1", optional = true, default-features = false }

[dev-dependencies]
baz = { path = "./tests/baz" }
syn = { version = "1", features = ["extra-traits", "full"] }

[features]
default = ["syndicate"]
syndicate = ["syn"]
78 changes: 27 additions & 51 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,34 @@ impl Expander {
self
}

#[cfg(feature = "syndicate")]
/// Create a file with `filename` under `env!("OUT_DIR")` if it's not an `Err(_)`.
pub fn maybe_write_to_out_dir(
self,
tokens: impl Into<Result<TokenStream, syn::Error>>,
) -> Result<TokenStream, std::io::Error> {
self.maybe_write_to(
tokens.into(),
std::path::PathBuf::from(env!("OUT_DIR")).as_path(),
)
}

/// Create a file with `filename` under `env!("OUT_DIR")`.
pub fn write_to_out_dir(self, tokens: TokenStream) -> Result<TokenStream, std::io::Error> {
if self.dry {
Ok(tokens)
} else {
let out = env!("OUT_DIR");
let out = std::path::PathBuf::from(out);
let path = out.join(self.filename);
expand_to_file(
tokens,
path.as_path(),
out.as_path(),
self.rustfmt,
self.comment,
self.verbose,
)
let out = std::path::PathBuf::from(env!("OUT_DIR"));
self.write_to(tokens, out.as_path())
}

#[cfg(feature = "syndicate")]
/// Create a file with `filename` at `dest` if it's not an `Err(_)`.
pub fn maybe_write_to(
self,
maybe_tokens: Result<TokenStream, syn::Error>,
dest_dir: &Path,
) -> Result<TokenStream, std::io::Error> {
match maybe_tokens {
Ok(tokens) => self.write_to(tokens, dest_dir),
Err(err) => Ok(err.to_compile_error()),
}
}

Expand Down Expand Up @@ -197,40 +209,4 @@ fn expand_to_file(
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn dry() -> Result<(), std::io::Error> {
let ts = quote! {
pub struct X {
x: [u8;32],
}
};
let modified = Expander::new("foo")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
.dry(true)
.write_to_out_dir(ts.clone())?;

assert_eq!(ts.to_string(), modified.to_string());
Ok(())
}

#[test]
fn basic() -> Result<(), std::io::Error> {
let ts = quote! {
pub struct X {
x: [u8;32],
}
};
let modified = Expander::new("bar")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.write_to_out_dir(ts.clone())?;

assert_ne!(ts.to_string(), modified.to_string());
Ok(())
}
}
mod tests;
82 changes: 82 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use super::*;

#[test]
fn dry() -> Result<(), std::io::Error> {
let ts = quote! {
pub struct X {
x: [u8;32],
}
};
let modified = Expander::new("foo")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
.dry(true)
.write_to_out_dir(ts.clone())?;

assert_eq!(
ts.to_string(),
modified.to_string(),
"Dry does not alter the provided `TokenStream`. qed"
);
Ok(())
}

#[test]
fn basic() -> Result<(), std::io::Error> {
let ts = quote! {
pub struct X {
x: [u8;32],
}
};
let modified = Expander::new("bar")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.write_to_out_dir(ts.clone())?;

let s = modified.to_string();
assert_ne!(s, ts.to_string());
assert!(s.contains("include ! ("));
Ok(())
}

#[cfg(feature = "syndicate")]
mod syndicate {
use super::*;
use proc_macro2::Span;

#[test]
fn ok_is_written_to_external_file() -> Result<(), std::io::Error> {
let ts = Ok(quote! {
pub struct X {
x: [u8;32],
}
});
let modified = Expander::new("bar")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.maybe_write_to_out_dir(ts.clone())?;

let s = modified.to_string();
assert_ne!(s, ts.unwrap().to_string());
assert!(s.contains("include ! ("));
Ok(())
}

#[test]
fn errors_are_not_written_to_external_file() -> Result<(), std::io::Error> {
let ts = Err(syn::Error::new(Span::call_site(), "Hajajajaiii!"));
let modified = Expander::new("")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
// .dry(false)
.maybe_write_to_out_dir(ts.clone())?;

assert_eq!(
ts.unwrap_err().to_compile_error().to_string(),
modified.to_string()
);
Ok(())
}
}
2 changes: 1 addition & 1 deletion tests/baz/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ fn baz2(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let expanded = Expander::new("baz")
.add_comment("This is generated code!".to_owned())
.fmt(Edition::_2021)
.write_to_out_dir(modified).expect("IO error");
.write_to_out_dir(modified).expect("No IO error");
expanded
}

0 comments on commit e90965d

Please sign in to comment.