newtype NoteID

This commit is contained in:
Damien Elmes 2020-03-26 14:42:43 +10:00
parent 5eed3d7f71
commit 4ec30e412a
3 changed files with 23 additions and 4 deletions

View file

@ -7,15 +7,18 @@ use crate::err::{AnkiError, DBErrorKind, Result};
use crate::text::strip_html_preserving_image_filenames; use crate::text::strip_html_preserving_image_filenames;
use crate::timestamp::TimestampSecs; use crate::timestamp::TimestampSecs;
use crate::{ use crate::{
define_newtype,
notetypes::NoteType, notetypes::NoteType,
types::{ObjID, Usn}, types::{ObjID, Usn},
}; };
use rusqlite::{params, Connection, Row, NO_PARAMS}; use rusqlite::{params, Connection, Row, NO_PARAMS};
use std::convert::TryInto; use std::convert::TryInto;
define_newtype!(NoteID, i64);
#[derive(Debug)] #[derive(Debug)]
pub(super) struct Note { pub(super) struct Note {
pub id: ObjID, pub id: NoteID,
pub mid: ObjID, pub mid: ObjID,
pub mtime: TimestampSecs, pub mtime: TimestampSecs,
pub usn: Usn, pub usn: Usn,

View file

@ -1,14 +1,16 @@
// 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::types::ObjID; use crate::define_newtype;
use serde_aux::field_attributes::deserialize_number_from_string; use serde_aux::field_attributes::deserialize_number_from_string;
use serde_derive::Deserialize; use serde_derive::Deserialize;
define_newtype!(NoteTypeID, i64);
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub(crate) struct NoteType { pub(crate) struct NoteType {
#[serde(deserialize_with = "deserialize_number_from_string")] #[serde(deserialize_with = "deserialize_number_from_string")]
pub id: ObjID, pub id: NoteTypeID,
pub name: String, pub name: String,
#[serde(rename = "sortf")] #[serde(rename = "sortf")]
pub sort_field_idx: u16, pub sort_field_idx: u16,

View file

@ -7,9 +7,23 @@ pub type ObjID = i64;
macro_rules! define_newtype { macro_rules! define_newtype {
( $name:ident, $type:ident ) => { ( $name:ident, $type:ident ) => {
#[repr(transparent)] #[repr(transparent)]
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord,
serde::Serialize, serde::Deserialize)]
pub struct $name(pub $type); pub struct $name(pub $type);
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))
}
}
impl rusqlite::types::FromSql for $name { impl rusqlite::types::FromSql for $name {
fn column_result( fn column_result(
value: rusqlite::types::ValueRef<'_>, value: rusqlite::types::ValueRef<'_>,