lib

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

codec_adapters.rs (2961B)


      1 //! Compatibility adapters between upstream Nostr values and event-codec data.
      2 //!
      3 //! The module contains pure value transformations and performs no storage or
      4 //! network operations.
      5 
      6 extern crate alloc;
      7 use alloc::{string::String, vec::Vec};
      8 
      9 use crate::types::RadrootsNostrEvent;
     10 
     11 use radroots_event::{
     12     social::job_feedback::JobFeedback, social::job_request::JobRequest,
     13     social::job_result::JobResult,
     14 };
     15 use radroots_event_codec::decode::job::{
     16     JobParseError, feedback as fb_decode, request as req_decode, result as res_decode,
     17 };
     18 use radroots_event_codec::decode::{RadrootsParsedData, RadrootsParsedEvent};
     19 
     20 fn event_id(e: &RadrootsNostrEvent) -> String {
     21     e.id.to_hex()
     22 }
     23 
     24 fn author(e: &RadrootsNostrEvent) -> String {
     25     e.pubkey.to_hex()
     26 }
     27 
     28 fn published_at(e: &RadrootsNostrEvent) -> u64 {
     29     e.created_at.as_secs()
     30 }
     31 
     32 fn kind_u32(e: &RadrootsNostrEvent) -> u32 {
     33     e.kind.as_u16() as u32
     34 }
     35 
     36 fn content(e: &RadrootsNostrEvent) -> String {
     37     e.content.clone()
     38 }
     39 
     40 fn tags_vec(e: &RadrootsNostrEvent) -> Vec<Vec<String>> {
     41     e.tags.iter().map(|t| t.as_slice().to_vec()).collect()
     42 }
     43 
     44 fn sig_hex(e: &RadrootsNostrEvent) -> String {
     45     e.sig.to_string()
     46 }
     47 
     48 pub fn to_job_request_metadata(
     49     e: &RadrootsNostrEvent,
     50 ) -> Result<RadrootsParsedData<JobRequest>, JobParseError> {
     51     req_decode::data_from_event(
     52         event_id(e),
     53         author(e),
     54         published_at(e),
     55         kind_u32(e),
     56         tags_vec(e),
     57     )
     58 }
     59 
     60 pub fn to_job_result_metadata(
     61     e: &RadrootsNostrEvent,
     62 ) -> Result<RadrootsParsedData<JobResult>, JobParseError> {
     63     res_decode::data_from_event(
     64         event_id(e),
     65         author(e),
     66         published_at(e),
     67         kind_u32(e),
     68         content(e),
     69         tags_vec(e),
     70     )
     71 }
     72 
     73 pub fn to_job_feedback_metadata(
     74     e: &RadrootsNostrEvent,
     75 ) -> Result<RadrootsParsedData<JobFeedback>, JobParseError> {
     76     fb_decode::data_from_event(
     77         event_id(e),
     78         author(e),
     79         published_at(e),
     80         kind_u32(e),
     81         content(e),
     82         tags_vec(e),
     83     )
     84 }
     85 
     86 pub fn to_job_request_index(
     87     e: &RadrootsNostrEvent,
     88 ) -> Result<RadrootsParsedEvent<JobRequest>, JobParseError> {
     89     req_decode::parsed_from_event(
     90         event_id(e),
     91         author(e),
     92         published_at(e),
     93         kind_u32(e),
     94         content(e),
     95         tags_vec(e),
     96         sig_hex(e),
     97     )
     98 }
     99 
    100 pub fn to_job_result_index(
    101     e: &RadrootsNostrEvent,
    102 ) -> Result<RadrootsParsedEvent<JobResult>, JobParseError> {
    103     res_decode::parsed_from_event(
    104         event_id(e),
    105         author(e),
    106         published_at(e),
    107         kind_u32(e),
    108         content(e),
    109         tags_vec(e),
    110         sig_hex(e),
    111     )
    112 }
    113 
    114 pub fn to_job_feedback_index(
    115     e: &RadrootsNostrEvent,
    116 ) -> Result<RadrootsParsedEvent<JobFeedback>, JobParseError> {
    117     fb_decode::parsed_from_event(
    118         event_id(e),
    119         author(e),
    120         published_at(e),
    121         kind_u32(e),
    122         content(e),
    123         tags_vec(e),
    124         sig_hex(e),
    125     )
    126 }