From 353358f0ed776684ea2d77d2f53a2209d637cdaf Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Mon, 7 Jul 2025 12:32:39 +0000
Subject: [PATCH] chore: Move the naddr parsers to `parsers.rs`
Signed-off-by: Awiteb
---
src/cli/config.rs | 31 +++----------------------------
src/cli/parsers.rs | 30 +++++++++++++++++++++++++++++-
2 files changed, 32 insertions(+), 29 deletions(-)
diff --git a/src/cli/config.rs b/src/cli/config.rs
index 64b4341..a053936 100644
--- a/src/cli/config.rs
+++ b/src/cli/config.rs
@@ -17,10 +17,9 @@
use std::{collections::HashSet, fs, path::PathBuf};
use nostr::{
- nips::nip19::{FromBech32, Nip19Coordinate, ToBech32},
+ nips::nip19::Nip19Coordinate,
types::RelayUrl,
};
-use serde::{Deserialize, Serialize, Serializer};
use crate::error::{N34Error, N34Result};
@@ -74,8 +73,8 @@ pub struct RepoRelaySet {
#[serde(
default,
skip_serializing_if = "HashSet::is_empty",
- serialize_with = "ser_naddrs",
- deserialize_with = "de_naddrs"
+ serialize_with = "super::parsers::ser_naddrs",
+ deserialize_with = "super::parsers::de_naddrs"
)]
pub naddrs: HashSet,
/// Relay URLs in this group.
@@ -288,27 +287,3 @@ impl &[RepoRelaySet] {
fn duplicate_in_sorted(items: &[T]) -> Option<&T> {
items.windows(2).find(|w| w[0] == w[1]).map(|w| &w[0])
}
-
-fn ser_naddrs(naddr: &HashSet, serializer: S) -> Result
-where
- S: Serializer,
-{
- let str_naddrs = naddr
- .iter()
- .map(|n| n.to_bech32().map_err(|err| err.to_string()))
- .collect::, _>>()
- .map_err(serde::ser::Error::custom)?;
-
- str_naddrs.serialize(serializer)
-}
-
-fn de_naddrs<'de, D>(deserializer: D) -> Result, D::Error>
-where
- D: serde::Deserializer<'de>,
-{
- Vec::::deserialize(deserializer)?
- .into_iter()
- .map(|naddr| Nip19Coordinate::from_bech32(&naddr))
- .collect::, _>>()
- .map_err(serde::de::Error::custom)
-}
diff --git a/src/cli/parsers.rs b/src/cli/parsers.rs
index c7f482e..e392299 100644
--- a/src/cli/parsers.rs
+++ b/src/cli/parsers.rs
@@ -15,14 +15,16 @@
// along with this program. If not, see .
use std::{
+ collections::HashSet,
fs,
path::{Path, PathBuf},
};
use nostr::{
Kind,
- nips::nip19::{FromBech32, Nip19Coordinate},
+ nips::nip19::{FromBech32, Nip19Coordinate, ToBech32},
};
+use serde::{Deserialize, Serialize, Serializer};
use super::CliConfig;
use crate::{
@@ -69,3 +71,29 @@ pub fn parse_config_path(config_path: &str) -> N34Result {
CliConfig::load(path)
}
+
+/// Serializes a set of NIP-19 coordinates as a list of bech32 strings.
+pub fn ser_naddrs(naddr: &HashSet, serializer: S) -> Result
+where
+ S: Serializer,
+{
+ let str_naddrs = naddr
+ .iter()
+ .map(|n| n.to_bech32().map_err(|err| err.to_string()))
+ .collect::, _>>()
+ .map_err(serde::ser::Error::custom)?;
+
+ str_naddrs.serialize(serializer)
+}
+
+/// Deserializes a list of bech32 strings into a set of NIP-19 coordinates.
+pub fn de_naddrs<'de, D>(deserializer: D) -> Result, D::Error>
+where
+ D: serde::Deserializer<'de>,
+{
+ Vec::::deserialize(deserializer)?
+ .into_iter()
+ .map(|naddr| Nip19Coordinate::from_bech32(&naddr))
+ .collect::, _>>()
+ .map_err(serde::de::Error::custom)
+}