From 57fd444f4efc30b534c1a7eebb550050d505b379 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 5 Feb 2021 22:09:41 +0100 Subject: [PATCH] move lib to separate folder --- Cargo.lock | 71 +++++++++++++++++++++++++- Cargo.toml | 7 ++- src/main.rs | 19 ++----- yopa/Cargo.toml | 20 ++++++++ {src => yopa/src}/cool.rs | 0 src/yopa.rs => yopa/src/lib.rs | 91 ++++++++++++++++++++++++++++------ 6 files changed, 172 insertions(+), 36 deletions(-) create mode 100644 yopa/Cargo.toml rename {src => yopa/src}/cool.rs (100%) rename src/yopa.rs => yopa/src/lib.rs (94%) diff --git a/Cargo.lock b/Cargo.lock index f1cf747..9a8c5b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,6 +29,15 @@ dependencies = [ "wasi", ] +[[package]] +name = "instant" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" +dependencies = [ + "cfg-if", +] + [[package]] name = "itertools" version = "0.10.0" @@ -56,6 +65,15 @@ version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" +[[package]] +name = "lock_api" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312" +dependencies = [ + "scopeguard", +] + [[package]] name = "log" version = "0.4.14" @@ -65,6 +83,31 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "parking_lot" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +dependencies = [ + "instant", + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ccb628cad4f84851442432c60ad8e1f607e29752d0bf072cbd0baf28aa34272" +dependencies = [ + "cfg-if", + "instant", + "libc", + "redox_syscall", + "smallvec", + "winapi", +] + [[package]] name = "proc-macro2" version = "1.0.24" @@ -95,6 +138,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "scopeguard" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" + [[package]] name = "serde" version = "1.0.123" @@ -137,6 +186,12 @@ dependencies = [ "thread-id", ] +[[package]] +name = "smallvec" +version = "1.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" + [[package]] name = "syn" version = "1.0.60" @@ -229,10 +284,24 @@ version = "0.1.0" dependencies = [ "anyhow", "itertools", + "lazy_static", "log", + "parking_lot", "serde", "serde_json", - "simple-logging", "thiserror", "uuid", ] + +[[package]] +name = "yopa-test" +version = "0.1.0" +dependencies = [ + "anyhow", + "log", + "serde", + "serde_json", + "simple-logging", + "thiserror", + "yopa", +] diff --git a/Cargo.toml b/Cargo.toml index e157a0c..0458bbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "yopa" +name = "yopa-test" version = "0.1.0" authors = ["Ondřej Hruška "] edition = "2018" @@ -10,11 +10,10 @@ edition = "2018" log = "0.4.13" simple-logging = "2.0.2" -uuid = { version = "0.8", features = ["serde", "v4"] } +yopa = { path = "./yopa" } + serde_json = "1.0.61" serde = { version = "1.0.120", features = ["derive"] } -#parking_lot = "0.11.1" anyhow = "1.0.38" thiserror = "1.0.23" -itertools = "0.10.0" diff --git a/src/main.rs b/src/main.rs index e057e7d..74eec8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,12 @@ use log::LevelFilter; -mod yopa; +use yopa; use yopa::model; use yopa::model::DataType; use crate::yopa::insert::{InsertObj, InsertValue, InsertRel}; use crate::yopa::data::TypedValue; -mod cool; - fn main() { simple_logging::log_to_stderr(LevelFilter::Debug); @@ -89,10 +87,7 @@ fn _main() -> anyhow::Result<()> { let MyBook1 = store.insert_object(InsertObj { model_id: Book, values: vec![ - InsertValue { - model_id: BookName, - value: TypedValue::String("Recipe Book 1".into()), - } + InsertValue::new(BookName, TypedValue::String("Recipe Book 1".into())), ], relations: vec![], })?; @@ -100,20 +95,14 @@ fn _main() -> anyhow::Result<()> { store.insert_object(InsertObj { model_id: Recipe, values: vec![ - InsertValue { - model_id: RecipeTitle, - value: TypedValue::String("Pancakes".into()), - } + InsertValue::new(RecipeTitle, TypedValue::String("Pancakes".into())), ], relations: vec![ InsertRel { model_id: BookToRecipe, related_id: MyBook1, values: vec![ - InsertValue { - model_id: BookToRecipePage, - value: TypedValue::Integer(123), - } + InsertValue::new(BookToRecipePage, TypedValue::Integer(123)) ] } ], diff --git a/yopa/Cargo.toml b/yopa/Cargo.toml new file mode 100644 index 0000000..e3c88bf --- /dev/null +++ b/yopa/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "yopa" +version = "0.1.0" +authors = ["Ondřej Hruška "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +log = "0.4.13" + +uuid = { version = "0.8", features = ["serde", "v4"] } +serde_json = "1.0.61" +serde = { version = "1.0.120", features = ["derive"] } + +parking_lot = "0.11.1" +anyhow = "1.0.38" +thiserror = "1.0.23" +itertools = "0.10.0" +lazy_static = "1.4.0" diff --git a/src/cool.rs b/yopa/src/cool.rs similarity index 100% rename from src/cool.rs rename to yopa/src/cool.rs diff --git a/src/yopa.rs b/yopa/src/lib.rs similarity index 94% rename from src/yopa.rs rename to yopa/src/lib.rs index 256fd28..38c6f62 100644 --- a/src/yopa.rs +++ b/yopa/src/lib.rs @@ -10,9 +10,47 @@ use model::Describe; use insert::InsertValue; use data::TypedValue; -/// Common identifier type -#[allow(non_camel_case_types)] -pub type ID = uuid::Uuid; +mod cool; + +/* +pub mod id { + /// Common identifier type + #[allow(non_camel_case_types)] + pub type ID = uuid::Uuid; + + pub fn next_id() -> ID { + uuid::Uuid::new_v4() + } + + pub fn zero_id() -> ID { + uuid::Uuid::nil() + } +} +*/ + +pub mod id { + /// Common identifier type + #[allow(non_camel_case_types)] + pub type ID = u64; + + lazy_static::lazy_static! { + static ref counter: parking_lot::Mutex = parking_lot::Mutex::new(0); + } + + pub fn next_id() -> ID { + let mut m = counter.lock(); + let v = *m; + *m += 1; + v + } + + pub fn zero_id() -> ID { + 0 + } +} + +pub use id::ID; +use id::next_id; /// Data model structs and enums pub mod model { @@ -111,11 +149,11 @@ pub mod model { /// Data value structs pub mod data { use serde::{Serialize, Deserialize}; - use super::ID; + use crate::ID; use std::borrow::Cow; - use super::StorageError; + use crate::StorageError; use std::num::ParseIntError; - use crate::yopa::model::DataType; + use crate::model::DataType; /// Value of a particular type #[derive(Debug,Clone,Serialize,Deserialize,PartialEq)] @@ -185,8 +223,8 @@ pub mod data { #[cfg(test)] mod tests { - use super::TypedValue; - use crate::yopa::model::DataType; + use crate::TypedValue; + use crate::model::DataType; #[test] fn test_cast_to_bool() { @@ -315,6 +353,15 @@ pub mod insert { pub value: TypedValue } + impl InsertValue { + pub fn new(model_id : ID, value : TypedValue) -> Self { + Self { + model_id, + value + } + } + } + /// Info for inserting a relation #[derive(Debug,Clone,Serialize,Deserialize)] pub struct InsertRel { @@ -323,6 +370,16 @@ pub mod insert { pub values: Vec } + impl InsertRel { + pub fn new(model_id : ID, related_id: ID, values : Vec) -> Self { + Self { + model_id, + related_id, + values + } + } + } + /// Info for inserting a relation #[derive(Debug,Clone,Serialize,Deserialize)] pub struct InsertObj { @@ -330,6 +387,16 @@ pub mod insert { pub values: Vec, pub relations: Vec, } + + impl InsertObj { + pub fn new(model_id : ID, values : Vec, relations: Vec) -> Self { + Self { + model_id, + values, + relations + } + } + } } #[derive(Debug, Default)] @@ -343,14 +410,6 @@ pub struct InMemoryStorage { properties: HashMap, } -fn next_id() -> ID { - uuid::Uuid::new_v4() -} - -pub fn zero_id() -> ID { - uuid::Uuid::nil() -} - #[derive(Debug,Error)] pub enum StorageError { #[error("Referenced {0} does not exist")]