polariton_server 0.6.0

Server functionality for the Photon packet system
Documentation
use polariton::operation::{ParameterTable, OperationResponse};

pub fn result_to_op_resp<const CODE: u8, C>(result: Result<ParameterTable<C>, i16>) -> OperationResponse<C> {
    match result {
        Ok(p_out) => {
            OperationResponse {
                code: CODE,
                return_code: 0,
                message: polariton::operation::Typed::Null,
                params: p_out,
            }
        },
        Err(e_code) => {
            OperationResponse {
                code: CODE,
                return_code: e_code,
                message: polariton::operation::Typed::Null,
                params: std::collections::HashMap::new().into(),
            }
        }
    }
}

pub struct StateFunc<const CODE: u8, S: Send + Sync, U: Send + Sync, F: (Fn(ParameterTable<C>, &S, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync, C: Send + Sync + 'static = ()> {
    //_state_ty: std::marker::PhantomData<S>,
    _user_ty: std::marker::PhantomData<U>,
    _custom_ty: std::marker::PhantomData<C>,
    func: F,
    state: S,
}

impl <C: Send + Sync + 'static, const CODE: u8, S: Send + Sync, U: Send + Sync, F: (Fn(ParameterTable<C>, &S, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync> StateFunc<CODE, S, U, F, C> {
    pub fn new(f: F, s: S) -> Self {
        Self {
            //_state_ty: std::marker::PhantomData::default(),
            _user_ty: std::marker::PhantomData::default(),
            _custom_ty: std::marker::PhantomData::default(),
            func: f,
            state: s,
        }
    }
}

#[cfg_attr(feature = "async", async_trait::async_trait)]
impl <C: Send + Sync + 'static, const CODE: u8, S: Send + Sync, U: Send + Sync, F: (Fn(ParameterTable<C>, &S, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync> super::Operation<C> for StateFunc<CODE, S, U, F, C> {
    type User = U;

    fn handle(&self, p: polariton::operation::ParameterTable<C>, u: &Self::User) -> OperationResponse<C> {
        result_to_op_resp::<CODE, C>((self.func)(p, &self.state, u))
    }

    #[cfg(feature = "async")]
    async fn handle_async(&self, p: polariton::operation::ParameterTable<C>, u: &Self::User) -> OperationResponse<C> {
        self.handle(p, u)
    }
}

impl <C: Send + Sync + 'static, const CODE: u8, S: Send + Sync, U: Send + Sync, F: (Fn(ParameterTable<C>, &S, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync> super::OperationCode for StateFunc<CODE, S, U, F, C> {
    fn op_code() -> u8 {
        CODE
    }
}

pub struct SimpleFunc<const CODE: u8, U: Send + Sync, F: (Fn(ParameterTable<C>, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync, C: Send + Sync + 'static = ()> {
    _user_ty: std::marker::PhantomData<U>,
    _custom_ty: std::marker::PhantomData<C>,
    func: F,
}

impl <C: Send + Sync + 'static, const CODE: u8, U: Send + Sync, F: (Fn(ParameterTable<C>, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync> SimpleFunc<CODE, U, F, C> {
    pub fn new(f: F) -> Self {
        Self {
            _user_ty: std::marker::PhantomData::default(),
            _custom_ty: std::marker::PhantomData::default(),
            func: f,
        }
    }
}

#[cfg_attr(feature = "async", async_trait::async_trait)]
impl <C: Send + Sync + 'static, const CODE: u8, U: Send + Sync, F: (Fn(ParameterTable<C>, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync> super::Operation<C> for SimpleFunc<CODE, U, F, C> {
    type User = U;

    fn handle(&self, p: polariton::operation::ParameterTable<C>, u: &Self::User) -> OperationResponse<C> {
        result_to_op_resp::<CODE, C>((self.func)(p, u))
    }

    #[cfg(feature = "async")]
    async fn handle_async(&self, p: polariton::operation::ParameterTable<C>, u: &Self::User) -> OperationResponse<C> {
        self.handle(p, u)
    }
}

impl <C: Send + Sync + 'static, const CODE: u8, U: Send + Sync, F: (Fn(ParameterTable<C>, &U) -> Result<ParameterTable<C>, i16>) + Send + Sync> super::OperationCode for SimpleFunc<CODE, U, F, C> {
    fn op_code() -> u8 {
        CODE
    }
}