Skip to content

Commit ad6c902

Browse files
mx00sJosé Duarte
authored and
José Duarte
committed
fix(diagrams): make EXPORT_FOLDER logic more robust
Before these changes when I tried to export diagram artifacts to `./gen` nothing was written to the expected directory. $ export EXPORT_FOLDER="./gen" $ cargo run --example traffic_light --all-features I discovered the files were actually written, but to unexpected locations. These changes eliminate assumptions about how the path is specified and also attempt to create the `EXPORT_FOLDER` if it doesn't already exist.
1 parent 9fc035e commit ad6c902

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

typestate-proc-macro/src/igraph/export.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub mod mermaid {
4848
/// Blanket implementation for the [`Mermaid`] format.
4949
impl super::Format for Mermaid {
5050
fn file_extension<'a>() -> &'a str {
51-
".mermaid"
51+
"mermaid"
5252
}
5353
}
5454

@@ -170,7 +170,7 @@ pub mod plantuml {
170170

171171
impl super::Format for PlantUml {
172172
fn file_extension<'a>() -> &'a str {
173-
".uml"
173+
"uml"
174174
}
175175
}
176176

@@ -317,7 +317,7 @@ pub mod dot {
317317

318318
impl super::Format for Dot {
319319
fn file_extension<'a>() -> &'a str {
320-
".dot"
320+
"dot"
321321
}
322322
}
323323

@@ -425,7 +425,15 @@ pub mod dot {
425425
feature = "export-plantuml",
426426
feature = "export-mermaid"
427427
))]
428-
use {super::IntermediateGraph, std::fs::File, syn::Ident};
428+
use {
429+
super::IntermediateGraph,
430+
std::{
431+
env,
432+
fs::{create_dir_all, File},
433+
path::Path,
434+
},
435+
syn::Ident,
436+
};
429437

430438
#[cfg(any(
431439
feature = "export-dot",
@@ -440,16 +448,15 @@ pub(crate) fn export<F: Format>(
440448
where
441449
IntermediateGraph<Ident, Ident>: Export<F>,
442450
{
443-
let folder_path = ::std::env::var_os("EXPORT_FOLDER")
444-
.and_then(|s| s.into_string().ok())
445-
.unwrap_or_else(|| "./".to_string());
446-
let mut f = File::create(format!(
447-
"{}{}{}",
448-
folder_path,
449-
file_name,
450-
F::file_extension(),
451-
))?;
451+
let folder = {
452+
let cwd = env::current_dir()?;
453+
env::var_os("EXPORT_FOLDER").map_or_else(|| cwd, |dir| Path::new(&dir).to_path_buf())
454+
};
455+
if !folder.exists() {
456+
create_dir_all(&folder)?;
457+
}
452458

459+
let mut f = File::create(folder.join(file_name).with_extension(F::file_extension()))?;
453460
igraph.export(&mut f, format)?;
454461
Ok(())
455462
}

0 commit comments

Comments
 (0)