use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseSerialized {
pub lc: u32,
pub id: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedConstructor {
#[serde(flatten)]
pub base: BaseSerialized,
#[serde(rename = "type")]
pub type_tag: String,
pub kwargs: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedSecret {
#[serde(flatten)]
pub base: BaseSerialized,
#[serde(rename = "type")]
pub type_tag: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedNotImplemented {
#[serde(flatten)]
pub base: BaseSerialized,
#[serde(rename = "type")]
pub type_tag: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub repr: Option<String>,
}
pub trait Serializable: Send + Sync {
fn is_lc_serializable(&self) -> bool {
false
}
fn get_lc_namespace(&self) -> Vec<String>;
fn lc_secrets(&self) -> HashMap<String, String> {
HashMap::new()
}
fn lc_attributes(&self) -> HashMap<String, Value> {
HashMap::new()
}
fn lc_id(&self) -> Vec<String>;
fn to_json(&self) -> Value;
}
pub fn dumpd(obj: &dyn Serializable) -> Value {
obj.to_json()
}
pub fn dumps(obj: &dyn Serializable, pretty: bool) -> String {
let value = obj.to_json();
if pretty {
serde_json::to_string_pretty(&value).unwrap_or_default()
} else {
serde_json::to_string(&value).unwrap_or_default()
}
}
pub fn to_json_not_implemented(type_name: &str, repr: &str, namespace: Vec<String>) -> Value {
let mut id = namespace;
id.push(type_name.to_string());
serde_json::json!({
"lc": 1,
"type": "not_implemented",
"id": id,
"repr": repr,
})
}
pub fn make_serialized_secret(secret_id: Vec<String>) -> Value {
serde_json::json!({
"lc": 1,
"type": "secret",
"id": secret_id,
})
}