diff --git a/Cargo.lock b/Cargo.lock index 183248f..8397a1f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -151,9 +151,9 @@ checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "cc" -version = "1.2.30" +version = "1.2.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7" +checksum = "c3a42d84bb6b69d3a8b3eaacf0d88f179e1929695e1ad012b6cf64d9caaa5fd2" dependencies = [ "shlex", ] @@ -318,9 +318,9 @@ checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "event-listener" -version = "5.4.0" +version = "5.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3492acde4c3fc54c845eaab3eed8bd00c7a7d881f78bfc801e43a93dec1331ae" +checksum = "e13b66accf52311f30a0db42147dadea9850cb48cd070028831ae5f5d4b856ab" dependencies = [ "concurrent-queue", "parking", @@ -955,9 +955,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.15" +version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e8af0dde094006011e6a740d4879319439489813bd0bcdc7d821beaeeff48ec" +checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77" dependencies = [ "bitflags", ] @@ -1022,15 +1022,15 @@ dependencies = [ [[package]] name = "rustc-demangle" -version = "0.1.25" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "989e6739f80c4ad5b13e0fd7fe89531180375b18520cc8c82080e4dc4035b84f" +checksum = "56f7d92ca342cea22a06f2121d944b4fd82af56988c270852495420f961d4ace" [[package]] name = "rustls" -version = "0.23.29" +version = "0.23.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2491382039b29b9b11ff08b76ff6c97cf287671dbb74f0be44bda389fffe9bd1" +checksum = "c0ebcbd2f03de0fc1122ad9bb24b127a5a6cd51d72604a3f3c50ac459762b6cc" dependencies = [ "once_cell", "ring", @@ -1219,7 +1219,7 @@ dependencies = [ [[package]] name = "seamantic" -version = "0.0.1" +version = "0.0.2" dependencies = [ "sea-orm", "sea-orm-migration", @@ -1250,9 +1250,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.141" +version = "1.0.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30b9eff21ebe718216c6ec64e1d9ac57087aad11efc64e32002bce4a0d4c03d3" +checksum = "030fedb782600dcbd6f02d479bf0d817ac3bb40d644745b769d6a96bc3afc5a7" dependencies = [ "itoa", "memchr", @@ -1321,12 +1321,12 @@ checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" [[package]] name = "socket2" -version = "0.5.10" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e22376abed350d73dd1cd119b57ffccad95b4e585a7cda43e286245ce23c0678" +checksum = "233504af464074f9d066d7b5416c5f9b894a5862a6506e306f7b816cdd6f1807" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] @@ -1531,9 +1531,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.46.1" +version = "1.47.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0cc3a2344dafbe23a245241fe8b09735b521110d30fcefbbd5feb1797ca35d17" +checksum = "89e49afdadebb872d3145a5638b59eb0691ea23e46ca484037cfab3b76b95038" dependencies = [ "backtrace", "bytes", @@ -1544,7 +1544,7 @@ dependencies = [ "slab", "socket2", "tokio-macros", - "windows-sys 0.52.0", + "windows-sys 0.59.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 22c0b7d..33e6308 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "seamantic" -version = "0.0.1" +version = "0.0.2" categories = [] description = "A library to enhance SeaORM" diff --git a/README.md b/README.md index 740767e..e4bf843 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Seamantic +[![Crates Version](https://img.shields.io/crates/v/seamantic.svg)](https://crates.io/crates/seamantic) + A library to enhance SeaORM ## Various builds diff --git a/src/model/duration.rs b/src/model/duration.rs new file mode 100644 index 0000000..74a6e08 --- /dev/null +++ b/src/model/duration.rs @@ -0,0 +1,94 @@ +//! [Duration] utilities + +use core::time::Duration; + +use sea_orm::sea_query::{ArrayType, Nullable, ValueType, ValueTypeErr}; +use sea_orm::{ColIdx, ColumnType, QueryResult, TryGetError, TryGetable, Value}; + +type SeaOrmRepr = u64; + +/// Wrapper around [Duration] to store a number of seconds +/// +/// ### Warning: +/// Sub-second precision will be lost +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr(feature = "serde", serde(transparent))] +#[repr(transparent)] +pub struct Seconds(pub Duration); + +impl From for Seconds { + fn from(value: Duration) -> Self { + Self(Duration::from_secs(value.as_secs())) + } +} + +impl From for Duration { + fn from(value: Seconds) -> Self { + value.0 + } +} + +impl ValueType for Seconds { + fn try_from(v: Value) -> Result { + ::try_from(v) + .map(Duration::from_secs) + .map(Self) + } + + fn type_name() -> String { + core::any::type_name::().to_string() + } + + fn array_type() -> ArrayType { + SeaOrmRepr::array_type() + } + + fn column_type() -> ColumnType { + SeaOrmRepr::column_type() + } +} + +impl From for Value { + fn from(value: Seconds) -> Self { + value.0.as_secs().into() + } +} + +impl TryGetable for Seconds { + fn try_get_by(res: &QueryResult, index: I) -> Result { + SeaOrmRepr::try_get_by(res, index) + .map(Duration::from_secs) + .map(Self) + } +} + +impl Nullable for Seconds { + fn null() -> Value { + SeaOrmRepr::null() + } +} + +#[cfg(test)] +mod tests { + use sea_orm::{ + ActiveModelBehavior, DeriveEntityModel, DerivePrimaryKey, DeriveRelation, EnumIter, + PrimaryKeyTrait, + }; + + use super::Seconds; + + #[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel)] + #[sea_orm(table_name = "seconds")] + pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + id: u8, + seconds: Seconds, + nullable: Option, + } + + impl ActiveModelBehavior for ActiveModel {} + + #[derive(Debug, EnumIter, DeriveRelation)] + pub enum Relation {} +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 88914af..89ca22d 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -1,4 +1,5 @@ //! SeaORM column types for enforcing data consistency +pub mod duration; pub mod id; pub mod path; diff --git a/src/model/path.rs b/src/model/path.rs index a1bb276..901b09e 100644 --- a/src/model/path.rs +++ b/src/model/path.rs @@ -90,7 +90,7 @@ mod tests { use super::PathBytes; #[derive(Debug, Clone, PartialEq, Eq, DeriveEntityModel)] - #[sea_orm(table_name = "media_library")] + #[sea_orm(table_name = "paths")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] id: u8,