lib

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

job_adapter.rs (1868B)


      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;
      9 use radroots_event_codec::decode::job::{JobEventBorrow, JobEventLike};
     10 
     11 #[derive(Clone, Debug)]
     12 pub struct EventAdapter<'a> {
     13     evt: &'a RadrootsNostrEvent,
     14     id_hex: String,
     15     author_hex: String,
     16 }
     17 
     18 impl<'a> EventAdapter<'a> {
     19     #[inline]
     20     pub fn new(evt: &'a RadrootsNostrEvent) -> Self {
     21         Self {
     22             evt,
     23             id_hex: evt.id.to_hex(),
     24             author_hex: evt.pubkey.to_string(),
     25         }
     26     }
     27 
     28     #[inline]
     29     fn tags_as_slices(&self) -> Vec<Vec<String>> {
     30         self.evt
     31             .tags
     32             .iter()
     33             .map(|t| t.as_slice().to_vec())
     34             .collect()
     35     }
     36 }
     37 
     38 impl<'a> JobEventBorrow<'a> for EventAdapter<'a> {
     39     #[inline]
     40     fn raw_id(&'a self) -> String {
     41         self.id_hex.clone()
     42     }
     43     #[inline]
     44     fn raw_author(&'a self) -> String {
     45         self.author_hex.clone()
     46     }
     47     #[inline]
     48     fn raw_content(&'a self) -> &'a str {
     49         &self.evt.content
     50     }
     51     #[inline]
     52     fn raw_kind(&'a self) -> u32 {
     53         u32::from(self.evt.kind.as_u16())
     54     }
     55 }
     56 
     57 impl JobEventLike for EventAdapter<'_> {
     58     fn raw_id(&self) -> String {
     59         self.id_hex.clone()
     60     }
     61     fn raw_author(&self) -> String {
     62         self.author_hex.clone()
     63     }
     64     fn raw_published_at(&self) -> u64 {
     65         self.evt.created_at.as_secs()
     66     }
     67     fn raw_kind(&self) -> u32 {
     68         u32::from(self.evt.kind.as_u16())
     69     }
     70     fn raw_content(&self) -> String {
     71         self.evt.content.clone()
     72     }
     73     fn raw_tags(&self) -> Vec<Vec<String>> {
     74         self.tags_as_slices()
     75     }
     76     fn raw_sig(&self) -> String {
     77         self.evt.sig.to_string()
     78     }
     79 }