From 744b480f882586be90de3c4d11d2d6e0ac19d74d Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Mon, 31 Jan 2022 23:11:06 +0000 Subject: [PATCH] Add Into> and From> for storage keys --- src/key/database.rs | 12 ++++++++++++ src/key/db.rs | 12 ++++++++++++ src/key/dt.rs | 12 ++++++++++++ src/key/du.rs | 12 ++++++++++++ src/key/ev.rs | 12 ++++++++++++ src/key/fd.rs | 12 ++++++++++++ src/key/ft.rs | 12 ++++++++++++ src/key/index.rs | 12 ++++++++++++ src/key/ix.rs | 12 ++++++++++++ src/key/key.rs | 12 ++++++++++++ src/key/kv.rs | 12 ++++++++++++ src/key/lv.rs | 12 ++++++++++++ src/key/namespace.rs | 12 ++++++++++++ src/key/ns.rs | 12 ++++++++++++ src/key/nt.rs | 12 ++++++++++++ src/key/nu.rs | 12 ++++++++++++ src/key/point.rs | 12 ++++++++++++ src/key/sc.rs | 12 ++++++++++++ src/key/st.rs | 12 ++++++++++++ src/key/table.rs | 12 ++++++++++++ src/key/tb.rs | 12 ++++++++++++ src/key/thing.rs | 13 ++++++++++++- src/sql/idiom.rs | 5 +++++ src/sql/object.rs | 2 +- 24 files changed, 270 insertions(+), 2 deletions(-) diff --git a/src/key/database.rs b/src/key/database.rs index 402e30c1..b12d895f 100644 --- a/src/key/database.rs +++ b/src/key/database.rs @@ -12,6 +12,18 @@ pub struct Database { db: String, } +impl Into> for Database { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Database { + fn from(val: Vec) -> Self { + Database::decode(&val).unwrap() + } +} + pub fn new(ns: &str, db: &str) -> Database { Database::new(ns.to_string(), db.to_string()) } diff --git a/src/key/db.rs b/src/key/db.rs index 605a8af3..5d840b4c 100644 --- a/src/key/db.rs +++ b/src/key/db.rs @@ -12,6 +12,18 @@ pub struct Db { db: String, } +impl Into> for Db { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Db { + fn from(val: Vec) -> Self { + Db::decode(&val).unwrap() + } +} + pub fn new(ns: &str, db: &str) -> Db { Db::new(ns.to_string(), db.to_string()) } diff --git a/src/key/dt.rs b/src/key/dt.rs index d63f18ce..80d6fe91 100644 --- a/src/key/dt.rs +++ b/src/key/dt.rs @@ -14,6 +14,18 @@ pub struct Dt { tk: String, } +impl Into> for Dt { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Dt { + fn from(val: Vec) -> 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()) } diff --git a/src/key/du.rs b/src/key/du.rs index ae2c8928..d3348c0e 100644 --- a/src/key/du.rs +++ b/src/key/du.rs @@ -14,6 +14,18 @@ pub struct Du { us: String, } +impl Into> for Du { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Du { + fn from(val: Vec) -> 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()) } diff --git a/src/key/ev.rs b/src/key/ev.rs index f4e98dcc..94dae0cc 100644 --- a/src/key/ev.rs +++ b/src/key/ev.rs @@ -16,6 +16,18 @@ pub struct Ev { ev: String, } +impl Into> for Ev { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Ev { + fn from(val: Vec) -> 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()) } diff --git a/src/key/fd.rs b/src/key/fd.rs index 04d50792..7554daf6 100644 --- a/src/key/fd.rs +++ b/src/key/fd.rs @@ -16,6 +16,18 @@ pub struct Fd { fd: String, } +impl Into> for Fd { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Fd { + fn from(val: Vec) -> 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()) } diff --git a/src/key/ft.rs b/src/key/ft.rs index 8c079054..45815e6e 100644 --- a/src/key/ft.rs +++ b/src/key/ft.rs @@ -16,6 +16,18 @@ pub struct Ft { ft: String, } +impl Into> for Ft { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Ft { + fn from(val: Vec) -> 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()) } diff --git a/src/key/index.rs b/src/key/index.rs index cf0271b0..3888c8c0 100644 --- a/src/key/index.rs +++ b/src/key/index.rs @@ -18,6 +18,18 @@ pub struct Index { fd: Value, } +impl Into> for Index { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Index { + fn from(val: Vec) -> 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) } diff --git a/src/key/ix.rs b/src/key/ix.rs index d9e7940e..6af8ef12 100644 --- a/src/key/ix.rs +++ b/src/key/ix.rs @@ -16,6 +16,18 @@ pub struct Ix { ix: String, } +impl Into> for Ix { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Ix { + fn from(val: Vec) -> 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()) } diff --git a/src/key/key.rs b/src/key/key.rs index 90cc83f3..23c58fdb 100644 --- a/src/key/key.rs +++ b/src/key/key.rs @@ -68,6 +68,18 @@ pub enum Key { Edge, // Edge resource data key } +impl Into> for Key { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Key { + fn from(val: Vec) -> Self { + Key::decode(&val).unwrap() + } +} + impl Key { pub fn encode(&self) -> Result, Error> { Ok(serialize(self)?) diff --git a/src/key/kv.rs b/src/key/kv.rs index fbb50ae1..8a3fefc4 100644 --- a/src/key/kv.rs +++ b/src/key/kv.rs @@ -8,6 +8,18 @@ pub struct Kv { kv: String, } +impl Into> for Kv { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Kv { + fn from(val: Vec) -> Self { + Kv::decode(&val).unwrap() + } +} + pub fn new() -> Kv { Kv::new() } diff --git a/src/key/lv.rs b/src/key/lv.rs index 9e4a3c2f..7b1b9f4f 100644 --- a/src/key/lv.rs +++ b/src/key/lv.rs @@ -16,6 +16,18 @@ pub struct Lv { lv: String, } +impl Into> for Lv { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Lv { + fn from(val: Vec) -> 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()) } diff --git a/src/key/namespace.rs b/src/key/namespace.rs index 36aa949d..3cb9ca26 100644 --- a/src/key/namespace.rs +++ b/src/key/namespace.rs @@ -10,6 +10,18 @@ pub struct Namespace { ns: String, } +impl Into> for Namespace { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Namespace { + fn from(val: Vec) -> Self { + Namespace::decode(&val).unwrap() + } +} + pub fn new(ns: &str) -> Namespace { Namespace::new(ns.to_string()) } diff --git a/src/key/ns.rs b/src/key/ns.rs index ca94954f..9a7afb66 100644 --- a/src/key/ns.rs +++ b/src/key/ns.rs @@ -10,6 +10,18 @@ pub struct Ns { ns: String, } +impl Into> for Ns { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Ns { + fn from(val: Vec) -> Self { + Ns::decode(&val).unwrap() + } +} + pub fn new(ns: &str) -> Ns { Ns::new(ns.to_string()) } diff --git a/src/key/nt.rs b/src/key/nt.rs index a6ffee5b..925dbe6f 100644 --- a/src/key/nt.rs +++ b/src/key/nt.rs @@ -12,6 +12,18 @@ pub struct Nt { tk: String, } +impl Into> for Nt { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Nt { + fn from(val: Vec) -> Self { + Nt::decode(&val).unwrap() + } +} + pub fn new(ns: &str, tk: &str) -> Nt { Nt::new(ns.to_string(), tk.to_string()) } diff --git a/src/key/nu.rs b/src/key/nu.rs index abbcd69e..35b035ec 100644 --- a/src/key/nu.rs +++ b/src/key/nu.rs @@ -12,6 +12,18 @@ pub struct Nu { us: String, } +impl Into> for Nu { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Nu { + fn from(val: Vec) -> Self { + Nu::decode(&val).unwrap() + } +} + pub fn new(ns: &str, us: &str) -> Nu { Nu::new(ns.to_string(), us.to_string()) } diff --git a/src/key/point.rs b/src/key/point.rs index ba908f2b..65bc0613 100644 --- a/src/key/point.rs +++ b/src/key/point.rs @@ -19,6 +19,18 @@ pub struct Index { id: String, } +impl Into> for Index { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Index { + fn from(val: Vec) -> 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()) } diff --git a/src/key/sc.rs b/src/key/sc.rs index 114a6187..7b360475 100644 --- a/src/key/sc.rs +++ b/src/key/sc.rs @@ -14,6 +14,18 @@ pub struct Sc { sc: String, } +impl Into> for Sc { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Sc { + fn from(val: Vec) -> 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()) } diff --git a/src/key/st.rs b/src/key/st.rs index 0543edab..a930e1c3 100644 --- a/src/key/st.rs +++ b/src/key/st.rs @@ -16,6 +16,18 @@ pub struct St { tk: String, } +impl Into> for St { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for St { + fn from(val: Vec) -> 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()) } diff --git a/src/key/table.rs b/src/key/table.rs index a482a693..b7708e46 100644 --- a/src/key/table.rs +++ b/src/key/table.rs @@ -14,6 +14,18 @@ pub struct Table { tb: String, } +impl Into> for Table { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Table { + fn from(val: Vec) -> 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()) } diff --git a/src/key/tb.rs b/src/key/tb.rs index e0894319..59885b2f 100644 --- a/src/key/tb.rs +++ b/src/key/tb.rs @@ -14,6 +14,18 @@ pub struct Tb { tb: String, } +impl Into> for Tb { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Tb { + fn from(val: Vec) -> 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()) } diff --git a/src/key/thing.rs b/src/key/thing.rs index 6b3a4512..66a20e76 100644 --- a/src/key/thing.rs +++ b/src/key/thing.rs @@ -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> for Thing { + fn into(self) -> Vec { + self.encode().unwrap() + } +} + +impl From> for Thing { + fn from(val: Vec) -> 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()) } diff --git a/src/sql/idiom.rs b/src/sql/idiom.rs index 8f8f11dd..56de95f5 100644 --- a/src/sql/idiom.rs +++ b/src/sql/idiom.rs @@ -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 { diff --git a/src/sql/object.rs b/src/sql/object.rs index f4e0eaf7..20b8fee8 100644 --- a/src/sql/object.rs +++ b/src/sql/object.rs @@ -53,7 +53,7 @@ impl From 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, }, }