use crate::error::Error;
use crate::platform;
use crate::platform::ffi;
#[derive(Clone, Debug)]
pub struct Platform {
pub architecture: String,
pub os: String,
pub os_version: Option<String>,
pub os_features: Option<Vec<String>>,
pub variant: Option<String>,
}
impl PartialEq for Platform {
fn eq(&self, other: &Self) -> bool {
platform::outcome(
ffi::cz_platform_equals(self.clone(), other.clone()),
"compare two platforms",
)
.map(|outcome| outcome.boolean())
.unwrap_or_else(|_| {
(
&self.architecture,
&self.os,
&self.os_version,
&self.os_features,
&self.variant,
) == (
&other.architecture,
&other.os,
&other.os_version,
&other.os_features,
&other.variant,
)
})
}
}
impl Eq for Platform {}
impl Platform {
pub fn current() -> Result<Self, Error> {
platform::outcome(ffi::cz_platform_current(), "read the current platform").map(|outcome| outcome.platform())
}
pub fn parse(platform: &str) -> Result<Self, Error> {
platform::outcome(
ffi::cz_platform_parse(platform),
format!("parse {platform:?} as a platform"),
)
.map(|outcome| outcome.platform())
}
pub fn description(&self) -> Result<String, Error> {
platform::outcome(ffi::cz_platform_description(self.clone()), "describe a platform").map(|outcome| outcome.text())
}
pub fn matches(&self, other: &Platform) -> Result<bool, Error> {
platform::outcome(
ffi::cz_platform_matches(self.clone(), other.clone()),
"check whether one platform runs on another",
)
.map(|outcome| outcome.boolean())
}
}
pub(crate) fn crossing(platform: Option<&Platform>) -> (bool, Platform) {
match platform {
Some(platform) => (true, platform.clone()),
None => (
false,
Platform {
architecture: String::new(),
os: String::new(),
os_version: None,
os_features: None,
variant: None,
},
),
}
}