Skip to content

Commit

Permalink
include_str to watch TOML file
Browse files Browse the repository at this point in the history
  • Loading branch information
bnaecker committed Jun 10, 2024
1 parent 6ec3fe5 commit 3358724
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 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 oximeter/timeseries-macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ proc-macro = true
[dependencies]
oximeter-impl.workspace = true
proc-macro2.workspace = true
quote.workspace = true
syn.workspace = true

[lints]
Expand Down
25 changes: 23 additions & 2 deletions oximeter/timeseries-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@ pub fn use_timeseries(
) -> proc_macro::TokenStream {
match syn::parse::<syn::LitStr>(tokens) {
Ok(path) => {
let filename = path.value();
let filename =
match std::path::Path::new(&path.value()).canonicalize() {
Ok(f) => f.display().to_string(),
Err(e) => {
let msg = format!(
"Failed to canonicalize file from path '{}': {:?}",
path.value(),
e,
);
return syn::Error::new(path.span(), msg)
.into_compile_error()
.into();
}
};
let contents = match std::fs::read_to_string(&filename) {
Ok(c) => c,
Err(e) => {
Expand All @@ -21,7 +34,15 @@ pub fn use_timeseries(
}
};
match oximeter_impl::schema::codegen::use_timeseries(&contents) {
Ok(toks) => return toks.into(),
Ok(toks) => {
return quote::quote! {
/// Include the schema file itself to ensure we recompile
/// when that changes.
const _: &str = include_str!(#filename);
#toks
}
.into()
}
Err(e) => {
let msg = format!(
"Failed to generate timeseries types \
Expand Down

0 comments on commit 3358724

Please sign in to comment.