Add Into<Vec<u8>> and From<Vec<u8>> for storage keys

This commit is contained in:
Tobie Morgan Hitchcock 2022-01-31 23:11:06 +00:00
parent c3cf0e5e24
commit 744b480f88
24 changed files with 270 additions and 2 deletions

View file

@ -12,6 +12,18 @@ pub struct Database {
db: String,
}
impl Into<Vec<u8>> for Database {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Database {
fn from(val: Vec<u8>) -> Self {
Database::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str) -> Database {
Database::new(ns.to_string(), db.to_string())
}

View file

@ -12,6 +12,18 @@ pub struct Db {
db: String,
}
impl Into<Vec<u8>> for Db {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Db {
fn from(val: Vec<u8>) -> Self {
Db::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str) -> Db {
Db::new(ns.to_string(), db.to_string())
}

View file

@ -14,6 +14,18 @@ pub struct Dt {
tk: String,
}
impl Into<Vec<u8>> for Dt {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Dt {
fn from(val: Vec<u8>) -> Self {
Dt::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str) -> Dt {
Dt::new(ns.to_string(), db.to_string(), tb.to_string())
}

View file

@ -14,6 +14,18 @@ pub struct Du {
us: String,
}
impl Into<Vec<u8>> for Du {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Du {
fn from(val: Vec<u8>) -> Self {
Du::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, us: &str) -> Du {
Du::new(ns.to_string(), db.to_string(), us.to_string())
}

View file

@ -16,6 +16,18 @@ pub struct Ev {
ev: String,
}
impl Into<Vec<u8>> for Ev {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Ev {
fn from(val: Vec<u8>) -> Self {
Ev::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, ev: &str) -> Ev {
Ev::new(ns.to_string(), db.to_string(), tb.to_string(), ev.to_string())
}

View file

@ -16,6 +16,18 @@ pub struct Fd {
fd: String,
}
impl Into<Vec<u8>> for Fd {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Fd {
fn from(val: Vec<u8>) -> Self {
Fd::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, fd: &str) -> Fd {
Fd::new(ns.to_string(), db.to_string(), tb.to_string(), fd.to_string())
}

View file

@ -16,6 +16,18 @@ pub struct Ft {
ft: String,
}
impl Into<Vec<u8>> for Ft {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Ft {
fn from(val: Vec<u8>) -> Self {
Ft::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, ft: &str) -> Ft {
Ft::new(ns.to_string(), db.to_string(), tb.to_string(), ft.to_string())
}

View file

@ -18,6 +18,18 @@ pub struct Index {
fd: Value,
}
impl Into<Vec<u8>> for Index {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Index {
fn from(val: Vec<u8>) -> Self {
Index::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Value) -> Index {
Index::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string(), fd)
}

View file

@ -16,6 +16,18 @@ pub struct Ix {
ix: String,
}
impl Into<Vec<u8>> for Ix {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Ix {
fn from(val: Vec<u8>) -> Self {
Ix::decode(&val).unwrap()
}
}
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())
}

View file

@ -68,6 +68,18 @@ pub enum Key {
Edge, // Edge resource data key
}
impl Into<Vec<u8>> for Key {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Key {
fn from(val: Vec<u8>) -> Self {
Key::decode(&val).unwrap()
}
}
impl Key {
pub fn encode(&self) -> Result<Vec<u8>, Error> {
Ok(serialize(self)?)

View file

@ -8,6 +8,18 @@ pub struct Kv {
kv: String,
}
impl Into<Vec<u8>> for Kv {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Kv {
fn from(val: Vec<u8>) -> Self {
Kv::decode(&val).unwrap()
}
}
pub fn new() -> Kv {
Kv::new()
}

View file

@ -16,6 +16,18 @@ pub struct Lv {
lv: String,
}
impl Into<Vec<u8>> for Lv {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Lv {
fn from(val: Vec<u8>) -> Self {
Lv::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, lv: &str) -> Lv {
Lv::new(ns.to_string(), db.to_string(), tb.to_string(), lv.to_string())
}

View file

@ -10,6 +10,18 @@ pub struct Namespace {
ns: String,
}
impl Into<Vec<u8>> for Namespace {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Namespace {
fn from(val: Vec<u8>) -> Self {
Namespace::decode(&val).unwrap()
}
}
pub fn new(ns: &str) -> Namespace {
Namespace::new(ns.to_string())
}

View file

@ -10,6 +10,18 @@ pub struct Ns {
ns: String,
}
impl Into<Vec<u8>> for Ns {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Ns {
fn from(val: Vec<u8>) -> Self {
Ns::decode(&val).unwrap()
}
}
pub fn new(ns: &str) -> Ns {
Ns::new(ns.to_string())
}

View file

@ -12,6 +12,18 @@ pub struct Nt {
tk: String,
}
impl Into<Vec<u8>> for Nt {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Nt {
fn from(val: Vec<u8>) -> Self {
Nt::decode(&val).unwrap()
}
}
pub fn new(ns: &str, tk: &str) -> Nt {
Nt::new(ns.to_string(), tk.to_string())
}

View file

@ -12,6 +12,18 @@ pub struct Nu {
us: String,
}
impl Into<Vec<u8>> for Nu {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Nu {
fn from(val: Vec<u8>) -> Self {
Nu::decode(&val).unwrap()
}
}
pub fn new(ns: &str, us: &str) -> Nu {
Nu::new(ns.to_string(), us.to_string())
}

View file

@ -19,6 +19,18 @@ pub struct Index {
id: String,
}
impl Into<Vec<u8>> for Index {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Index {
fn from(val: Vec<u8>) -> Self {
Index::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, ix: &str, fd: Value, id: &str) -> Index {
Index::new(ns.to_string(), db.to_string(), tb.to_string(), ix.to_string(), fd, id.to_string())
}

View file

@ -14,6 +14,18 @@ pub struct Sc {
sc: String,
}
impl Into<Vec<u8>> for Sc {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Sc {
fn from(val: Vec<u8>) -> Self {
Sc::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, sc: &str) -> Sc {
Sc::new(ns.to_string(), db.to_string(), sc.to_string())
}

View file

@ -16,6 +16,18 @@ pub struct St {
tk: String,
}
impl Into<Vec<u8>> for St {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for St {
fn from(val: Vec<u8>) -> Self {
St::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, sc: &str, tk: &str) -> St {
St::new(ns.to_string(), db.to_string(), sc.to_string(), tk.to_string())
}

View file

@ -14,6 +14,18 @@ pub struct Table {
tb: String,
}
impl Into<Vec<u8>> for Table {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Table {
fn from(val: Vec<u8>) -> Self {
Table::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str) -> Table {
Table::new(ns.to_string(), db.to_string(), tb.to_string())
}

View file

@ -14,6 +14,18 @@ pub struct Tb {
tb: String,
}
impl Into<Vec<u8>> for Tb {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Tb {
fn from(val: Vec<u8>) -> Self {
Tb::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str) -> Tb {
Tb::new(ns.to_string(), db.to_string(), tb.to_string())
}

View file

@ -1,7 +1,6 @@
use crate::err::Error;
use crate::key::bytes::{deserialize, serialize};
use crate::key::BASE;
use crate::sql::value::Value;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
@ -17,6 +16,18 @@ pub struct Thing {
id: String,
}
impl Into<Vec<u8>> for Thing {
fn into(self) -> Vec<u8> {
self.encode().unwrap()
}
}
impl From<Vec<u8>> for Thing {
fn from(val: Vec<u8>) -> Self {
Thing::decode(&val).unwrap()
}
}
pub fn new(ns: &str, db: &str, tb: &str, id: &str) -> Thing {
Thing::new(ns.to_string(), db.to_string(), tb.to_string(), id.to_string())
}

View file

@ -54,12 +54,17 @@ impl Idiom {
p.push(n);
Idiom::from(p)
}
pub fn next(&self) -> Idiom {
match self.parts.len() {
0 => Idiom::from(vec![]),
_ => Idiom::from(self.parts[1..].to_vec()),
}
}
pub fn to_path(&self) -> String {
format!("/{}", self).replace(']', "").replace(&['.', '['][..], "/")
}
}
impl Idiom {

View file

@ -53,7 +53,7 @@ impl From<Operation> for Object {
Op::Replace => Value::from("replace"),
Op::Change => Value::from("change"),
},
String::from("path") => Value::from(v.path),
String::from("path") => v.path.to_path().into(),
String::from("value") => v.value,
},
}