chore: Move the naddr parsers to parsers.rs
Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
@@ -17,10 +17,9 @@
|
|||||||
use std::{collections::HashSet, fs, path::PathBuf};
|
use std::{collections::HashSet, fs, path::PathBuf};
|
||||||
|
|
||||||
use nostr::{
|
use nostr::{
|
||||||
nips::nip19::{FromBech32, Nip19Coordinate, ToBech32},
|
nips::nip19::Nip19Coordinate,
|
||||||
types::RelayUrl,
|
types::RelayUrl,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize, Serializer};
|
|
||||||
|
|
||||||
use crate::error::{N34Error, N34Result};
|
use crate::error::{N34Error, N34Result};
|
||||||
|
|
||||||
@@ -74,8 +73,8 @@ pub struct RepoRelaySet {
|
|||||||
#[serde(
|
#[serde(
|
||||||
default,
|
default,
|
||||||
skip_serializing_if = "HashSet::is_empty",
|
skip_serializing_if = "HashSet::is_empty",
|
||||||
serialize_with = "ser_naddrs",
|
serialize_with = "super::parsers::ser_naddrs",
|
||||||
deserialize_with = "de_naddrs"
|
deserialize_with = "super::parsers::de_naddrs"
|
||||||
)]
|
)]
|
||||||
pub naddrs: HashSet<Nip19Coordinate>,
|
pub naddrs: HashSet<Nip19Coordinate>,
|
||||||
/// Relay URLs in this group.
|
/// Relay URLs in this group.
|
||||||
@@ -288,27 +287,3 @@ impl &[RepoRelaySet] {
|
|||||||
fn duplicate_in_sorted<T: PartialEq + Clone>(items: &[T]) -> Option<&T> {
|
fn duplicate_in_sorted<T: PartialEq + Clone>(items: &[T]) -> Option<&T> {
|
||||||
items.windows(2).find(|w| w[0] == w[1]).map(|w| &w[0])
|
items.windows(2).find(|w| w[0] == w[1]).map(|w| &w[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ser_naddrs<S>(naddr: &HashSet<Nip19Coordinate>, serializer: S) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
|
||||||
S: Serializer,
|
|
||||||
{
|
|
||||||
let str_naddrs = naddr
|
|
||||||
.iter()
|
|
||||||
.map(|n| n.to_bech32().map_err(|err| err.to_string()))
|
|
||||||
.collect::<Result<Vec<_>, _>>()
|
|
||||||
.map_err(serde::ser::Error::custom)?;
|
|
||||||
|
|
||||||
str_naddrs.serialize(serializer)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn de_naddrs<'de, D>(deserializer: D) -> Result<HashSet<Nip19Coordinate>, D::Error>
|
|
||||||
where
|
|
||||||
D: serde::Deserializer<'de>,
|
|
||||||
{
|
|
||||||
Vec::<String>::deserialize(deserializer)?
|
|
||||||
.into_iter()
|
|
||||||
.map(|naddr| Nip19Coordinate::from_bech32(&naddr))
|
|
||||||
.collect::<Result<HashSet<_>, _>>()
|
|
||||||
.map_err(serde::de::Error::custom)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -15,14 +15,16 @@
|
|||||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashSet,
|
||||||
fs,
|
fs,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use nostr::{
|
use nostr::{
|
||||||
Kind,
|
Kind,
|
||||||
nips::nip19::{FromBech32, Nip19Coordinate},
|
nips::nip19::{FromBech32, Nip19Coordinate, ToBech32},
|
||||||
};
|
};
|
||||||
|
use serde::{Deserialize, Serialize, Serializer};
|
||||||
|
|
||||||
use super::CliConfig;
|
use super::CliConfig;
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -69,3 +71,29 @@ pub fn parse_config_path(config_path: &str) -> N34Result<CliConfig> {
|
|||||||
|
|
||||||
CliConfig::load(path)
|
CliConfig::load(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Serializes a set of NIP-19 coordinates as a list of bech32 strings.
|
||||||
|
pub fn ser_naddrs<S>(naddr: &HashSet<Nip19Coordinate>, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
|
where
|
||||||
|
S: Serializer,
|
||||||
|
{
|
||||||
|
let str_naddrs = naddr
|
||||||
|
.iter()
|
||||||
|
.map(|n| n.to_bech32().map_err(|err| err.to_string()))
|
||||||
|
.collect::<Result<Vec<_>, _>>()
|
||||||
|
.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<HashSet<Nip19Coordinate>, D::Error>
|
||||||
|
where
|
||||||
|
D: serde::Deserializer<'de>,
|
||||||
|
{
|
||||||
|
Vec::<String>::deserialize(deserializer)?
|
||||||
|
.into_iter()
|
||||||
|
.map(|naddr| Nip19Coordinate::from_bech32(&naddr))
|
||||||
|
.collect::<Result<HashSet<_>, _>>()
|
||||||
|
.map_err(serde::de::Error::custom)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user