From e5177ae930364a0fab3bfb383ca133a313aeae74 Mon Sep 17 00:00:00 2001 From: Brian Olsen Date: Mon, 4 Aug 2025 23:31:25 +0200 Subject: [PATCH] feat(nix-compat): Use serde_with for HashAlgo serialization We already have serde_with in the workspace and using it cuts down on code lines (very slightly). This also adds a FromStr implementation for HashAlgo in addition to the existing TryFrom<&str>. Change-Id: I1b525d7391f3881d54cef0c74d22198ad394f47d Reviewed-on: https://cl.snix.dev/c/snix/+/30652 Reviewed-by: Florian Klink Tested-by: besadii Autosubmit: Brian Olsen --- contrib/crunch-v2/Cargo.nix | 2 +- contrib/fetchroots/Cargo.nix | 2 +- contrib/narinfo2parquet/Cargo.nix | 2 +- contrib/weave/Cargo.nix | 2 +- snix/Cargo.lock | 1 + snix/Cargo.nix | 13 ++++++- snix/Cargo.toml | 4 +- .../ui/deserialize_from_str_missing.stderr | 2 +- snix/nix-compat/Cargo.toml | 3 +- snix/nix-compat/src/nixhash/algos.rs | 38 +++++++------------ 10 files changed, 36 insertions(+), 33 deletions(-) diff --git a/contrib/crunch-v2/Cargo.nix b/contrib/crunch-v2/Cargo.nix index 66fc29e8a..f5fa2d171 100644 --- a/contrib/crunch-v2/Cargo.nix +++ b/contrib/crunch-v2/Cargo.nix @@ -4178,7 +4178,7 @@ rec { "futures" = [ "dep:futures" ]; "nix-compat-derive" = [ "dep:nix-compat-derive" ]; "pin-project-lite" = [ "dep:pin-project-lite" ]; - "serde" = [ "dep:serde" "dep:serde_json" ]; + "serde" = [ "dep:serde" "dep:serde_json" "dep:serde_with" ]; "tokio" = [ "dep:tokio" ]; "url" = [ "dep:url" ]; "wire" = [ "tokio" "pin-project-lite" "bytes" ]; diff --git a/contrib/fetchroots/Cargo.nix b/contrib/fetchroots/Cargo.nix index 6e245d6e7..f16261fab 100644 --- a/contrib/fetchroots/Cargo.nix +++ b/contrib/fetchroots/Cargo.nix @@ -5611,7 +5611,7 @@ rec { "futures" = [ "dep:futures" ]; "nix-compat-derive" = [ "dep:nix-compat-derive" ]; "pin-project-lite" = [ "dep:pin-project-lite" ]; - "serde" = [ "dep:serde" "dep:serde_json" ]; + "serde" = [ "dep:serde" "dep:serde_json" "dep:serde_with" ]; "tokio" = [ "dep:tokio" ]; "url" = [ "dep:url" ]; "wire" = [ "tokio" "pin-project-lite" "bytes" ]; diff --git a/contrib/narinfo2parquet/Cargo.nix b/contrib/narinfo2parquet/Cargo.nix index b7f67b647..7ac213278 100644 --- a/contrib/narinfo2parquet/Cargo.nix +++ b/contrib/narinfo2parquet/Cargo.nix @@ -2905,7 +2905,7 @@ rec { "futures" = [ "dep:futures" ]; "nix-compat-derive" = [ "dep:nix-compat-derive" ]; "pin-project-lite" = [ "dep:pin-project-lite" ]; - "serde" = [ "dep:serde" "dep:serde_json" ]; + "serde" = [ "dep:serde" "dep:serde_json" "dep:serde_with" ]; "tokio" = [ "dep:tokio" ]; "url" = [ "dep:url" ]; "wire" = [ "tokio" "pin-project-lite" "bytes" ]; diff --git a/contrib/weave/Cargo.nix b/contrib/weave/Cargo.nix index 77f93fb3c..f262f0d81 100644 --- a/contrib/weave/Cargo.nix +++ b/contrib/weave/Cargo.nix @@ -2987,7 +2987,7 @@ rec { "futures" = [ "dep:futures" ]; "nix-compat-derive" = [ "dep:nix-compat-derive" ]; "pin-project-lite" = [ "dep:pin-project-lite" ]; - "serde" = [ "dep:serde" "dep:serde_json" ]; + "serde" = [ "dep:serde" "dep:serde_json" "dep:serde_with" ]; "tokio" = [ "dep:tokio" ]; "url" = [ "dep:url" ]; "wire" = [ "tokio" "pin-project-lite" "bytes" ]; diff --git a/snix/Cargo.lock b/snix/Cargo.lock index 04a261a23..94ac6d91a 100644 --- a/snix/Cargo.lock +++ b/snix/Cargo.lock @@ -2613,6 +2613,7 @@ dependencies = [ "rstest", "serde", "serde_json", + "serde_with", "sha2", "smol_str", "thiserror 2.0.9", diff --git a/snix/Cargo.nix b/snix/Cargo.nix index cae6bbefc..9a1301d04 100644 --- a/snix/Cargo.nix +++ b/snix/Cargo.nix @@ -8300,6 +8300,13 @@ rec { packageId = "serde_json"; optional = true; } + { + name = "serde_with"; + packageId = "serde_with"; + optional = true; + usesDefaultFeatures = false; + features = [ "macros" ]; + } { name = "sha2"; packageId = "sha2"; @@ -8383,7 +8390,7 @@ rec { "futures" = [ "dep:futures" ]; "nix-compat-derive" = [ "dep:nix-compat-derive" ]; "pin-project-lite" = [ "dep:pin-project-lite" ]; - "serde" = [ "dep:serde" "dep:serde_json" ]; + "serde" = [ "dep:serde" "dep:serde_json" "dep:serde_with" ]; "tokio" = [ "dep:tokio" ]; "url" = [ "dep:url" ]; "wire" = [ "tokio" "pin-project-lite" "bytes" ]; @@ -13770,6 +13777,8 @@ rec { { name = "serde_with"; packageId = "serde_with"; + usesDefaultFeatures = false; + features = [ "macros" ]; } { name = "snix-tracing"; @@ -14685,6 +14694,8 @@ rec { { name = "serde_with"; packageId = "serde_with"; + usesDefaultFeatures = false; + features = [ "macros" ]; } { name = "sha1"; diff --git a/snix/Cargo.toml b/snix/Cargo.toml index 3363387cd..af57b7fb5 100644 --- a/snix/Cargo.toml +++ b/snix/Cargo.toml @@ -117,7 +117,9 @@ serde = "1.0.209" serde_json = "1.0" serde_qs = "0.12.0" serde_tagged = "0.3.0" -serde_with = "3.9.0" +serde_with = { version = "3.9.0", default-features = false, features = [ + "macros", +] } sha1 = "0.10.6" sha2 = "0.10.8" smol_str = "0.2.2" diff --git a/snix/nix-compat-derive-tests/tests/ui/deserialize_from_str_missing.stderr b/snix/nix-compat-derive-tests/tests/ui/deserialize_from_str_missing.stderr index 3cf99da5c..2bfc39cb8 100644 --- a/snix/nix-compat-derive-tests/tests/ui/deserialize_from_str_missing.stderr +++ b/snix/nix-compat-derive-tests/tests/ui/deserialize_from_str_missing.stderr @@ -7,10 +7,10 @@ error[E0277]: the trait bound `Test: FromStr` is not satisfied = help: the following other types implement trait `FromStr`: ByteString CString + HashAlgo IpAddr Ipv4Addr Ipv6Addr NonZero NonZero - NonZero and $N others diff --git a/snix/nix-compat/Cargo.toml b/snix/nix-compat/Cargo.toml index 56c001a36..f841b76da 100644 --- a/snix/nix-compat/Cargo.toml +++ b/snix/nix-compat/Cargo.toml @@ -16,7 +16,7 @@ flakeref = ["url"] # nix-daemon protocol handling daemon = ["tokio", "nix-compat-derive", "futures"] # serde support on types -serde = ["dep:serde", "dep:serde_json"] +serde = ["dep:serde", "dep:serde_json", "dep:serde_with"] test = [] # Enable all features by default. @@ -36,6 +36,7 @@ nom.workspace = true num-traits.workspace = true serde = { workspace = true, features = ["derive"], optional = true } serde_json = { workspace = true, optional = true } +serde_with = { workspace = true, optional = true } sha2.workspace = true thiserror.workspace = true tracing.workspace = true diff --git a/snix/nix-compat/src/nixhash/algos.rs b/snix/nix-compat/src/nixhash/algos.rs index 435002f01..d53aa8b4e 100644 --- a/snix/nix-compat/src/nixhash/algos.rs +++ b/snix/nix-compat/src/nixhash/algos.rs @@ -1,12 +1,14 @@ use std::fmt::Display; +use std::str::FromStr; #[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; +use serde_with::{DeserializeFromStr, SerializeDisplay}; use crate::nixhash::Error; /// This are the hash algorithms supported by cppnix. #[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[cfg_attr(feature = "serde", derive(DeserializeFromStr, SerializeDisplay))] pub enum HashAlgo { Md5, Sha1, @@ -37,37 +39,16 @@ impl Display for HashAlgo { } } -#[cfg(feature = "serde")] -impl Serialize for HashAlgo { - fn serialize(&self, serializer: S) -> Result - where - S: serde::Serializer, - { - serializer.collect_str(&self) - } -} - -#[cfg(feature = "serde")] -impl<'de> Deserialize<'de> for HashAlgo { - fn deserialize(deserializer: D) -> Result - where - D: serde::Deserializer<'de>, - { - let s: &str = Deserialize::deserialize(deserializer)?; - HashAlgo::try_from(s).map_err(serde::de::Error::custom) - } -} - /// TODO(Raito): this could be automated via macros, I suppose. /// But this may be more expensive than just doing it by hand /// and ensuring that is kept in sync. #[cfg(feature = "serde")] pub const SUPPORTED_ALGOS: [&str; 4] = ["md5", "sha1", "sha256", "sha512"]; -impl TryFrom<&str> for HashAlgo { - type Error = Error; +impl FromStr for HashAlgo { + type Err = Error; - fn try_from(algo_str: &str) -> Result { + fn from_str(algo_str: &str) -> Result { match algo_str { "md5" => Ok(Self::Md5), "sha1" => Ok(Self::Sha1), @@ -77,3 +58,10 @@ impl TryFrom<&str> for HashAlgo { } } } + +impl TryFrom<&str> for HashAlgo { + type Error = Error; + fn try_from(algo_str: &str) -> Result { + algo_str.parse() + } +}