Skip to content

Commit

Permalink
add FlakeAttr::as_list
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Mar 1, 2024
1 parent c02faba commit 90514e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/nix_rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
edition.workspace = true
name = "nix_rs"
# Important: remember to update the top-level Cargo.toml if updating major version
version = "0.3.0"
version = "0.3.1"
license.workspace = true
repository.workspace = true
description = "Rust library for interacting with the Nix command"
Expand Down
28 changes: 27 additions & 1 deletion crates/nix_rs/src/flake/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,25 @@ impl FlakeAttr {
pub fn is_none(&self) -> bool {
self.0.is_none()
}

/// Return nested attrs if the user specified one is separated by '.'
pub fn as_list(&self) -> Vec<String> {
self.0
.clone()
.map(|s| s.split('.').map(|s| s.to_string()).collect())
.unwrap_or_default()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_flake_url() {
fn test_flake_url_and_attr() {
let url = FlakeUrl("github:srid/nixci".to_string());
assert_eq!(url.split_attr(), (url.clone(), FlakeAttr(None)));
assert_eq!(url.split_attr().1.as_list(), [] as [&str; 0]);

let url = FlakeUrl("github:srid/nixci#extra-tests".to_string());
assert_eq!(
Expand All @@ -162,6 +171,23 @@ mod tests {
FlakeAttr(Some("extra-tests".to_string()))
)
);
assert_eq!(
url.split_attr().1.as_list(),
vec!["extra-tests".to_string()]
);

let url = FlakeUrl(".#foo.bar.qux".to_string());
assert_eq!(
url.split_attr(),
(
FlakeUrl(".".to_string()),
FlakeAttr(Some("foo.bar.qux".to_string()))
)
);
assert_eq!(
url.split_attr().1.as_list(),
vec!["foo".to_string(), "bar".to_string(), "qux".to_string()]
)
}

#[test]
Expand Down

0 comments on commit 90514e4

Please sign in to comment.