lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

job_adapter.rs (2105B)


      1 //! Nostr conversion helpers for legacy job event payloads.
      2 //!
      3 //! This module only maps supplied values; it does not schedule work, contact a
      4 //! relay, or persist job state.
      5 
      6 #![forbid(unsafe_code)]
      7 
      8 use crate::types::{RadrootsNostrEvent, RadrootsNostrKind};
      9 use radroots_event::envelope::kind::KIND_PROFILE;
     10 use radroots_event_codec::decode::job::{JobEventBorrow, JobEventLike};
     11 
     12 #[derive(Clone, Debug)]
     13 pub struct EventAdapter<'a> {
     14     evt: &'a RadrootsNostrEvent,
     15     id_hex: String,
     16     author_hex: String,
     17 }
     18 
     19 impl<'a> EventAdapter<'a> {
     20     #[inline]
     21     pub fn new(evt: &'a RadrootsNostrEvent) -> Self {
     22         Self {
     23             evt,
     24             id_hex: evt.id.to_hex(),
     25             author_hex: evt.pubkey.to_string(),
     26         }
     27     }
     28 
     29     #[inline]
     30     fn tags_as_slices(&self) -> Vec<Vec<String>> {
     31         self.evt
     32             .tags
     33             .iter()
     34             .map(|t| t.as_slice().to_vec())
     35             .collect()
     36     }
     37 }
     38 
     39 impl<'a> JobEventBorrow<'a> for EventAdapter<'a> {
     40     #[inline]
     41     fn raw_id(&'a self) -> String {
     42         self.id_hex.clone()
     43     }
     44     #[inline]
     45     fn raw_author(&'a self) -> String {
     46         self.author_hex.clone()
     47     }
     48     #[inline]
     49     fn raw_content(&'a self) -> &'a str {
     50         &self.evt.content
     51     }
     52     #[inline]
     53     fn raw_kind(&'a self) -> u32 {
     54         match self.evt.kind {
     55             RadrootsNostrKind::Custom(v) => v as u32,
     56             _ => KIND_PROFILE,
     57         }
     58     }
     59 }
     60 
     61 impl JobEventLike for EventAdapter<'_> {
     62     fn raw_id(&self) -> String {
     63         self.id_hex.clone()
     64     }
     65     fn raw_author(&self) -> String {
     66         self.author_hex.clone()
     67     }
     68     fn raw_published_at(&self) -> u64 {
     69         self.evt.created_at.as_secs()
     70     }
     71     fn raw_kind(&self) -> u32 {
     72         match self.evt.kind {
     73             RadrootsNostrKind::Custom(v) => v as u32,
     74             _ => KIND_PROFILE,
     75         }
     76     }
     77     fn raw_content(&self) -> String {
     78         self.evt.content.clone()
     79     }
     80     fn raw_tags(&self) -> Vec<Vec<String>> {
     81         self.tags_as_slices()
     82     }
     83     fn raw_sig(&self) -> String {
     84         self.evt.sig.to_string()
     85     }
     86 }