anki/rslib/src/types.rs

50 lines
1.6 KiB
Rust
Raw Normal View History

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
pub type ObjID = i64;
2020-03-26 04:06:02 +01:00
2020-03-26 04:50:20 +01:00
#[macro_export]
macro_rules! define_newtype {
( $name:ident, $type:ident ) => {
#[repr(transparent)]
2020-03-26 05:42:43 +01:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord,
serde::Serialize, serde::Deserialize)]
2020-03-26 04:50:20 +01:00
pub struct $name(pub $type);
2020-03-26 05:42:43 +01:00
impl std::fmt::Display for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl std::str::FromStr for $name {
type Err = std::num::ParseIntError;
fn from_str(s: &std::primitive::str) -> std::result::Result<Self, Self::Err> {
$type::from_str(s).map(|n| $name(n))
}
}
2020-03-26 04:50:20 +01:00
impl rusqlite::types::FromSql for $name {
fn column_result(
value: rusqlite::types::ValueRef<'_>,
) -> std::result::Result<Self, rusqlite::types::FromSqlError> {
if let rusqlite::types::ValueRef::Integer(i) = value {
Ok(Self(i as $type))
} else {
Err(rusqlite::types::FromSqlError::InvalidType)
}
}
}
impl rusqlite::ToSql for $name {
fn to_sql(&self) -> ::rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Ok(rusqlite::types::ToSqlOutput::Owned(
rusqlite::types::Value::Integer(self.0 as i64),
))
}
}
};
}
define_newtype!(Usn, i32);