job_adapter.rs (1967B)
1 #![forbid(unsafe_code)] 2 3 use crate::types::{RadrootsNostrEvent, RadrootsNostrKind}; 4 use radroots_events::kinds::KIND_PROFILE; 5 use radroots_events_codec::job::traits::{JobEventBorrow, JobEventLike}; 6 7 #[derive(Clone, Debug)] 8 pub struct RadrootsNostrEventAdapter<'a> { 9 evt: &'a RadrootsNostrEvent, 10 id_hex: String, 11 author_hex: String, 12 } 13 14 impl<'a> RadrootsNostrEventAdapter<'a> { 15 #[inline] 16 pub fn new(evt: &'a RadrootsNostrEvent) -> Self { 17 Self { 18 evt, 19 id_hex: evt.id.to_hex(), 20 author_hex: evt.pubkey.to_string(), 21 } 22 } 23 24 #[inline] 25 fn tags_as_slices(&self) -> Vec<Vec<String>> { 26 self.evt 27 .tags 28 .iter() 29 .map(|t| t.as_slice().to_vec()) 30 .collect() 31 } 32 } 33 34 impl<'a> JobEventBorrow<'a> for RadrootsNostrEventAdapter<'a> { 35 #[inline] 36 fn raw_id(&'a self) -> &'a str { 37 &self.id_hex 38 } 39 #[inline] 40 fn raw_author(&'a self) -> &'a str { 41 &self.author_hex 42 } 43 #[inline] 44 fn raw_content(&'a self) -> &'a str { 45 &self.evt.content 46 } 47 #[inline] 48 fn raw_kind(&'a self) -> u32 { 49 match self.evt.kind { 50 RadrootsNostrKind::Custom(v) => v as u32, 51 _ => KIND_PROFILE, 52 } 53 } 54 } 55 56 impl JobEventLike for RadrootsNostrEventAdapter<'_> { 57 fn raw_id(&self) -> String { 58 self.id_hex.clone() 59 } 60 fn raw_author(&self) -> String { 61 self.author_hex.clone() 62 } 63 fn raw_published_at(&self) -> u32 { 64 self.evt.created_at.as_secs() as u32 65 } 66 fn raw_kind(&self) -> u32 { 67 match self.evt.kind { 68 RadrootsNostrKind::Custom(v) => v as u32, 69 _ => KIND_PROFILE, 70 } 71 } 72 fn raw_content(&self) -> String { 73 self.evt.content.clone() 74 } 75 fn raw_tags(&self) -> Vec<Vec<String>> { 76 self.tags_as_slices() 77 } 78 fn raw_sig(&self) -> String { 79 self.evt.sig.to_string() 80 } 81 }