From 90514e4adcf03638e9057fc677ca74fc477ebfa5 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Fri, 1 Mar 2024 12:50:42 -0500 Subject: [PATCH] add FlakeAttr::as_list --- Cargo.lock | 2 +- crates/nix_rs/Cargo.toml | 2 +- crates/nix_rs/src/flake/url.rs | 28 +++++++++++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a831df6..dbd71ffb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2388,7 +2388,7 @@ dependencies = [ [[package]] name = "nix_rs" -version = "0.3.0" +version = "0.3.1" dependencies = [ "bytesize", "cfg-if", diff --git a/crates/nix_rs/Cargo.toml b/crates/nix_rs/Cargo.toml index 918fcf9f..27e1ee31 100644 --- a/crates/nix_rs/Cargo.toml +++ b/crates/nix_rs/Cargo.toml @@ -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" diff --git a/crates/nix_rs/src/flake/url.rs b/crates/nix_rs/src/flake/url.rs index e3288760..c8f03d3e 100644 --- a/crates/nix_rs/src/flake/url.rs +++ b/crates/nix_rs/src/flake/url.rs @@ -143,6 +143,14 @@ 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 { + self.0 + .clone() + .map(|s| s.split('.').map(|s| s.to_string()).collect()) + .unwrap_or_default() + } } #[cfg(test)] @@ -150,9 +158,10 @@ 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!( @@ -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]