use a macro for newtype defs

This commit is contained in:
Damien Elmes 2020-03-26 13:50:20 +10:00
parent 39f916a23e
commit 5eed3d7f71
5 changed files with 33 additions and 79 deletions

View file

@ -1,5 +1,3 @@
mod sqlite;
mod timestamp;
mod usn;
pub(crate) use sqlite::{SqliteStorage, StorageContext};

View file

@ -1,40 +0,0 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::timestamp::{TimestampMillis, TimestampSecs};
use rusqlite::{
types::{FromSql, FromSqlError, ToSqlOutput, Value, ValueRef},
ToSql,
};
impl FromSql for TimestampSecs {
fn column_result(value: ValueRef<'_>) -> std::result::Result<Self, FromSqlError> {
if let ValueRef::Integer(i) = value {
Ok(TimestampSecs(i))
} else {
Err(FromSqlError::InvalidType)
}
}
}
impl ToSql for TimestampSecs {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Ok(ToSqlOutput::Owned(Value::Integer(self.0)))
}
}
impl FromSql for TimestampMillis {
fn column_result(value: ValueRef<'_>) -> std::result::Result<Self, FromSqlError> {
if let ValueRef::Integer(i) = value {
Ok(TimestampMillis(i))
} else {
Err(FromSqlError::InvalidType)
}
}
}
impl ToSql for TimestampMillis {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Ok(ToSqlOutput::Owned(Value::Integer(self.0)))
}
}

View file

@ -1,24 +0,0 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::types::Usn;
use rusqlite::{
types::{FromSql, FromSqlError, ToSqlOutput, Value, ValueRef},
ToSql,
};
impl FromSql for Usn {
fn column_result(value: ValueRef<'_>) -> std::result::Result<Self, FromSqlError> {
if let ValueRef::Integer(i) = value {
Ok(Self(i as i32))
} else {
Err(FromSqlError::InvalidType)
}
}
}
impl ToSql for Usn {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
Ok(ToSqlOutput::Owned(Value::Integer(self.0 as i64)))
}
}

View file

@ -1,11 +1,11 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::define_newtype;
use std::time;
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct TimestampSecs(pub i64);
define_newtype!(TimestampSecs, i64);
define_newtype!(TimestampMillis, i64);
impl TimestampSecs {
pub fn now() -> Self {
@ -13,10 +13,6 @@ impl TimestampSecs {
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct TimestampMillis(pub i64);
impl TimestampMillis {
pub fn now() -> Self {
Self(elapsed().as_millis() as i64)

View file

@ -1,11 +1,35 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
// while Anki tends to only use positive numbers, sqlite only supports
// signed integers, so these numbers are signed as well.
pub type ObjID = i64;
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct Usn(pub i32);
#[macro_export]
macro_rules! define_newtype {
( $name:ident, $type:ident ) => {
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct $name(pub $type);
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);