Merge pull request #1860 from hannobraun/fj

Add standardized CLI for models
This commit is contained in:
Hanno Braun 2023-06-06 11:33:36 +02:00 committed by GitHub
commit d50d3da43d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 87 additions and 46 deletions

5
Cargo.lock generated
View File

@ -629,10 +629,7 @@ dependencies = [
name = "cuboid" name = "cuboid"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"clap",
"fj", "fj",
"itertools",
] ]
[[package]] [[package]]
@ -781,12 +778,14 @@ dependencies = [
name = "fj" name = "fj"
version = "0.46.0" version = "0.46.0"
dependencies = [ dependencies = [
"clap",
"fj-core", "fj-core",
"fj-export", "fj-export",
"fj-interop", "fj-interop",
"fj-math", "fj-math",
"fj-viewer", "fj-viewer",
"fj-window", "fj-window",
"thiserror",
] ]
[[package]] [[package]]

View File

@ -13,4 +13,4 @@
mod run; mod run;
mod window; mod window;
pub use self::run::run; pub use self::run::{run, Error};

View File

@ -10,6 +10,7 @@ license.workspace = true
keywords.workspace = true keywords.workspace = true
categories.workspace = true categories.workspace = true
[dependencies] [dependencies]
fj-core.workspace = true fj-core.workspace = true
fj-export.workspace = true fj-export.workspace = true
@ -17,3 +18,8 @@ fj-interop.workspace = true
fj-math.workspace = true fj-math.workspace = true
fj-viewer.workspace = true fj-viewer.workspace = true
fj-window.workspace = true fj-window.workspace = true
thiserror = "1.0.40"
[dependencies.clap]
version = "4.3.1"
features = ["derive"]

19
crates/fj/src/args.rs Normal file
View File

@ -0,0 +1,19 @@
use std::path::PathBuf;
/// Standardized CLI for Fornjot models
#[derive(clap::Parser)]
pub struct Args {
/// Export model to this path
#[arg(short, long, value_name = "PATH")]
pub export: Option<PathBuf>,
}
impl Args {
/// Parse the command-line arguments
///
/// Convenience method that saves the caller from having to import the
/// `clap::Parser` trait.
pub fn parse() -> Self {
<Self as clap::Parser>::parse()
}
}

View File

@ -0,0 +1,47 @@
use std::ops::Deref;
use fj_core::algorithms::{approx::Tolerance, triangulate::Triangulate};
use crate::Args;
/// Export or display a model, according to CLI arguments
///
/// This function is intended to be called by applications that define a model
/// and want to provide a standardized CLI interface for dealing with that
/// model.
///
/// This function is used by Fornjot's own testing infrastructure, but is useful
/// beyond that, when using Fornjot directly to define a model.
pub fn handle_model<Model>(
model: impl Deref<Target = Model>,
tolerance: impl Into<Tolerance>,
) -> Result
where
for<'r> (&'r Model, Tolerance): Triangulate,
{
let mesh = (model.deref(), tolerance.into()).triangulate();
let args = Args::parse();
if let Some(path) = args.export {
crate::export::export(&mesh, &path)?;
} else {
crate::window::run(mesh, false)?;
}
Ok(())
}
/// Return value of [`handle_model`]
pub type Result = std::result::Result<(), Error>;
/// Error returned by [`handle_model`]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Error displaying model
#[error("Error displaying model")]
Display(#[from] crate::window::Error),
/// Error exporting model
#[error("Error exporting model")]
Export(#[from] crate::export::Error),
}

View File

@ -11,6 +11,14 @@
#![warn(missing_docs)] #![warn(missing_docs)]
mod args;
mod handle_model;
pub use self::{
args::Args,
handle_model::{handle_model, Error, Result},
};
pub use fj_core as core; pub use fj_core as core;
pub use fj_export as export; pub use fj_export as export;
pub use fj_interop as interop; pub use fj_interop as interop;

View File

@ -3,14 +3,5 @@ name = "cuboid"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies]
anyhow = "1.0.71"
itertools = "0.10.5"
[dependencies.clap]
version = "4.3.1"
features = ["derive"]
[dependencies.fj] [dependencies.fj]
path = "../../crates/fj" path = "../../crates/fj"

View File

@ -1,41 +1,12 @@
use std::{ops::Deref, path::PathBuf}; use fj::handle_model;
use fj::core::algorithms::{approx::Tolerance, triangulate::Triangulate};
fn main() -> anyhow::Result<()> {
let args = Args::parse();
fn main() -> fj::Result {
let cuboid = cuboid::cuboid(3., 2., 1.); let cuboid = cuboid::cuboid(3., 2., 1.);
// The tolerance makes no difference for this model, as there aren't any // The tolerance makes no difference for this model, as there aren't any
// curves. // curves.
let tolerance = Tolerance::from_scalar(1.)?; let tolerance = 1.;
handle_model(cuboid, tolerance)?;
let mesh = (cuboid.deref(), tolerance).triangulate();
if let Some(path) = args.export {
fj::export::export(&mesh, &path)?;
} else {
fj::window::run(mesh, false)?;
}
Ok(()) Ok(())
} }
/// Standardized CLI for Fornjot models
#[derive(clap::Parser)]
pub struct Args {
/// Export model to this path
#[arg(short, long, value_name = "PATH")]
pub export: Option<PathBuf>,
}
impl Args {
/// Parse the command-line arguments
///
/// Convenience method that saves the caller from having to import the
/// `clap::Parser` trait.
pub fn parse() -> Self {
<Self as clap::Parser>::parse()
}
}