fache 0.1.308

发车工具箱
Documentation
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Color(pub u8, pub u8, pub u8);

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct Point(pub f64, pub f64);

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct LogLat {
    pub longitude: f64, // 经度
    pub latitude: f64,  // 纬度
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, PartialOrd)]
pub enum Status {
    #[default]
    Waiting,
    Processing,
    Done,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct Define<T: Default> {
    #[serde(default)]
    pub status: Status,

    #[serde(default)]
    pub key: String,

    #[serde(default)]
    pub color: Color,

    #[serde(default)]
    pub point: Point,

    #[serde(default)]
    pub jwd: LogLat,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub store_id: Option<String>,
    /// 字段重命名
    #[serde(rename = "printerContent")]
    #[serde(default)]
    pub printer_content: String,

    #[serde(default = "Define::<T>::default_username")]
    pub username: String,

    #[serde(default = "Define::<T>::default_age")]
    pub age: u8,

    #[serde(default = "Define::<T>::default_active")]
    pub active: bool,

    #[serde(default = "Define::<T>::default_port")]
    pub port: u16,

    #[serde(default = "Define::<T>::default_host")]
    pub host: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(flatten, default)]
    pub adcode: Option<String>,

    #[serde(flatten, default)]
    pub value: T,
}
/// 为serde提供的默认值函数
impl<T: Default> Define<T> {
    fn default_username() -> String {
        "Anonymous".to_string()
    }
    fn default_age() -> u8 {
        18
    }
    fn default_active() -> bool {
        true
    }
    fn default_port() -> u16 {
        8080
    }
    fn default_host() -> String {
        "localhost".to_string()
    }
}

// 通过 Trait 为结构体添加方法
pub trait Info: Default + Clone + Sync + Send + Sized {
    fn add(a: u32, b: u32) -> u32 {
        a + b
    }
    ///定义异步函数
    fn testing(&self) -> impl Future<Output = String> + Send + '_;
}

impl<T: Default + Clone + Sync + Send + Sized> Info for Define<T> {
    async fn testing(&self) -> String {
        self.key.clone()
    }
}