cli

Command-line interface for Radroots
git clone https://radroots.dev/git/cli.git
Log | Files | Refs | README | LICENSE

request.rs (1560B)


      1 use std::fmt::Debug;
      2 
      3 use serde_json::{Map, Value};
      4 
      5 use super::context::OperationContext;
      6 use super::error::OperationAdapterError;
      7 use crate::registry::{OperationSpec, get_operation};
      8 
      9 pub type OperationData = Map<String, Value>;
     10 
     11 pub trait OperationRequestPayload: Debug + Clone + PartialEq + 'static {
     12     const OPERATION_ID: &'static str;
     13     const REQUEST_TYPE: &'static str;
     14 }
     15 
     16 pub trait OperationRequestData: OperationRequestPayload {
     17     fn input(&self) -> &OperationData;
     18 }
     19 
     20 #[derive(Debug, Clone, PartialEq)]
     21 pub struct OperationRequest<P: OperationRequestPayload> {
     22     pub spec: &'static OperationSpec,
     23     pub context: OperationContext,
     24     pub payload: P,
     25 }
     26 
     27 impl<P: OperationRequestPayload> OperationRequest<P> {
     28     pub fn new(context: OperationContext, payload: P) -> Result<Self, OperationAdapterError> {
     29         let spec = get_operation(P::OPERATION_ID)
     30             .ok_or_else(|| OperationAdapterError::UnknownOperation(P::OPERATION_ID.to_owned()))?;
     31         if spec.rust_request != P::REQUEST_TYPE {
     32             return Err(OperationAdapterError::RequestTypeMismatch {
     33                 operation_id: P::OPERATION_ID.to_owned(),
     34                 registry_request: spec.rust_request.to_owned(),
     35                 adapter_request: P::REQUEST_TYPE.to_owned(),
     36             });
     37         }
     38         Ok(Self {
     39             spec,
     40             context,
     41             payload,
     42         })
     43     }
     44 
     45     pub fn operation_id(&self) -> &'static str {
     46         P::OPERATION_ID
     47     }
     48 
     49     pub fn request_type_name(&self) -> &'static str {
     50         P::REQUEST_TYPE
     51     }
     52 }