-
Notifications
You must be signed in to change notification settings - Fork 210
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: serialize system requirements #2753
Merged
Hofer-Julian
merged 3 commits into
prefix-dev:main
from
ruben-arts:feature/ser_system_req
Dec 20, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
use miette::Diagnostic; | ||
use rattler_conda_types::Version; | ||
use rattler_virtual_packages::{Cuda, LibC, Linux, Osx, VirtualPackage}; | ||
use serde::Deserialize; | ||
use serde::{Deserialize, Serialize}; | ||
use serde_value::Value; | ||
use serde_with::{serde_as, DisplayFromStr}; | ||
use std::collections::BTreeMap; | ||
use std::str::FromStr; | ||
use thiserror::Error; | ||
|
||
const GLIBC_FAMILY: &str = "glibc"; | ||
|
||
/// Describes the minimal system requirements to be able to run a certain environment. | ||
#[serde_as] | ||
#[derive(Debug, Clone, Deserialize, Default, PartialEq, Eq)] | ||
#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Eq)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct SystemRequirements { | ||
/// Dictates the minimum version of macOS required. | ||
|
@@ -124,6 +126,15 @@ impl SystemRequirements { | |
archspec, | ||
}) | ||
} | ||
|
||
/// Returns true if the system requirements are empty, meaning that no requirements were specified. | ||
pub fn is_empty(&self) -> bool { | ||
self.linux.is_none() | ||
&& self.cuda.is_none() | ||
&& self.macos.is_none() | ||
&& self.libc.is_none() | ||
&& self.archspec.is_none() | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Error, Diagnostic)] | ||
|
@@ -183,8 +194,33 @@ impl LibCSystemRequirement { | |
} | ||
} | ||
|
||
impl Serialize for LibCSystemRequirement { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
match self { | ||
LibCSystemRequirement::GlibC(version) => serializer.serialize_str(&version.to_string()), | ||
LibCSystemRequirement::OtherFamily(LibCFamilyAndVersion { family, version }) => { | ||
let mut map = BTreeMap::new(); // Initialize the BTreeMap | ||
if let Some(fam) = family { | ||
map.insert( | ||
Value::String("family".to_string()), | ||
Value::String(fam.clone()), | ||
); | ||
} | ||
map.insert( | ||
Value::String("version".to_string()), | ||
Value::String(version.to_string()), | ||
); | ||
Value::Map(map).serialize(serializer) // Wrap the map in Value::Map | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[serde_as] | ||
#[derive(Debug, Clone, Deserialize)] | ||
#[derive(Debug, Clone, Deserialize, Serialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct LibCFamilyAndVersion { | ||
/// The libc family, e.g. glibc | ||
|
@@ -225,6 +261,7 @@ mod tests { | |
use rattler_virtual_packages::{Cuda, LibC, Linux, Osx, VirtualPackage}; | ||
use serde::Deserialize; | ||
use std::str::FromStr; | ||
use toml_edit::ser::to_string_pretty; | ||
|
||
#[test] | ||
fn system_requirements_works() { | ||
|
@@ -471,4 +508,61 @@ mod tests { | |
|
||
assert_matches!(eglibc_2_17.union(&glibc_2_12).unwrap_err(), SystemRequirementsUnionError::DifferentLibcFamilies(fam_a, fam_b) if fam_a == "eglibc" && fam_b == "glibc"); | ||
} | ||
|
||
#[test] | ||
fn test_serialization() { | ||
let system_requirements = SystemRequirements { | ||
macos: Some(Version::from_str("10.15").unwrap()), | ||
linux: Some(Version::from_str("5.11").unwrap()), | ||
cuda: Some(Version::from_str("12.2").unwrap()), | ||
libc: Some(LibCSystemRequirement::GlibC( | ||
Version::from_str("2.12").unwrap(), | ||
)), | ||
archspec: Some("x86_64".to_string()), | ||
}; | ||
|
||
let serialized = to_string_pretty(&system_requirements).unwrap(); | ||
|
||
let expected = r#" | ||
macos = "10.15" | ||
linux = "5.11" | ||
cuda = "12.2" | ||
libc = "2.12" | ||
archspec = "x86_64" | ||
"#; | ||
assert_eq!( | ||
serialized.replace("\n", "").replace(" ", ""), | ||
expected.replace("\n", "").replace(" ", "") | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_serialization_other_family() { | ||
let system_requirements = SystemRequirements { | ||
macos: Some(Version::from_str("10.15").unwrap()), | ||
linux: Some(Version::from_str("5.11").unwrap()), | ||
cuda: Some(Version::from_str("12.2").unwrap()), | ||
libc: Some(LibCSystemRequirement::OtherFamily(LibCFamilyAndVersion { | ||
family: Some("glibc".to_string()), | ||
version: Version::from_str("2.12").unwrap(), | ||
})), | ||
archspec: Some("x86_64".to_string()), | ||
}; | ||
|
||
let serialized = to_string_pretty(&system_requirements).unwrap(); | ||
let expected = r#" | ||
macos = "10.15" | ||
linux = "5.11" | ||
cuda = "12.2" | ||
archspec = "x86_64" | ||
|
||
[libc] | ||
family = "glibc" | ||
version = "2.12" | ||
"#; | ||
assert_eq!( | ||
serialized.replace("\n", "").replace(" ", ""), | ||
expected.replace("\n", "").replace(" ", "") | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use shapshots here as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use snapshots here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done