lib

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

encode.rs (1486B)


      1 #[cfg(not(feature = "std"))]
      2 use alloc::{
      3     string::{String, ToString},
      4     vec::Vec,
      5 };
      6 
      7 use radroots_events::{
      8     http_auth::{KIND_HTTP_AUTH, RadrootsHttpAuth},
      9     tags::{TAG_METHOD, TAG_PAYLOAD, TAG_URL_AUTH},
     10 };
     11 
     12 use crate::error::EventEncodeError;
     13 use crate::field_helpers::{
     14     push_optional_tag, push_tag, validate_lowercase_hex_64, validate_non_empty_field,
     15 };
     16 use crate::wire::WireEventParts;
     17 
     18 pub fn http_auth_build_tags(auth: &RadrootsHttpAuth) -> Result<Vec<Vec<String>>, EventEncodeError> {
     19     validate_non_empty_field(&auth.url, "url")?;
     20     validate_non_empty_field(&auth.method, "method")?;
     21     if let Some(payload) = auth.payload_sha256.as_deref() {
     22         validate_lowercase_hex_64(payload, "payload_sha256")?;
     23     }
     24     let mut tags = Vec::new();
     25     push_tag(&mut tags, TAG_URL_AUTH, auth.url.as_str());
     26     push_tag(&mut tags, TAG_METHOD, auth.method.as_str());
     27     push_optional_tag(&mut tags, TAG_PAYLOAD, auth.payload_sha256.as_deref());
     28     Ok(tags)
     29 }
     30 
     31 pub fn to_wire_parts(auth: &RadrootsHttpAuth) -> Result<WireEventParts, EventEncodeError> {
     32     to_wire_parts_with_kind(auth, KIND_HTTP_AUTH)
     33 }
     34 
     35 pub fn to_wire_parts_with_kind(
     36     auth: &RadrootsHttpAuth,
     37     kind: u32,
     38 ) -> Result<WireEventParts, EventEncodeError> {
     39     if kind != KIND_HTTP_AUTH {
     40         return Err(EventEncodeError::InvalidKind(kind));
     41     }
     42     let tags = http_auth_build_tags(auth)?;
     43     Ok(WireEventParts {
     44         kind,
     45         content: String::new(),
     46         tags,
     47     })
     48 }