tina-core 0.0.2

Tina platform
Documentation
//! 请求数据

use std::fmt::Debug;

use http::{header::IntoHeaderName, Extensions, HeaderMap, HeaderValue};

use crate::{app_error_from, tina::data::AppResult};

/// 请求数据
pub struct GrpcReqData<D> {
    /// 数据
    pub data: D,
    /// 元数据
    pub metadata: HeaderMap,
    /// 扩展
    pub extensions: Extensions,
}

impl<D> GrpcReqData<D> {
    /// 构建
    pub fn new(data: D) -> Self {
        Self {
            data,
            metadata: HeaderMap::new(),
            extensions: Extensions::new(),
        }
    }
    /// 添加metadata
    pub fn with_metadata<K, V>(mut self, key: K, value: V) -> AppResult<Self>
    where
        K: IntoHeaderName,
        V: TryInto<HeaderValue>,
        V::Error: std::error::Error + Send + Sync + 'static,
    {
        let value = value.try_into().map_err(app_error_from!())?;
        self.metadata.insert(key, value);
        Ok(self)
    }
    /// 添加Extention
    pub fn with_extension<T: Send + Sync + 'static>(mut self, extension: T) -> AppResult<Self> {
        self.extensions.insert(extension);
        Ok(self)
    }
}

impl<D: Debug> Debug for GrpcReqData<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GrpcReqData").field("data", &self.data).field("metadata", &self.metadata).finish()
    }
}