From 31926784f36d52f6f47402da4827e2d62f290a44 Mon Sep 17 00:00:00 2001 From: Hanno Braun Date: Wed, 16 Mar 2022 14:29:59 +0100 Subject: [PATCH] Compute all `Model` paths in constructor This is both simpler, and opens the door for using `Model` in more flexible ways. --- src/main.rs | 2 +- src/model.rs | 73 +++++++++++++++++++++++++--------------------------- 2 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index a5765b5c0..a2c8b65c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,7 @@ fn main() -> anyhow::Result<()> { let mut path = config.default_path; path.push(args.model.unwrap_or(config.default_model)); - let model = Model::from_path(path); + let model = Model::from_path(path)?; let mut parameters = HashMap::new(); for parameter in args.parameters { diff --git a/src/model.rs b/src/model.rs index c4bc4e97a..fe103d3bc 100644 --- a/src/model.rs +++ b/src/model.rs @@ -1,60 +1,57 @@ -use std::{ - collections::HashMap, - io, - path::{Path, PathBuf}, - process::Command, -}; +use std::{collections::HashMap, io, path::PathBuf, process::Command}; use thiserror::Error; pub struct Model { - path: PathBuf, + src_path: PathBuf, + lib_path: PathBuf, + manifest_path: PathBuf, } impl Model { - pub fn from_path(path: PathBuf) -> Self { - Self { path } - } + pub fn from_path(path: PathBuf) -> Result { + let name = { + // Can't panic. It only would, if the path ends with "..", and we + // are canonicalizing it here to prevent that. + let canonical = path.canonicalize()?; + let file_name = canonical.file_name().unwrap(); - pub fn name(&self) -> Result { - let canonical = self.path.canonicalize()?; + file_name.to_string_lossy().replace('-', "_") + }; - // Can't panic. It only would, if the path ends with "..", and we just - // canonicalized it. - let file_name = canonical.file_name().unwrap(); + let src_path = path.join("src"); - Ok(file_name.to_string_lossy().into_owned()) - } + let lib_path = { + let file = if cfg!(windows) { + format!("{}.dll", name) + } else if cfg!(target_os = "macos") { + format!("lib{}.dylib", name) + } else { + //Unix + format!("lib{}.so", name) + }; - pub fn path(&self) -> &Path { - &self.path + path.join("target/debug").join(file) + }; + + let manifest_path = path.join("Cargo.toml"); + + Ok(Self { + src_path, + lib_path, + manifest_path, + }) } pub fn src_path(&self) -> PathBuf { - self.path().join("src") - } - - pub fn lib_path(&self) -> Result { - let name = self.name()?.replace('-', "_"); - - let file = if cfg!(windows) { - format!("{}.dll", name) - } else if cfg!(target_os = "macos") { - format!("lib{}.dylib", name) - } else { - //Unix - format!("lib{}.so", name) - }; - - Ok(self.path().join("target/debug").join(file)) + self.src_path.clone() } pub fn load( &self, arguments: &HashMap, ) -> Result { - let manifest_path = - self.path().join("Cargo.toml").display().to_string(); + let manifest_path = self.manifest_path.display().to_string(); let status = Command::new("cargo") .arg("build") @@ -82,7 +79,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) };