Trait TransformResult

Source
pub trait TransformResult {
    // Required method
    fn to_json_str<T>(self, err_log: T) -> String
       where T: Debug + Display;
}
Expand description

将API调用结果转换为对外数据形式的特性声明。

Required Methods§

Source

fn to_json_str<T>(self, err_log: T) -> String
where T: Debug + Display,

将API结果转换为JSON字符串。

§Arguments
  • err_log: 客制化的出错日志信息。

returns: String 返回JSON字符串。

§Examples
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Display};
use api_resp::TransformResult;
#[derive(Debug, Serialize, Deserialize)]
struct Dept {
    id: Option<String>,
    pid: Option<String>,
    other_attr: Option<i32>
}

impl TransformResult for Dept {
    fn to_json_str<T>(self, err_log: T) -> String where T: Debug + Display {
        serde_json::to_string(&self).unwrap()
    }
}

let d = Dept {id: Some("01".to_string()), pid: None, other_attr: Some(10)};

let json_str = d.to_json_str("执行出错".to_string());
println!("json_str: {}", json_str);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§