From 55f9b9194ebdbf295e32119b0987a736107cfd5e Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Thu, 22 May 2025 20:45:01 +0000
Subject: [PATCH] chore: Utils to work with the `nostr-address` file
Signed-off-by: Awiteb
---
src/nostr_utils/utils.rs | 39 +++++++++++++++++++++++++++++++++++++--
1 file changed, 37 insertions(+), 2 deletions(-)
diff --git a/src/nostr_utils/utils.rs b/src/nostr_utils/utils.rs
index 6158fe1..9683a4d 100644
--- a/src/nostr_utils/utils.rs
+++ b/src/nostr_utils/utils.rs
@@ -14,7 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
-use std::{fmt, fs, str::FromStr, sync::atomic::Ordering};
+use std::{
+ fmt,
+ fs,
+ path::{Path, PathBuf},
+ str::FromStr,
+ sync::atomic::Ordering,
+};
use nostr::{
event::{Event, EventId, Kind, TagKind, TagStandard},
@@ -30,7 +36,10 @@ use nostr::{
};
use super::traits::TagsExt;
-use crate::error::{N34Error, N34Result};
+use crate::{
+ cli::{NOSTR_ADDRESS_FILE, parsers},
+ error::{N34Error, N34Result},
+};
/// Returns the value of the given tag
fn tag_value(tag: &TagStandard) -> String {
@@ -195,3 +204,29 @@ where
}
read_editor(file_suffix)
}
+
+/// Path to the `nostr-address` file in current directory.
+pub fn nostr_address_path() -> std::io::Result {
+ std::env::current_dir().map(|p| p.join(NOSTR_ADDRESS_FILE))
+}
+
+/// If the given coordinate is Some, return it. Otherwise, try to read and parse
+/// the first non-comment line from the `nostr-address` file.
+pub fn naddr_or_file(
+ naddr: Option,
+ address_file_path: &Path,
+) -> N34Result {
+ if let Some(naddr) = naddr {
+ return Ok(naddr);
+ }
+
+ parsers::repo_naddr(
+ fs::read_to_string(address_file_path)
+ .map_err(N34Error::CanNotReadNostrAddressFile)?
+ .split("\n")
+ .find(|line| !line.starts_with("#") && !line.trim().is_empty())
+ .ok_or(N34Error::EmptyNostrAddressFile)?
+ .trim(),
+ )
+ .map_err(N34Error::InvalidNostrAddressFileContent)
+}