Files
jap_learn/src/data.rs
T

63 lines
1.3 KiB
Rust
Raw Normal View History

2026-08-24 11:35:37 +03:00
pub mod entities;
2026-08-25 15:46:39 +03:00
pub mod repositories;
2026-08-24 11:35:37 +03:00
2026-08-25 12:04:07 +03:00
use rusqlite::ToSql;
use rusqlite::types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef};
2026-08-18 12:47:06 +03:00
use std::fmt::{Display, Formatter};
2026-08-18 12:58:02 +03:00
use std::num::ParseIntError;
use std::str::FromStr;
2026-04-13 22:25:58 +03:00
2026-08-24 23:10:23 +03:00
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd, Default)]
2026-08-24 01:37:00 +03:00
pub struct Id(pub(crate) u32);
2026-08-18 12:47:06 +03:00
pub const INVALID_ID: Id = Id(0);
2026-08-18 12:58:02 +03:00
impl FromStr for Id {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Id(s.parse::<u32>()?))
}
}
2026-08-18 17:47:37 +03:00
impl From<u32> for Id {
fn from(value: u32) -> Self {
Id(value)
2026-08-18 12:58:02 +03:00
}
}
2026-08-20 00:18:16 +03:00
impl From<Id> for u32 {
fn from(value: Id) -> Self {
value.0
2026-08-18 12:58:02 +03:00
}
}
impl Display for Id {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromSql for Id {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Integer(i) => Ok(Id(i as u32)),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl ToSql for Id {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(self.0 as i64))
}
}
2026-08-20 16:22:38 +03:00
impl From<Id> for String {
fn from(value: Id) -> Self {
value.0.to_string()
}
}
2026-08-18 17:31:19 +03:00
impl Id {
pub(crate) fn is_valid(self) -> bool {
self.0 != 0
}
2026-08-25 12:04:07 +03:00
}