From 39f916a23e64c79795de0ff9e0f6879dfd25459e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 26 Mar 2020 13:06:02 +1000 Subject: [PATCH] usn newtype --- rslib/src/notes.rs | 2 +- rslib/src/storage/mod.rs | 1 + rslib/src/storage/sqlite.rs | 2 +- rslib/src/storage/usn.rs | 24 ++++++++++++++++++++++++ rslib/src/timestamp.rs | 4 ++-- rslib/src/types.rs | 5 ++++- 6 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 rslib/src/storage/usn.rs diff --git a/rslib/src/notes.rs b/rslib/src/notes.rs index 7b70659bc..ffe047f4e 100644 --- a/rslib/src/notes.rs +++ b/rslib/src/notes.rs @@ -87,7 +87,7 @@ fn row_to_note(row: &Row) -> Result { pub(super) fn set_note(db: &Connection, note: &mut Note, note_type: &NoteType) -> Result<()> { note.mtime = TimestampSecs::now(); // hard-coded for now - note.usn = -1; + note.usn = Usn(-1); let field1_nohtml = strip_html_preserving_image_filenames(¬e.fields()[0]); let csum = field_checksum(field1_nohtml.as_ref()); let sort_field = if note_type.sort_field_idx == 0 { diff --git a/rslib/src/storage/mod.rs b/rslib/src/storage/mod.rs index a84013282..1fe26fcb7 100644 --- a/rslib/src/storage/mod.rs +++ b/rslib/src/storage/mod.rs @@ -1,4 +1,5 @@ mod sqlite; mod timestamp; +mod usn; pub(crate) use sqlite::{SqliteStorage, StorageContext}; diff --git a/rslib/src/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index 083b55e00..6b1a47057 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -297,7 +297,7 @@ impl StorageContext<'_> { } Ok(*self.usn.as_ref().unwrap()) } else { - Ok(-1) + Ok(Usn(-1)) } } diff --git a/rslib/src/storage/usn.rs b/rslib/src/storage/usn.rs new file mode 100644 index 000000000..d911604af --- /dev/null +++ b/rslib/src/storage/usn.rs @@ -0,0 +1,24 @@ +// 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 { + 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> { + Ok(ToSqlOutput::Owned(Value::Integer(self.0 as i64))) + } +} diff --git a/rslib/src/timestamp.rs b/rslib/src/timestamp.rs index 35089b36f..79d86685a 100644 --- a/rslib/src/timestamp.rs +++ b/rslib/src/timestamp.rs @@ -4,7 +4,7 @@ use std::time; #[repr(transparent)] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct TimestampSecs(pub i64); impl TimestampSecs { @@ -14,7 +14,7 @@ impl TimestampSecs { } #[repr(transparent)] -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub struct TimestampMillis(pub i64); impl TimestampMillis { diff --git a/rslib/src/types.rs b/rslib/src/types.rs index 51d66858c..8912fd3cf 100644 --- a/rslib/src/types.rs +++ b/rslib/src/types.rs @@ -5,4 +5,7 @@ // signed integers, so these numbers are signed as well. pub type ObjID = i64; -pub type Usn = i32; + +#[repr(transparent)] +#[derive(Debug, Clone, Copy)] +pub struct Usn(pub i32);