Skip to content

Commit

Permalink
flake-info: support platform patterns
Browse files Browse the repository at this point in the history
...for some value of "support". For now, just skip them without causing
an error. Later, maybe figure out a way to display them properly.

An example of a pattern is `lib.systems.inspect.patterns.isGnu`.
  • Loading branch information
ncfavier committed Jan 24, 2023
1 parent 8f14293 commit 3e577b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
16 changes: 4 additions & 12 deletions flake-info/src/data/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,11 @@ impl TryFrom<import::NixpkgsEntry> for Derivation {
.map(|l: &License| l.fullName.to_owned())
.collect();

let platforms: HashSet<String> = package
.meta
.platforms
.map_or(Default::default(), Flatten::flatten)
.into_iter()
.collect();
let platforms: HashSet<String> =
package.meta.platforms.unwrap_or_default().collect();

let bad_platforms: HashSet<String> = package
.meta
.bad_platforms
.map_or(Default::default(), Flatten::flatten)
.into_iter()
.collect();
let bad_platforms: HashSet<String> =
package.meta.bad_platforms.unwrap_or_default().collect();

let platforms: Vec<String> =
platforms.difference(&bad_platforms).cloned().collect();
Expand Down
37 changes: 34 additions & 3 deletions flake-info/src/data/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ pub struct Meta {
pub license: Option<OneOrMany<StringOrStruct<License>>>,
pub maintainers: Option<Flatten<Maintainer>>,
pub homepage: Option<OneOrMany<String>>,
pub platforms: Option<Flatten<String>>,
pub platforms: Option<Platforms>,
#[serde(rename = "badPlatforms")]
pub bad_platforms: Option<Flatten<String>>,
pub bad_platforms: Option<Platforms>,
pub position: Option<String>,
pub description: Option<String>,
#[serde(rename = "longDescription")]
Expand Down Expand Up @@ -252,6 +252,36 @@ where
}
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Platform {
System(String),
Pattern {}, // TODO how should those be displayed?
}

#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Platforms(Flatten<Platform>);

impl Platforms {
// A bit of abstract nonsense: what we really want is
// into_iter : Platforms → ∃ (I : Iterator<String>). I
// however Rust makes this annoying to write: we would either have to pick a
// concrete iterator type or use something like Box<dyn Iterator<Item = String>>.
// Instead, we can use the dual Church-encoded form of that existential type:
// ? : Platforms → ∀ B. (∀ (I : Iterator<String>). I → B) → B
// ...which is exactly the type of collect! (think about what FromIterator means)
pub fn collect<B: std::iter::FromIterator<String>>(self) -> B {
self.0
.flatten()
.into_iter()
.flat_map(|p| match p {
Platform::System(s) => Some(s),
_ => None,
})
.collect()
}
}

/// Different representations of the licence attribute
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
Expand Down Expand Up @@ -388,7 +418,8 @@ mod tests {
"powerpc64-linux",
"powerpc64le-linux",
"riscv32-linux",
"riscv64-linux"
"riscv64-linux",
{}
],
"position": "/nix/store/97lxf2n6zip41j5flbv6b0928mxv9za8-nixpkgs-unstable-21.03pre268853.d9c6f13e13f/nixpkgs-unstable/pkgs/games/0verkill/default.nix:34",
"unfree": false,
Expand Down
6 changes: 6 additions & 0 deletions flake-info/src/data/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ impl<T: Clone> Flatten<T> {
}
}

impl<T> Default for Flatten<T> {
fn default() -> Self {
Flatten::Deep(Vec::new())
}
}

// TODO: use this or a to_ist function?
/// Serialization helper that serializes single elements as a list with a single
/// item
Expand Down

0 comments on commit 3e577b1

Please sign in to comment.