use std::sync::Arc;
use pep440_rs::{Version, VersionParseError};
use crate::{MarkerValueString, MarkerValueVersion, StringVersion};
#[allow(missing_docs, clippy::unsafe_derive_deserialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct MarkerEnvironment {
#[serde(flatten)]
inner: Arc<MarkerEnvironmentInner>,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
struct MarkerEnvironmentInner {
implementation_name: String,
implementation_version: StringVersion,
os_name: String,
platform_machine: String,
platform_python_implementation: String,
platform_release: String,
platform_system: String,
platform_version: String,
python_full_version: StringVersion,
python_version: StringVersion,
sys_platform: String,
}
impl MarkerEnvironment {
pub fn get_version(&self, key: &MarkerValueVersion) -> &Version {
match key {
MarkerValueVersion::ImplementationVersion => &self.implementation_version().version,
MarkerValueVersion::PythonFullVersion => &self.python_full_version().version,
MarkerValueVersion::PythonVersion => &self.python_version().version,
}
}
pub fn get_string(&self, key: &MarkerValueString) -> &str {
match key {
MarkerValueString::ImplementationName => self.implementation_name(),
MarkerValueString::OsName | MarkerValueString::OsNameDeprecated => self.os_name(),
MarkerValueString::PlatformMachine | MarkerValueString::PlatformMachineDeprecated => {
self.platform_machine()
}
MarkerValueString::PlatformPythonImplementation
| MarkerValueString::PlatformPythonImplementationDeprecated
| MarkerValueString::PythonImplementationDeprecated => {
self.platform_python_implementation()
}
MarkerValueString::PlatformRelease => self.platform_release(),
MarkerValueString::PlatformSystem => self.platform_system(),
MarkerValueString::PlatformVersion | MarkerValueString::PlatformVersionDeprecated => {
self.platform_version()
}
MarkerValueString::SysPlatform | MarkerValueString::SysPlatformDeprecated => {
self.sys_platform()
}
}
}
}
impl MarkerEnvironment {
#[inline]
pub fn implementation_name(&self) -> &str {
&self.inner.implementation_name
}
#[inline]
pub fn implementation_version(&self) -> &StringVersion {
&self.inner.implementation_version
}
#[inline]
pub fn os_name(&self) -> &str {
&self.inner.os_name
}
#[inline]
pub fn platform_machine(&self) -> &str {
&self.inner.platform_machine
}
#[inline]
pub fn platform_python_implementation(&self) -> &str {
&self.inner.platform_python_implementation
}
#[inline]
pub fn platform_release(&self) -> &str {
&self.inner.platform_release
}
#[inline]
pub fn platform_system(&self) -> &str {
&self.inner.platform_system
}
#[inline]
pub fn platform_version(&self) -> &str {
&self.inner.platform_version
}
#[inline]
pub fn python_full_version(&self) -> &StringVersion {
&self.inner.python_full_version
}
#[inline]
pub fn python_version(&self) -> &StringVersion {
&self.inner.python_version
}
#[inline]
pub fn sys_platform(&self) -> &str {
&self.inner.sys_platform
}
}
impl MarkerEnvironment {
#[inline]
#[must_use]
pub fn with_implementation_name(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).implementation_name = value.into();
self
}
#[inline]
#[must_use]
pub fn with_implementation_version(
mut self,
value: impl Into<StringVersion>,
) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).implementation_version = value.into();
self
}
#[inline]
#[must_use]
pub fn with_os_name(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).os_name = value.into();
self
}
#[inline]
#[must_use]
pub fn with_platform_machine(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).platform_machine = value.into();
self
}
#[inline]
#[must_use]
pub fn with_platform_python_implementation(
mut self,
value: impl Into<String>,
) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).platform_python_implementation = value.into();
self
}
#[inline]
#[must_use]
pub fn with_platform_release(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).platform_release = value.into();
self
}
#[inline]
#[must_use]
pub fn with_platform_system(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).platform_system = value.into();
self
}
#[inline]
#[must_use]
pub fn with_platform_version(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).platform_version = value.into();
self
}
#[inline]
#[must_use]
pub fn with_python_full_version(
mut self,
value: impl Into<StringVersion>,
) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).python_full_version = value.into();
self
}
#[inline]
#[must_use]
pub fn with_python_version(mut self, value: impl Into<StringVersion>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).python_version = value.into();
self
}
#[inline]
#[must_use]
pub fn with_sys_platform(mut self, value: impl Into<String>) -> MarkerEnvironment {
Arc::make_mut(&mut self.inner).sys_platform = value.into();
self
}
}
#[allow(missing_docs)]
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct MarkerEnvironmentBuilder<'a> {
pub implementation_name: &'a str,
pub implementation_version: &'a str,
pub os_name: &'a str,
pub platform_machine: &'a str,
pub platform_python_implementation: &'a str,
pub platform_release: &'a str,
pub platform_system: &'a str,
pub platform_version: &'a str,
pub python_full_version: &'a str,
pub python_version: &'a str,
pub sys_platform: &'a str,
}
impl<'a> TryFrom<MarkerEnvironmentBuilder<'a>> for MarkerEnvironment {
type Error = VersionParseError;
fn try_from(builder: MarkerEnvironmentBuilder<'a>) -> Result<Self, Self::Error> {
Ok(MarkerEnvironment {
inner: Arc::new(MarkerEnvironmentInner {
implementation_name: builder.implementation_name.to_string(),
implementation_version: builder.implementation_version.parse()?,
os_name: builder.os_name.to_string(),
platform_machine: builder.platform_machine.to_string(),
platform_python_implementation: builder.platform_python_implementation.to_string(),
platform_release: builder.platform_release.to_string(),
platform_system: builder.platform_system.to_string(),
platform_version: builder.platform_version.to_string(),
python_full_version: builder.python_full_version.parse()?,
python_version: builder.python_version.parse()?,
sys_platform: builder.sys_platform.to_string(),
}),
})
}
}