feat: Support relays and naddrs sets
This commit introduces support for 'sets', a feature that simplifies referencing multiple relays or naddrs with a single word. Sets are stored in the config directory under `n34` as `config.toml` in `TOML` format. The config path follows the `$XDG_CONFIG_HOME` standard, defaulting to `$HOME/.config` if unset. The `n34 sets` command allows managing sets, adding, removing, updating, or displaying them. When using the main `--relays` flag, a set name can be provided to automatically expand its relays (an error occurs if the set contains no relays). Similarly, any command accepting naddrs can reference a set to extract its naddrs. Sets can also be merged by updating a set while specifying another set in `--repo` merges their naddrs, while `--set-relays` merges their relays. Removal follows the same pattern. Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
194
src/cli/types.rs
Normal file
194
src/cli/types.rs
Normal file
@@ -0,0 +1,194 @@
|
||||
// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
|
||||
// Copyright (C) 2025 Awiteb <a@4rs.nl>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <https://gnu.org/licenses/gpl-3.0.html>.
|
||||
|
||||
use std::str::FromStr;
|
||||
|
||||
use nostr::{
|
||||
event::Kind,
|
||||
nips::{self, nip01::Coordinate, nip19::Nip19Coordinate},
|
||||
types::RelayUrl,
|
||||
};
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
use super::{RepoRelaySetsExt, parsers};
|
||||
use crate::{
|
||||
cli::RepoRelaySet,
|
||||
error::{N34Error, N34Result},
|
||||
};
|
||||
|
||||
/// Either a NIP-19 coordinate (naddr) or a named set.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NaddrOrSet {
|
||||
/// NIP-19 coordinate.
|
||||
Naddr(Nip19Coordinate),
|
||||
/// Name of a set (may not exist).
|
||||
Set(String),
|
||||
}
|
||||
|
||||
/// Either relay URL or a named set.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RelayOrSet {
|
||||
/// Relay URL.
|
||||
Relay(RelayUrl),
|
||||
/// Name of a set (may not exist).
|
||||
Set(String),
|
||||
}
|
||||
|
||||
impl NaddrOrSet {
|
||||
/// Returns the naddr if `Naddr` or try to get the relays from the set.
|
||||
/// Returns error if the set naddrs are empty or the set not found.
|
||||
pub fn get_naddrs(self, sets: &[RepoRelaySet]) -> N34Result<Vec<Nip19Coordinate>> {
|
||||
match self {
|
||||
Self::Naddr(nip19_coordinate) => Ok(vec![nip19_coordinate]),
|
||||
Self::Set(name) => {
|
||||
let set = sets
|
||||
.get_set(&name)
|
||||
.map_err(|_| N34Error::InvalidNaddrArg(name.clone()))?;
|
||||
if set.naddrs.is_empty() {
|
||||
Err(N34Error::EmptySetNaddrs(name))
|
||||
} else {
|
||||
Ok(Vec::from_iter(set.naddrs.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl RelayOrSet {
|
||||
/// Returns the relay if `Relay` or try to get the relays from the set.
|
||||
/// Returns error if the set relays are empty or the set not found
|
||||
pub fn get_relays(self, sets: &[RepoRelaySet]) -> N34Result<Vec<RelayUrl>> {
|
||||
match self {
|
||||
Self::Relay(relay) => Ok(vec![relay]),
|
||||
Self::Set(name) => {
|
||||
let set = sets.get_set(&name)?;
|
||||
if set.relays.is_empty() {
|
||||
Err(N34Error::EmptySetRelays(name))
|
||||
} else {
|
||||
Ok(Vec::from_iter(set.relays.clone()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for NaddrOrSet {
|
||||
type Err = String;
|
||||
|
||||
/// Parses a Git repository address which can be either:
|
||||
/// - A bech32-encoded naddr (e.g. "naddr1...") for Git repository
|
||||
/// announcements (kind 30617)
|
||||
/// - A NIP-05 identifier with repository ID (e.g. "4rs.nl/n34" or
|
||||
/// "_@4rs.nl/n34")
|
||||
/// - A set name.
|
||||
///
|
||||
/// Returns an error for invalid formats, failed bech32 decoding, wrong
|
||||
/// event kind.
|
||||
fn from_str(naddr_or_set: &str) -> Result<Self, Self::Err> {
|
||||
let naddr_or_set = naddr_or_set.trim();
|
||||
|
||||
if naddr_or_set.contains("/") {
|
||||
let (nip5, repo_id) = naddr_or_set.split_once("/").expect("There is a `/`");
|
||||
parse_nip5_repo(nip5, repo_id)
|
||||
} else if naddr_or_set.starts_with("naddr1") || naddr_or_set.starts_with("nostr:naddr1") {
|
||||
parsers::parse_repo_naddr(naddr_or_set.trim_start_matches("nostr:")).map(Self::Naddr)
|
||||
} else {
|
||||
Ok(Self::Set(naddr_or_set.to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for RelayOrSet {
|
||||
type Err = String;
|
||||
|
||||
/// Parse a string into a relay URL or a set name.
|
||||
/// If the string is a valid URL (e.g., "wss://example.com"), it's treated
|
||||
/// as a relay URL. Otherwise, it's treated as a set name, and its
|
||||
/// associated relays will be merged.
|
||||
fn from_str(relay_or_set: &str) -> Result<Self, Self::Err> {
|
||||
let relay_or_set = relay_or_set.trim();
|
||||
|
||||
if relay_or_set.starts_with("wss://") {
|
||||
RelayUrl::from_str(relay_or_set)
|
||||
.map_err(|err| err.to_string())
|
||||
.map(Self::Relay)
|
||||
} else {
|
||||
Ok(Self::Set(relay_or_set.to_owned()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_nip5_repo(nip5: &str, repo_id: &str) -> Result<NaddrOrSet, String> {
|
||||
let (username, domain) = nip5.split_once("@").unwrap_or(("_", nip5));
|
||||
|
||||
let nip5_profile = tokio::task::block_in_place(|| {
|
||||
Handle::current().block_on(async {
|
||||
nips::nip05::profile(format!("{username}@{domain}"), None)
|
||||
.await
|
||||
.map_err(|err| err.to_string())
|
||||
})
|
||||
})?;
|
||||
|
||||
Ok(NaddrOrSet::Naddr(
|
||||
Nip19Coordinate::new(
|
||||
Coordinate::new(Kind::GitRepoAnnouncement, nip5_profile.public_key).identifier(repo_id),
|
||||
nip5_profile.relays,
|
||||
)
|
||||
.expect("The relays is `RelayUrl`"),
|
||||
))
|
||||
}
|
||||
|
||||
#[easy_ext::ext(NaddrOrSetVecExt)]
|
||||
impl Vec<NaddrOrSet> {
|
||||
/// Converts this vector of [`NaddrOrSet`] into a flat vector of
|
||||
/// [`Nip19Coordinates`] using the given sets.
|
||||
pub fn flat_naddrs(self, sets: &[RepoRelaySet]) -> N34Result<Vec<Nip19Coordinate>> {
|
||||
self.into_iter()
|
||||
.map(|n| n.get_naddrs(sets))
|
||||
.try_fold(Vec::new(), |mut acc, item| {
|
||||
acc.extend(item?);
|
||||
Ok(acc)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[easy_ext::ext(RelayOrSetVecExt)]
|
||||
impl Vec<RelayOrSet> {
|
||||
/// Converts this vector of [`RelayOrSet`] into a flat vector of
|
||||
/// [`RelayUrl`] using the given sets.
|
||||
pub fn flat_relays(self, sets: &[RepoRelaySet]) -> N34Result<Vec<RelayUrl>> {
|
||||
self.into_iter()
|
||||
.map(|n| n.get_relays(sets))
|
||||
.try_fold(Vec::new(), |mut acc, item| {
|
||||
acc.extend(item?);
|
||||
Ok(acc)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[easy_ext::ext(OptionNaddrOrSetVecExt)]
|
||||
impl Option<Vec<NaddrOrSet>> {
|
||||
/// Converts this vector of [`NaddrOrSet`] into a flat vector of
|
||||
/// [`Nip19Coordinates`] using the given sets.
|
||||
pub fn flat_naddrs(&self, sets: &[RepoRelaySet]) -> N34Result<Option<Vec<Nip19Coordinate>>> {
|
||||
// Clones self here to simplify command code
|
||||
self.clone()
|
||||
.map(|naddrs| naddrs.flat_naddrs(sets))
|
||||
.transpose()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user