surrealpatch/src/key/ix.rs
2022-01-13 17:40:20 +00:00

61 lines
1.2 KiB
Rust

use crate::err::Error;
use crate::key::bytes::{deserialize, serialize};
use crate::key::BASE;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct Ix {
kv: String,
_a: String,
ns: String,
_b: String,
db: String,
_c: String,
tb: String,
_d: String,
ix: String,
}
pub fn new(ns: &str, db: &str, tb: &str, ix: &str) -> Ix {
Ix::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string())
}
impl Ix {
pub fn new(ns: String, db: String, tb: String, ix: String) -> Ix {
Ix {
kv: BASE.to_owned(),
_a: String::from("*"),
ns,
_b: String::from("*"),
db,
_c: String::from("*"),
tb,
_d: String::from("!ix"),
ix,
}
}
pub fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(serialize(self)?)
}
pub fn decode(v: &[u8]) -> Result<Ix, Error> {
Ok(deserialize(v)?)
}
}
#[cfg(test)]
mod tests {
#[test]
fn key() {
use super::*;
#[rustfmt::skip]
let val = Ix::new(
"test".to_string(),
"test".to_string(),
"test".to_string(),
"test".to_string(),
);
let enc = Ix::encode(&val).unwrap();
let dec = Ix::decode(&enc).unwrap();
assert_eq!(val, dec);
}
}