Skip to content

Commit

Permalink
nostr: change Tag::parse arg from slice to iterator
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
  • Loading branch information
yukibtc committed Nov 20, 2024
1 parent 5e0896f commit 7e49fe0
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* nostr: remove `tags` arg from `EventBuilder::job_request` ([Yuki Kishimoto])
* nostr: disable all default features except `std` ([Yuki Kishimoto])
* nostr: change `Timestamp::to_human_datetime` fingerprint ([Yuki Kishimoto])
* nostr: change `Tag::parse` arg from slice to iterator ([Yuki Kishimoto])
* pool: switch from async to sync message sending for `Relay` ([Yuki Kishimoto])
* sdk: disable all default features ([Yuki Kishimoto])
* sdk: set `Client::from_builder` as private ([Yuki Kishimoto])
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-ffi/src/protocol/event/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Tag {
/// Return error if the tag is empty!
#[inline]
#[uniffi::constructor]
pub fn parse(data: &[String]) -> Result<Self> {
pub fn parse(data: Vec<String>) -> Result<Self> {
Ok(Self {
inner: tag::Tag::parse(data)?,
})
Expand Down
2 changes: 1 addition & 1 deletion bindings/nostr-sdk-js/src/protocol/event/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl JsTag {
#[wasm_bindgen]
pub fn parse(tag: Vec<String>) -> Result<JsTag> {
Ok(Self {
inner: Tag::parse(&tag).map_err(into_err)?,
inner: Tag::parse(tag).map_err(into_err)?,
})
}

Expand Down
5 changes: 1 addition & 4 deletions crates/nostr-database/src/flatbuffers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ impl FlatBufferDecode for Event {
.tags()
.ok_or(Error::NotFound)?
.into_iter()
.filter_map(|tag| {
tag.data()
.map(|tag| Tag::parse(&tag.into_iter().collect::<Vec<&str>>()))
})
.filter_map(|tag| tag.data().map(Tag::parse))
.collect::<Result<Vec<Tag>, _>>()?;

Ok(Self::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/nostr-lmdb/src/store/lmdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl Lmdb {
}
}

// Reverse order, optionally apply limit
// Optionally apply limit
Ok(match limit {
Some(limit) => Box::new(output.into_iter().take(limit)),
None => Box::new(output.into_iter()),
Expand Down
5 changes: 1 addition & 4 deletions crates/nostr-lmdb/src/store/types/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ impl<'a> DatabaseEvent<'a> {
let tags: Vec<Tag> = self
.tags
.into_iter()
.filter_map(|tag| {
tag.data()
.map(|tag| Tag::parse(&tag.into_iter().collect::<Vec<&str>>()).ok())
})
.filter_map(|tag| tag.data().map(Tag::parse))
.flatten()
.collect();
let sig = Signature::from_slice(&self.sig.0)?;
Expand Down
35 changes: 20 additions & 15 deletions crates/nostr/src/event/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,21 @@ impl Tag {
/// Parse tag
///
/// Return error if the tag is empty!
pub fn parse<S>(tag: &[S]) -> Result<Self, Error>
pub fn parse<I, S>(tag: I) -> Result<Self, Error>
where
S: AsRef<str>,
I: IntoIterator<Item = S>,
S: Into<String>,
{
// Collect
let tag: Vec<String> = tag.into_iter().map(|v| v.into()).collect();

// Check if it's empty
if tag.is_empty() {
return Err(Error::EmptyTag);
}

// NOT USE `Self::new`!
Ok(Self::new_with_empty_cell(
tag.iter().map(|v| v.as_ref().to_string()).collect(),
))
// Construct without cell
Ok(Self::new_with_empty_cell(tag))
}

/// Construct from standardized tag
Expand Down Expand Up @@ -409,18 +411,21 @@ mod tests {

#[test]
fn test_parse_empty_tag() {
assert_eq!(Tag::parse::<String>(&[]).unwrap_err(), Error::EmptyTag);
assert_eq!(
Tag::parse::<Vec<_>, String>(vec![]).unwrap_err(),
Error::EmptyTag
);
}

#[test]
fn test_tag_match_standardized() {
let tag: Tag = Tag::parse(&["d", "bravery"]).unwrap();
let tag: Tag = Tag::parse(["d", "bravery"]).unwrap();
assert_eq!(
tag.as_standardized(),
Some(&TagStandard::Identifier(String::from("bravery")))
);

let tag: Tag = Tag::parse(&["d", "test"]).unwrap();
let tag: Tag = Tag::parse(["d", "test"]).unwrap();
assert_eq!(
tag.to_standardized(),
Some(TagStandard::Identifier(String::from("test")))
Expand All @@ -429,11 +434,11 @@ mod tests {

#[test]
fn test_extract_tag_content() {
let t: Tag = Tag::parse(&["aaaaaa", "bbbbbb"]).unwrap();
let t: Tag = Tag::parse(["aaaaaa", "bbbbbb"]).unwrap();
assert_eq!(t.content(), Some("bbbbbb"));

// Test extract public key
let t: Tag = Tag::parse(&[
let t: Tag = Tag::parse([
"custom-p",
"f86c44a2de95d9149b51c6a29afeabba264c18e2fa7c49de93424a0c56947785",
])
Expand All @@ -444,7 +449,7 @@ mod tests {
);

// Test extract event ID
let t: Tag = Tag::parse(&[
let t: Tag = Tag::parse([
"custom-e",
"2be17aa3031bdcb006f0fce80c146dea9c1c0268b0af2398bb673365c6444d45",
])
Expand Down Expand Up @@ -506,7 +511,7 @@ mod tests {
);

assert_eq!(
Tag::parse(&["r", "wss://atlas.nostr.land", ""]).unwrap(),
Tag::parse(["r", "wss://atlas.nostr.land", ""]).unwrap(),
Tag::custom(
TagKind::SingleLetter(SingleLetterTag::lowercase(Alphabet::R)),
["wss://atlas.nostr.land", ""]
Expand All @@ -530,7 +535,7 @@ mod tests {
);

assert_eq!(
Tag::parse(&[
Tag::parse([
"r",
"3dbee968d1ddcdf07521e246e405e1fbb549080f1f4ef4e42526c4528f124220",
""
Expand All @@ -551,7 +556,7 @@ mod tests {
);

assert_eq!(
Tag::parse(&["client", "nostr-sdk"]).unwrap(),
Tag::parse(["client", "nostr-sdk"]).unwrap(),
Tag::custom(TagKind::Client, ["nostr-sdk"])
);
}
Expand Down

0 comments on commit 7e49fe0

Please sign in to comment.