mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
use a macro for newtype defs
This commit is contained in:
parent
39f916a23e
commit
5eed3d7f71
5 changed files with 33 additions and 79 deletions
|
@ -1,5 +1,3 @@
|
||||||
mod sqlite;
|
mod sqlite;
|
||||||
mod timestamp;
|
|
||||||
mod usn;
|
|
||||||
|
|
||||||
pub(crate) use sqlite::{SqliteStorage, StorageContext};
|
pub(crate) use sqlite::{SqliteStorage, StorageContext};
|
||||||
|
|
|
@ -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)))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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)))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,11 +1,11 @@
|
||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
|
use crate::define_newtype;
|
||||||
use std::time;
|
use std::time;
|
||||||
|
|
||||||
#[repr(transparent)]
|
define_newtype!(TimestampSecs, i64);
|
||||||
#[derive(Debug, Clone, Copy)]
|
define_newtype!(TimestampMillis, i64);
|
||||||
pub struct TimestampSecs(pub i64);
|
|
||||||
|
|
||||||
impl TimestampSecs {
|
impl TimestampSecs {
|
||||||
pub fn now() -> Self {
|
pub fn now() -> Self {
|
||||||
|
@ -13,10 +13,6 @@ impl TimestampSecs {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(transparent)]
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct TimestampMillis(pub i64);
|
|
||||||
|
|
||||||
impl TimestampMillis {
|
impl TimestampMillis {
|
||||||
pub fn now() -> Self {
|
pub fn now() -> Self {
|
||||||
Self(elapsed().as_millis() as i64)
|
Self(elapsed().as_millis() as i64)
|
||||||
|
|
|
@ -1,11 +1,35 @@
|
||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// 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;
|
pub type ObjID = i64;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[macro_export]
|
||||||
#[derive(Debug, Clone, Copy)]
|
macro_rules! define_newtype {
|
||||||
pub struct Usn(pub i32);
|
( $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);
|
||||||
|
|
Loading…
Reference in a new issue