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::timestamp::TimestampSecs;
use crate::{
define_newtype,
notetypes::NoteType,
types::{ObjID, Usn},
};
use rusqlite::{params, Connection, Row, NO_PARAMS};
use std::convert::TryInto;
define_newtype!(NoteID, i64);
#[derive(Debug)]
pub(super) struct Note {
pub id: ObjID,
pub id: NoteID,
pub mid: ObjID,
pub mtime: TimestampSecs,
pub usn: Usn,

View file

@ -1,14 +1,16 @@
// Copyright: Ankitects Pty Ltd and contributors
// 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_derive::Deserialize;
define_newtype!(NoteTypeID, i64);
#[derive(Deserialize, Debug)]
pub(crate) struct NoteType {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub id: ObjID,
pub id: NoteTypeID,
pub name: String,
#[serde(rename = "sortf")]
pub sort_field_idx: u16,

View file

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