Crates.io | kubetsu |
lib.rs | kubetsu |
version | 0.2.0 |
source | src |
created_at | 2024-01-27 07:10:36.3109 |
updated_at | 2024-01-27 09:52:51.210405 |
description | distinguish value type of other struct |
homepage | https://github.com/walf443/kubetsu |
repository | https://github.com/walf443/kubetsu.git |
max_upload_size | |
id | 1116541 |
size | 38,456 |
This is a library that distinguish struct value type as other type value.
kubetsu
(区別) means distinguish in Japanese.
use kubetsu::Id;
struct User {
id: Id<Self, i64>
}
type UserId = Id<User, i64>;
struct Item {
id: Id<Self, i64>
}
type ItemId = Id<Item, i64>;
fn main() {
let user_id = UserId::new(1);
let item_id = ItemId::new(1);
assert_ne!(user_id, item_id);
// compile error
// ---- src/lib.rs - Id (line 10) stdout ----
// error[E0308]: mismatched types
// --> src/lib.rs:29:3
// |
// 21 | assert_ne!(user_id, item_id); // compile error
// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Id<User, i64>`, found `Id<Item, i64>`
// |
// = note: expected struct `Id<User, _>`
// found struct `Id<Item, _>`
// = note: this error originates in the macro `assert_ne` (in Nightly builds, run with -Z macro-backtrace for more info)
// error: aborting due to previous error
}
struct User {}
type UserId = kubetsu::Id<User, i64>;
let user_id = UserId::new(1);
// you can access original value reference with `inner()`.
assert_eq!(&1, user_id.inner());
// you can use `==` that have same value`.
assert_eq!(UserId::new(1), user_id);
You can serialize and deserialize as original value if you use [serde] crate and feature = "serde" enabled.
use kubetsu::Id;
#[derive(serde::Serialize, serde::Deserialize)]
struct User {
id: Id<Self, i64>
}
fn main() {
let user = User { id: Id::new(1) };
let str = serde_json::to_string(&user).unwrap();
assert_eq!("{\"id\":1}", str);
// you can deserialize
let _: User = serde_json::from_str(&str).unwrap();
}
You can encode and decode Id
value if you use sqlx crate and feature = "sqlx" enabled.
You can select "sqlx-xxxx" feature for each driver. ("sqlx-any", "sqlx-mysql", "sqlx-postgres", "sqlx-sqlite")
#[derive(sqlx::FromRow)]
struct User {
id: kubetsu::Id<Self, i64>
}
async fn do_something_with_sqlx(conn: sqlx::AnyPool) -> Result<(), sqlx::Error> {
let mut tx = conn.begin().await?;
let user: User = sqlx::query_as("SELECT 1 as `id`").fetch_one(&mut *tx).await?;
// do something with user
// you can also pass to bind
let user2: User = sqlx::query_as("SELECT 1 as `id` WHERE 1 = ?").bind(&user.id).fetch_one(&mut *tx).await?;
Ok(())
}
You can use Faker.fake()
if you use fake crate and feature = "fake" enabled.
use kubetsu::Id;
use fake::{Faker, Fake, Dummy};
#[derive(Dummy)]
struct User {
id: Id<Self, i64>
}
fn main() {
let user: User = Faker.fake();
// do something with user
}
$ cargo add kubetsu
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) see LICENSE
Copyright (c) 2024 Keiji Yoshimi