Skip to content

Commit

Permalink
Merge pull request #1860 from hannobraun/fj
Browse files Browse the repository at this point in the history
Add standardized CLI for models
  • Loading branch information
hannobraun authored Jun 6, 2023
2 parents c39c522 + 9da5b5e commit d50d3da
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 46 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/fj-window/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
mod run;
mod window;

pub use self::run::run;
pub use self::run::{run, Error};
6 changes: 6 additions & 0 deletions crates/fj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ license.workspace = true
keywords.workspace = true
categories.workspace = true


[dependencies]
fj-core.workspace = true
fj-export.workspace = true
fj-interop.workspace = true
fj-math.workspace = true
fj-viewer.workspace = true
fj-window.workspace = true
thiserror = "1.0.40"

[dependencies.clap]
version = "4.3.1"
features = ["derive"]
19 changes: 19 additions & 0 deletions crates/fj/src/args.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}
47 changes: 47 additions & 0 deletions crates/fj/src/handle_model.rs
Original file line number Diff line number Diff line change
@@ -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),
}
8 changes: 8 additions & 0 deletions crates/fj/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
#![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_export as export;
pub use fj_interop as interop;
Expand Down
9 changes: 0 additions & 9 deletions models/cuboid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,5 @@ name = "cuboid"
version = "0.1.0"
edition = "2021"


[dependencies]
anyhow = "1.0.71"
itertools = "0.10.5"

[dependencies.clap]
version = "4.3.1"
features = ["derive"]

[dependencies.fj]
path = "../../crates/fj"
37 changes: 4 additions & 33 deletions models/cuboid/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
use std::{ops::Deref, path::PathBuf};

use fj::core::algorithms::{approx::Tolerance, triangulate::Triangulate};

fn main() -> anyhow::Result<()> {
let args = Args::parse();
use fj::handle_model;

fn main() -> fj::Result {
let cuboid = cuboid::cuboid(3., 2., 1.);

// The tolerance makes no difference for this model, as there aren't any
// curves.
let tolerance = Tolerance::from_scalar(1.)?;

let mesh = (cuboid.deref(), tolerance).triangulate();

if let Some(path) = args.export {
fj::export::export(&mesh, &path)?;
} else {
fj::window::run(mesh, false)?;
}
let tolerance = 1.;
handle_model(cuboid, tolerance)?;

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()
}
}

0 comments on commit d50d3da

Please sign in to comment.