diff --git a/Cargo.toml b/Cargo.toml index c0234e9..0388520 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,12 +38,14 @@ repository = "https://forgejo.fireturlte.net/lazy-supplements" [workspace.dependencies] bevy = { git = "https://github.com/bevyengine/bevy.git", rev="16ffdaea0daec11e4347d965f56c9c8e1122a488" } +caretta-id = {path="./id", features=["rusqlite", "serde"]} chrono = "0.4.41" ciborium = "0.2.2" clap = { version = "4.5.38", features = ["derive"] } caretta-sync-core.path = "core" futures = { version = "0.3.31", features = ["executor"] } rand = "0.8.5" +rusqlite = "0.37.0" serde = { version = "1.0.219", features = ["derive"] } thiserror = "2.0.12" tokio = { version = "1.45.0", features = ["macros", "rt", "rt-multi-thread"] } diff --git a/core/Cargo.toml b/core/Cargo.toml index 7985d1b..7a71bf9 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -13,6 +13,7 @@ test = ["dep:tempfile", ] [dependencies] base64 = "0.22.1" +caretta-id.workspace = true chrono.workspace = true chrono-tz = "0.10.3" ciborium.workspace = true @@ -22,7 +23,7 @@ futures.workspace = true iroh.workspace = true prost.workspace = true prost-types.workspace = true -rusqlite = { version = "0.37.0", features = ["bundled", "chrono"] } +rusqlite = { workspace = true, features = ["bundled", "chrono"] } serde.workspace = true sysinfo = "0.37.0" tempfile = { version = "3.20.0", optional = true } diff --git a/id/Cargo.toml b/id/Cargo.toml index 9ffa4e0..d77c947 100644 --- a/id/Cargo.toml +++ b/id/Cargo.toml @@ -7,7 +7,14 @@ license.workspace = true repository.workspace = true [features] +default=[] +rusqlite = ["dep:rusqlite"] +serde = ["dep:serde"] [dependencies] rand.workspace = true +rusqlite = {workspace = true, optional = true} +serde = {workspace = true, optional = true} thiserror.workspace = true + + diff --git a/id/src/double.rs b/id/src/double.rs index 6480d16..b215a81 100644 --- a/id/src/double.rs +++ b/id/src/double.rs @@ -36,7 +36,7 @@ impl Id for DoubleId{ #[cfg(test)] fn is_valid(&self) -> bool { - self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(self.clone()) < Self::SIZE) + self.inner.0.is_valid() && self.inner.1.is_valid() && (u32::from(self) < Self::SIZE) } } @@ -105,9 +105,9 @@ impl TryFrom for DoubleId { } } -impl From for u32 { - fn from(value: DoubleId) -> Self { - u32::from(u16::from(value.inner.0)) * u32::from(SingleId::SIZE) + u32::from(u16::from(value.inner.1)) +impl From<&DoubleId> for u32 { + fn from(value: &DoubleId) -> Self { + u32::from(u16::from(&value.inner.0)) * u32::from(SingleId::SIZE) + u32::from(u16::from(&value.inner.1)) } } @@ -122,7 +122,7 @@ mod tests { let id: DoubleId = rand.r#gen(); assert!(id.is_valid()); assert_eq!(id,DoubleId::from_str(&id.to_string()).unwrap()); - assert_eq!(id, DoubleId::try_from(u32::from(id.clone())).unwrap()) + assert_eq!(id, DoubleId::try_from(u32::from(&id)).unwrap()) } #[test] fn random_x10() { diff --git a/id/src/lib.rs b/id/src/lib.rs index 4ae05c7..334b220 100644 --- a/id/src/lib.rs +++ b/id/src/lib.rs @@ -3,16 +3,16 @@ mod double; mod error; mod triple; mod utils; +#[cfg(feature="rusqlite")] +mod rusqlite; +#[cfg(feature="serde")] +mod serde; -use rand::Rng; pub use single::*; pub use double::*; pub use triple::*; pub use error::*; -const DOUBLE_ID_SIZE: u32 = (SingleId::SIZE as u32).pow(2); -const TRIPLE_ID_SIZE: u64 = (SingleId::SIZE as u64).pow(3); - pub trait Id { type SizeType; const NIL: Self; diff --git a/id/src/rusqlite.rs b/id/src/rusqlite.rs new file mode 100644 index 0000000..fa30656 --- /dev/null +++ b/id/src/rusqlite.rs @@ -0,0 +1,52 @@ +use rusqlite::{types::FromSql, Error, ToSql}; + +use crate::{DoubleId, SingleId, TripleId}; + +impl FromSql for SingleId { + fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult { + let int = u16::column_result(value)?; + Self::try_from(int).or_else(|e| { + Err(rusqlite::types::FromSqlError::Other(Box::new(e))) + }) + } +} + +impl ToSql for SingleId { + fn to_sql(&self) -> rusqlite::Result> { + Ok(u16::from(self).into()) + } +} + +impl FromSql for DoubleId { + fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult { + let int = u32::column_result(value)?; + Self::try_from(int).or_else(|e| { + Err(rusqlite::types::FromSqlError::Other(Box::new(e))) + }) + } +} + +impl ToSql for DoubleId { + fn to_sql(&self) -> rusqlite::Result> { + Ok(u32::from(self).into()) + } +} + +impl FromSql for TripleId { + fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult { + let int = u64::column_result(value)?; + Self::try_from(int).or_else(|e| { + Err(rusqlite::types::FromSqlError::Other(Box::new(e))) + }) + } +} + +impl ToSql for TripleId { + fn to_sql(&self) -> rusqlite::Result> { + Ok(rusqlite::types::ToSqlOutput::Owned(rusqlite::types::Value::Integer( + i64::try_from(u64::from(self)).map_err( + |err| Error::ToSqlConversionFailure(err.into()) + )? + ))) + } +} \ No newline at end of file diff --git a/id/src/serde.rs b/id/src/serde.rs new file mode 100644 index 0000000..e69de29 diff --git a/id/src/single.rs b/id/src/single.rs index d5367d5..4557328 100644 --- a/id/src/single.rs +++ b/id/src/single.rs @@ -178,8 +178,8 @@ impl TryFrom for SingleId { } } -impl From for u16 { - fn from(value: SingleId) -> Self { +impl From<&SingleId> for u16 { + fn from(value: &SingleId) -> Self { value.inner } } @@ -198,7 +198,7 @@ mod tests { assert!(chunk.is_valid()); let s = chunk.to_string(); assert_eq!(chunk,SingleId::from_str(&s).unwrap()); - let i = u16::from(chunk.clone()); + let i = u16::from(&chunk); assert_eq!(chunk, SingleId::try_from(i).unwrap()); } #[test] diff --git a/id/src/triple.rs b/id/src/triple.rs index 2d68e7a..84a5c3c 100644 --- a/id/src/triple.rs +++ b/id/src/triple.rs @@ -38,7 +38,7 @@ impl Id for TripleId{ #[cfg(test)] fn is_valid(&self) -> bool { - self.inner.0.is_valid() && self.inner.1.is_valid() && self.inner.2.is_valid() && (u64::from(self.clone()) < Self::SIZE) + self.inner.0.is_valid() && self.inner.1.is_valid() && self.inner.2.is_valid() && (u64::from(self) < Self::SIZE) } } @@ -113,11 +113,11 @@ impl TryFrom for TripleId { } } -impl From for u64 { - fn from(value: TripleId) -> Self { - (u16::from(value.inner.0) as u64) * (DoubleId::SIZE as u64) - + (u16::from(value.inner.1) as u64) * (SingleId::SIZE as u64) - + (u16::from(value.inner.2) as u64) +impl From<&TripleId> for u64 { + fn from(value: &TripleId) -> Self { + (u16::from(&value.inner.0) as u64) * (DoubleId::SIZE as u64) + + (u16::from(&value.inner.1) as u64) * (SingleId::SIZE as u64) + + (u16::from(&value.inner.2) as u64) } } @@ -132,7 +132,7 @@ mod tests { let id: TripleId = rand.r#gen(); assert!(id.is_valid()); assert_eq!(id, TripleId::from_str(&id.to_string()).unwrap()); - assert_eq!(id, TripleId::try_from(u64::from(id.clone())).unwrap()); + assert_eq!(id, TripleId::try_from(u64::from(&id)).unwrap()); } #[test] fn random_x10() {