Write TableAccessQuerySet for other tables. Create Id macro

This commit is contained in:
micialware committed 2026-08-30 22:15:03 +03:00
1 parent 9a5b64a4fe
commit d9bbf9e705
8 files changed
+184 -57

No files matched your search

+53 -46
View File
@@ -7,57 +7,64 @@ use std::fmt::{write, Display, Formatter};
use std::num::ParseIntError;
use std::str::FromStr;
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd, Default)]
pub struct Id(pub(crate) u32);
pub const INVALID_ID: Id = Id(0);
impl FromStr for Id {
type Err = ParseIntError;
macro_rules! id_impl {
($id_type:ty, $name:ident, $invalid:ident) => {
#[derive(Clone, PartialEq, Eq, Debug, Hash, Copy, Ord, PartialOrd, Default)]
pub struct $name(pub(crate) $id_type);
pub const $invalid: $name = $name(0);
impl FromStr for $name {
type Err = ParseIntError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Id(s.parse::<u32>()?))
}
}
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok($name(s.parse::<$id_type>()?))
}
}
impl From<u32> for Id {
fn from(value: u32) -> Self {
Id(value)
}
}
impl From<$id_type> for $name {
fn from(value: $id_type) -> Self {
$name(value)
}
}
impl From<Id> for u32 {
fn from(value: Id) -> Self {
value.0
}
}
impl Display for Id {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<$name> for $id_type {
fn from(value: $name) -> Self {
value.0
}
}
impl Display for $name {
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 FromSql for $name {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Integer(i) => Ok($name(i as $id_type)),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl ToSql for $name {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(self.0 as i64))
}
}
impl From<$name> for String {
fn from(value: $name) -> Self {
value.0.to_string()
}
}
impl $name {
pub(crate) fn is_valid(self) -> bool {
self.0 != 0
}
}
}
}
impl ToSql for Id {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
Ok(ToSqlOutput::from(self.0 as i64))
}
}
impl From<Id> for String {
fn from(value: Id) -> Self {
value.0.to_string()
}
}
impl Id {
pub(crate) fn is_valid(self) -> bool {
self.0 != 0
}
}
id_impl!(u32, Id, INVALID_ID);
id_impl!(u16, SmallId, INVALID_SMALL_ID);