From d07bc6a4b82211e788ff1590b81a59db77e2a80d Mon Sep 17 00:00:00 2001 From: Manoj Ghimire Date: Sun, 20 Mar 2022 11:38:34 -0400 Subject: [PATCH] Added platform trait and specific platforms implementations plust HostPlatform to provide common api --- fj-host/src/lib.rs | 14 +++++-------- fj-host/src/platform.rs | 45 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 fj-host/src/platform.rs diff --git a/fj-host/src/lib.rs b/fj-host/src/lib.rs index 4a8f87f533..1ccffe3f7d 100644 --- a/fj-host/src/lib.rs +++ b/fj-host/src/lib.rs @@ -5,6 +5,8 @@ #![deny(missing_docs)] +mod platform; + use std::{ collections::{HashMap, HashSet}, ffi::OsStr, @@ -18,6 +20,8 @@ use std::{ use notify::Watcher as _; use thiserror::Error; +use self::platform::HostPlatform; + /// Represents a Fornjot model pub struct Model { src_path: PathBuf, @@ -47,15 +51,7 @@ impl Model { let src_path = path.join("src"); 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) - }; - + let file = HostPlatform::host_file_name(&name); let target_dir = target_dir.unwrap_or_else(|| path.join("target")); target_dir.join("debug").join(file) }; diff --git a/fj-host/src/platform.rs b/fj-host/src/platform.rs new file mode 100644 index 0000000000..ae71162af2 --- /dev/null +++ b/fj-host/src/platform.rs @@ -0,0 +1,45 @@ +// Represents platform trait +trait Platform { + fn file_name(name: &str) -> String; +} + +// Represents all platforms supported + +// Mac OS +struct Macos; +// Windows +struct Windows; +// Linux +struct Unix; + +impl Platform for Windows { + fn file_name(name: &str) -> String { + format!("{}.dll", name) + } +} +impl Platform for Macos { + fn file_name(name: &str) -> String { + format!("lib{}.dylib", name) + } +} +impl Platform for Unix { + fn file_name(name: &str) -> String { + format!("lib{}.so", name) + } +} + +// Represents common apis availiable independent of hosts +pub struct HostPlatform; + +impl HostPlatform { + pub fn host_file_name(name: &str) -> String { + if cfg!(windows) { + Windows::file_name(name) + } else if cfg!(target_os = "macos") { + Macos::file_name(name) + } else { + //Unix + Unix::file_name(name) + } + } +}