소스 검색

Add AsRef<[u8]>

Cesar Rodas 1 년 전
부모
커밋
737fc1cd91
4개의 변경된 파일38개의 추가작업 그리고 3개의 파일을 삭제
  1. 14 1
      crates/types/src/types/addr.rs
  2. 12 2
      crates/types/src/types/event.rs
  3. 6 0
      crates/types/src/types/id.rs
  4. 6 0
      crates/types/src/types/signature.rs

+ 14 - 1
crates/types/src/types/addr.rs

@@ -4,7 +4,7 @@
 //!
 //! It can also parse the bech32 version of the public key if provided (used by
 //! clients mostly)
-use std::hash::Hash;
+use std::{hash::Hash, ops::Deref};
 
 use bech32::{self, FromBase32, ToBase32, Variant};
 use serde::{
@@ -68,6 +68,19 @@ pub struct Addr {
     pub hrp: Option<HumanReadablePart>,
 }
 
+impl AsRef<[u8]> for Addr {
+    fn as_ref(&self) -> &[u8] {
+        &self.bytes
+    }
+}
+
+impl Deref for Addr {
+    type Target = [u8];
+    fn deref(&self) -> &[u8] {
+        &self.bytes
+    }
+}
+
 impl Hash for Addr {
     fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
         self.bytes.hash(state)

+ 12 - 2
crates/types/src/types/event.rs

@@ -42,7 +42,7 @@ pub enum Error {
 ///
 /// The event must be signed, in the crate::types::Event, which wraps this
 /// struct, but all the content of the event is defined here, but the signature.
-#[derive(Debug, Clone, Serialize)]
+#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
 pub struct UnsignedEvent {
     /// The public key that signs this event
     #[serde(rename = "pubkey")]
@@ -137,7 +137,7 @@ impl UnsignedEvent {
 /// This is a wrap for UnsignedEvent but it will serializes the ID and the signature.
 ///
 /// This is the struct that is published to relayers from clients
-#[derive(Debug, Clone, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct Event {
     /// Event Id
     ///
@@ -170,6 +170,11 @@ impl Event {
         })
     }
 
+    /// Returns the event author
+    pub fn author(&self) -> &Id {
+        &self.inner.public_key
+    }
+
     /// Checks if the event is valid
     pub fn is_valid(&self) -> Result<(), Error> {
         let calculated_id = self.inner.id()?;
@@ -179,6 +184,11 @@ impl Event {
         Self::verify_signature(&self.inner.public_key, &self.signature, &self.id)
     }
 
+    /// Returns list of tags for this event
+    pub fn tags(&self) -> &[super::Tag] {
+        &self.inner.tags
+    }
+
     /// Returns a reference to the inner content (the parsed content)
     pub fn content(&self) -> &Content {
         &self.inner.content

+ 6 - 0
crates/types/src/types/id.rs

@@ -12,6 +12,12 @@ use std::ops::Deref;
 #[derive(Debug, Clone, Hash, Eq, PartialEq)]
 pub struct Id(pub [u8; 32]);
 
+impl AsRef<[u8]> for Id {
+    fn as_ref(&self) -> &[u8] {
+        &self.0
+    }
+}
+
 impl Deref for Id {
     type Target = [u8; 32];
     fn deref(&self) -> &Self::Target {

+ 6 - 0
crates/types/src/types/signature.rs

@@ -24,6 +24,12 @@ pub enum Error {
 #[derive(Debug, Clone, PartialEq, Eq)]
 pub struct Signature(pub [u8; 64]);
 
+impl AsRef<[u8]> for Signature {
+    fn as_ref(&self) -> &[u8] {
+        &self.0
+    }
+}
+
 impl Deref for Signature {
     type Target = [u8; 64];
     fn deref(&self) -> &Self::Target {