From 4882a5d23d68b421dc68a065cf3f46b33367dccf Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:11:42 +0100 Subject: [PATCH 1/7] Refactor --- src/model.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model.rs b/src/model.rs index 92630d02e..0a9f8ce65 100644 --- a/src/model.rs +++ b/src/model.rs @@ -20,7 +20,9 @@ impl Model { } pub fn src_path(&self) -> PathBuf { - format!("{}/src", self.path()).into() + let mut src_path = PathBuf::from(self.path()); + src_path.push("src"); + src_path } pub fn lib_path(&self) -> String { From f2e82905c9f2544b2337f6f8ee8ab944a336904a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:14:35 +0100 Subject: [PATCH 2/7] Return `PathBuf` from `Model::lib_path` --- src/model.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/model.rs b/src/model.rs index 0a9f8ce65..aa18b265a 100644 --- a/src/model.rs +++ b/src/model.rs @@ -25,7 +25,7 @@ impl Model { src_path } - pub fn lib_path(&self) -> String { + pub fn lib_path(&self) -> PathBuf { let name = self.name().replace('-', "_"); let file = if cfg!(windows) { @@ -37,7 +37,11 @@ impl Model { format!("lib{}.so", name) }; - format!("{}/target/debug/{}", self.path(), file) + let mut lib_path = PathBuf::from(self.path()); + lib_path.push("target/debug"); + lib_path.push(file); + + lib_path } pub fn load( From f47dd06c73e1748a331e47bcfa88ca9511a75d0f Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:16:29 +0100 Subject: [PATCH 3/7] Refactor --- src/model.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/model.rs b/src/model.rs index aa18b265a..9a81f1f45 100644 --- a/src/model.rs +++ b/src/model.rs @@ -48,9 +48,11 @@ impl Model { &self, arguments: &HashMap, ) -> Result { + let manifest_path = format!("{}/Cargo.toml", self.path()); + let status = Command::new("cargo") .arg("build") - .args(["--manifest-path", &format!("{}/Cargo.toml", self.path())]) + .args(["--manifest-path", &manifest_path]) .status()?; if !status.success() { From fbcfac672fe383699253d8b257c33e61a55903bb Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:19:31 +0100 Subject: [PATCH 4/7] Return `PathBuf` from `Model::path` --- src/model.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/model.rs b/src/model.rs index 9a81f1f45..04f392dd8 100644 --- a/src/model.rs +++ b/src/model.rs @@ -15,14 +15,14 @@ impl Model { &self.name } - pub fn path(&self) -> String { - format!("models/{}", self.name) + pub fn path(&self) -> PathBuf { + let mut path = PathBuf::from("models"); + path.push(self.name()); + path } pub fn src_path(&self) -> PathBuf { - let mut src_path = PathBuf::from(self.path()); - src_path.push("src"); - src_path + self.path().join("src") } pub fn lib_path(&self) -> PathBuf { @@ -37,18 +37,15 @@ impl Model { format!("lib{}.so", name) }; - let mut lib_path = PathBuf::from(self.path()); - lib_path.push("target/debug"); - lib_path.push(file); - - lib_path + self.path().join("target/debug").join(file) } pub fn load( &self, arguments: &HashMap, ) -> Result { - let manifest_path = format!("{}/Cargo.toml", self.path()); + let manifest_path = + self.path().join("Cargo.toml").display().to_string(); let status = Command::new("cargo") .arg("build") From b023fff5577f39e051708cca355e866bf712ebb0 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:21:05 +0100 Subject: [PATCH 5/7] Return `String` from `Model::name` --- src/model.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/model.rs b/src/model.rs index 04f392dd8..16bb43565 100644 --- a/src/model.rs +++ b/src/model.rs @@ -11,8 +11,8 @@ impl Model { Self { name } } - pub fn name(&self) -> &str { - &self.name + pub fn name(&self) -> String { + self.name.clone() } pub fn path(&self) -> PathBuf { From 88dfe0c9aa5a13a271020a4378afd0e92d1bee3a Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:45:21 +0100 Subject: [PATCH 6/7] Make `model` argument a path --- src/args.rs | 2 +- src/model.rs | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/args.rs b/src/args.rs index 0529d16f6..d1669cbd1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; pub struct Args { /// The model to open #[clap(short, long, default_value = "cuboid")] - pub model: String, + pub model: PathBuf, /// Export model to this path #[clap(short, long)] diff --git a/src/model.rs b/src/model.rs index 16bb43565..b71c21ea0 100644 --- a/src/model.rs +++ b/src/model.rs @@ -3,30 +3,37 @@ use std::{collections::HashMap, io, path::PathBuf, process::Command}; use thiserror::Error; pub struct Model { - name: String, + path: PathBuf, } impl Model { - pub fn new(name: String) -> Self { - Self { name } + pub fn new(rel_path: PathBuf) -> Self { + let mut path = PathBuf::from("models"); + path.push(rel_path); + + Self { path } } - pub fn name(&self) -> String { - self.name.clone() + pub fn name(&self) -> Result { + let canonical = self.path.canonicalize()?; + + // Can't panic. It only would, if the path ends with "..", and we just + // canonicalized it. + let file_name = canonical.file_name().unwrap(); + + Ok(file_name.to_string_lossy().into_owned()) } pub fn path(&self) -> PathBuf { - let mut path = PathBuf::from("models"); - path.push(self.name()); - path + self.path.clone() } pub fn src_path(&self) -> PathBuf { self.path().join("src") } - pub fn lib_path(&self) -> PathBuf { - let name = self.name().replace('-', "_"); + pub fn lib_path(&self) -> Result { + let name = self.name()?.replace('-', "_"); let file = if cfg!(windows) { format!("{}.dll", name) @@ -37,7 +44,7 @@ impl Model { format!("lib{}.so", name) }; - self.path().join("target/debug").join(file) + Ok(self.path().join("target/debug").join(file)) } pub fn load( @@ -73,7 +80,7 @@ impl Model { // to switch to a better technique: // https://github.com/hannobraun/Fornjot/issues/71 let shape = unsafe { - let lib = libloading::Library::new(self.lib_path())?; + let lib = libloading::Library::new(self.lib_path()?)?; let model: libloading::Symbol = lib.get(b"model")?; model(arguments) }; From 91d16afb23ee46d0a2975fcb2ec69f3130d11e23 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 13:46:12 +0100 Subject: [PATCH 7/7] Simplify `Model::path` --- src/model.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/model.rs b/src/model.rs index b71c21ea0..a3bea9c30 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,4 +1,9 @@ -use std::{collections::HashMap, io, path::PathBuf, process::Command}; +use std::{ + collections::HashMap, + io, + path::{Path, PathBuf}, + process::Command, +}; use thiserror::Error; @@ -24,8 +29,8 @@ impl Model { Ok(file_name.to_string_lossy().into_owned()) } - pub fn path(&self) -> PathBuf { - self.path.clone() + pub fn path(&self) -> &Path { + &self.path } pub fn src_path(&self) -> PathBuf {