在java,go,Typescript等类型语言中,可以使用任意类型,在rust中,如何对于包含字段类型为值为任意类型的字典,支持serde序列化,可以参考以下代码:
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum NodeState {
Starting,
#[default]
Up,
Suspicious,
Down,
Isolation,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct Member {
pub ip: String,
pub port: i32,
pub state: NodeState,
pub extend_info: BTreeMap<String, serde_json::Value>,
pub address: String,
pub fail_access_cnt: i32,
}
impl Member {
pub fn new() -> Self {
let mut m = BTreeMap::<String, serde_json::Value>::new();
m.insert(
String::from("site"),
serde_json::Value::String(
std::env::var("nacos.core.member.meta.site").unwrap_or(String::from("unknow")),
),
);
Self {
ip: String::from(""),
port: -1,
state: NodeState::Up,
address: String::from(""),
extend_info: m,
fail_access_cnt: 0,
}
}
}