Merge pull request #384 from ozghimire/platform-api

Refactoring as per review
This commit is contained in:
Hanno Braun 2022-03-21 14:39:29 +01:00 committed by GitHub
commit f8dc1559ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 12 deletions

View File

@ -51,7 +51,7 @@ impl Model {
let src_path = path.join("src"); let src_path = path.join("src");
let lib_path = { let lib_path = {
let file = HostPlatform::host_file_name(&name); let file = HostPlatform::lib_file_name(&name);
let target_dir = target_dir.unwrap_or_else(|| path.join("target")); let target_dir = target_dir.unwrap_or_else(|| path.join("target"));
target_dir.join("debug").join(file) target_dir.join("debug").join(file)
}; };

View File

@ -1,9 +1,9 @@
// Represents platform trait // Represents platform trait
trait Platform { pub trait Platform {
fn file_name(name: &str) -> String; fn model_lib_file_name(&self, name: &str) -> String;
} }
// Represents all platforms supported // Represents all supported platforms
// Mac OS // Mac OS
struct Macos; struct Macos;
@ -13,17 +13,19 @@ struct Windows;
struct Unix; struct Unix;
impl Platform for Windows { impl Platform for Windows {
fn file_name(name: &str) -> String { fn model_lib_file_name(&self, name: &str) -> String {
format!("{}.dll", name) format!("{}.dll", name)
} }
} }
impl Platform for Macos { impl Platform for Macos {
fn file_name(name: &str) -> String { fn model_lib_file_name(&self, name: &str) -> String {
format!("lib{}.dylib", name) format!("lib{}.dylib", name)
} }
} }
impl Platform for Unix { impl Platform for Unix {
fn file_name(name: &str) -> String { fn model_lib_file_name(&self, name: &str) -> String {
format!("lib{}.so", name) format!("lib{}.so", name)
} }
} }
@ -32,14 +34,17 @@ impl Platform for Unix {
pub struct HostPlatform; pub struct HostPlatform;
impl HostPlatform { impl HostPlatform {
pub fn host_file_name(name: &str) -> String { pub fn get_os() -> Box<dyn Platform> {
if cfg!(windows) { if cfg!(windows) {
Windows::file_name(name) Box::new(Windows)
} else if cfg!(target_os = "macos") { } else if cfg!(target_os = "macos") {
Macos::file_name(name) Box::new(Macos)
} else { } else {
//Unix Box::new(Unix)
Unix::file_name(name)
} }
} }
pub fn lib_file_name(name: &str) -> String {
Self::get_os().model_lib_file_name(name)
}
} }