Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow export-validator to recieve model name #1698

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions tools/export-validator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
mod ffi;

use std::{ffi::CString, fs, process::Command, ptr};
use std::{env::args, ffi::CString, fs, process::Command, ptr};

use anyhow::{anyhow, bail, Context as _};
use tempfile::tempdir;

fn main() -> anyhow::Result<()> {
for model in fs::read_dir("models")? {
let model = model?;
let model = model.file_name().into_string().map_err(|err| {
anyhow!("Failed to convert directory name to `String`: {:?}", err)
})?;

let dir = tempdir()?;
let file_name = format!("{model}.3mf");
let export_file_path = dir.path().join(file_name);
let export_file_path_str = export_file_path.to_str().unwrap();

let exit_status = Command::new("cargo")
.arg("run")
.arg("--")
.arg(&model)
.args(["--export", export_file_path_str])
.status()?;

if !exit_status.success() {
bail!(
"Exporting model `{model}` failed with error code:\
{exit_status}"
);
let model = args().nth(1);
if let Some(model) = model {
// If user passed model name, validate that model
handle_model(model)?;
} else {
// Otherwise validate all models in `models` directory
for model in fs::read_dir("models")? {
let model = model?;
let model = model.file_name().into_string().map_err(|err| {
anyhow!(
"Failed to convert directory name to `String`: {:?}",
err
)
})?;

handle_model(model)?;
}

// Presumably we're using the library in the way it's intended, so this
// might be sound?
unsafe { validate_model(export_file_path_str) }
.with_context(|| format!("Could not validate model `{model}`"))?;
}

Ok(())
}

fn handle_model(model: String) -> Result<(), anyhow::Error> {
let dir = tempdir()?;
let file_name = format!("{model}.3mf");
let export_file_path = dir.path().join(file_name);
let export_file_path_str = export_file_path.to_str().unwrap();
let exit_status = Command::new("cargo")
.arg("run")
.arg("--")
.arg(&model)
.args(["--export", export_file_path_str])
.status()?;
if !exit_status.success() {
bail!(
"Exporting model `{model}` failed with error code:\
{exit_status}"
);
}
unsafe { validate_model(export_file_path_str) }
.with_context(|| format!("Could not validate model `{model}`"))?;
Ok(())
}

unsafe fn validate_model(file: &str) -> anyhow::Result<()> {
let mut model = ptr::null();

Expand Down