Skip to content

Commit

Permalink
fix: prepend CARGO_MANIFEST_DIR to include path
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Jan 12, 2024
1 parent 6701fdb commit 5e5649e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/attrs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use include_dir::{include_dir, Dir};
use itertools::Itertools;
use proc_macro::Span;
use proc_macro2::TokenStream;
use proc_macro_error::{abort, emit_call_site_warning};
use proc_macro_error::{abort, emit_call_site_warning, emit_error};
use quote::quote;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -104,7 +105,25 @@ impl quote::ToTokens for Attrs {
}
Attr::DiagramEnd(_) => (),
Attr::DiagramIncludeAnchor(_, path) => {
let data = std::fs::read_to_string(path).expect("Unable to read mermaid file");
// get cargo manifest dir
let manifest_dir =
std::env::var("CARGO_MANIFEST_DIR").unwrap_or(".".to_string());

// append path to cargo manifest dir using PathBuf
let path = &PathBuf::new().join(manifest_dir).join(path);

let data = match std::fs::read_to_string(path) {
Ok(data) => data,
Err(e) => {
emit_error!(
Span::call_site(),
"failed to read mermaid file from path {:?}: {}",
path,
e,
);
continue;
}
};
tokens.extend(generate_diagram_rustdoc(Some(data.as_str()).into_iter()))
}
}
Expand Down

0 comments on commit 5e5649e

Please sign in to comment.