j_db/src/model.rs

30 lines
727 B
Rust

use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt::Debug;
pub trait JdbModel: Serialize + DeserializeOwned + Debug {
fn id(&self) -> Option<u64>;
fn set_id(&mut self, id: u64);
fn tree() -> String;
fn check_unique(&self, _: &Self) -> bool {
true
}
fn to_json_string(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
}
fn try_from_str(s: &str) -> Result<Self, serde_json::Error> {
serde_json::from_str(s)
}
fn to_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
serde_json::to_vec(self)
}
fn try_from_bytes(b: &[u8]) -> Result<Self, serde_json::Error> {
serde_json::from_slice(b)
}
}