From f52e775354430186dbebcbde66b2bb6eeaf721b9 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 26 Mar 2020 15:00:24 +1000 Subject: [PATCH] NoteTypeID --- rslib/src/backend/mod.rs | 4 +++- rslib/src/media/check.rs | 2 +- rslib/src/notes.rs | 13 +++++-------- rslib/src/search/notes.rs | 6 +++--- rslib/src/search/parser.rs | 8 ++++---- rslib/src/search/sqlwriter.rs | 7 +++---- rslib/src/storage/sqlite.rs | 8 +++++--- rslib/src/types.rs | 16 +++++++++++++--- 8 files changed, 37 insertions(+), 27 deletions(-) diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 0240d8e9d..314b84383 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -608,7 +608,9 @@ impl Backend { self.with_col(|col| { col.with_ctx(|ctx| { let nids = search_notes(ctx, &input.search)?; - Ok(pb::SearchNotesOut { note_ids: nids }) + Ok(pb::SearchNotesOut { + note_ids: nids.into_iter().map(|v| v.0).collect(), + }) }) }) } diff --git a/rslib/src/media/check.rs b/rslib/src/media/check.rs index 971747b50..dd1c4bde3 100644 --- a/rslib/src/media/check.rs +++ b/rslib/src/media/check.rs @@ -390,7 +390,7 @@ where self.maybe_fire_progress_cb()?; } let nt = note_types - .get(¬e.mid) + .get(¬e.ntid) .ok_or_else(|| AnkiError::DBError { info: "missing note type".to_string(), kind: DBErrorKind::MissingEntity, diff --git a/rslib/src/notes.rs b/rslib/src/notes.rs index 68279b3a1..74f69200f 100644 --- a/rslib/src/notes.rs +++ b/rslib/src/notes.rs @@ -4,13 +4,10 @@ /// At the moment, this is just basic note reading/updating functionality for /// the media DB check. use crate::err::{AnkiError, DBErrorKind, Result}; +use crate::notetypes::NoteTypeID; use crate::text::strip_html_preserving_image_filenames; use crate::timestamp::TimestampSecs; -use crate::{ - define_newtype, - notetypes::NoteType, - types::{ObjID, Usn}, -}; +use crate::{define_newtype, notetypes::NoteType, types::Usn}; use rusqlite::{params, Connection, Row, NO_PARAMS}; use std::convert::TryInto; @@ -19,7 +16,7 @@ define_newtype!(NoteID, i64); #[derive(Debug)] pub(super) struct Note { pub id: NoteID, - pub mid: ObjID, + pub ntid: NoteTypeID, pub mtime: TimestampSecs, pub usn: Usn, fields: Vec, @@ -51,7 +48,7 @@ pub(crate) fn field_checksum(text: &str) -> u32 { } #[allow(dead_code)] -fn get_note(db: &Connection, nid: ObjID) -> Result> { +fn get_note(db: &Connection, nid: NoteID) -> Result> { let mut stmt = db.prepare_cached("select id, mid, mod, usn, flds from notes where id=?")?; let note = stmt.query_and_then(params![nid], row_to_note)?.next(); @@ -75,7 +72,7 @@ pub(super) fn for_every_note Result<()>>( fn row_to_note(row: &Row) -> Result { Ok(Note { id: row.get(0)?, - mid: row.get(1)?, + ntid: row.get(1)?, mtime: row.get(2)?, usn: row.get(3)?, fields: row diff --git a/rslib/src/search/notes.rs b/rslib/src/search/notes.rs index 50021a92e..5af735809 100644 --- a/rslib/src/search/notes.rs +++ b/rslib/src/search/notes.rs @@ -4,13 +4,13 @@ use super::{parser::Node, sqlwriter::node_to_sql}; use crate::collection::RequestContext; use crate::err::Result; +use crate::notes::NoteID; use crate::search::parser::parse; -use crate::types::ObjID; pub(crate) fn search_notes<'a, 'b>( req: &'a mut RequestContext<'b>, search: &'a str, -) -> Result> { +) -> Result> { let top_node = Node::Group(parse(search)?); let (sql, args) = node_to_sql(req, &top_node)?; @@ -20,7 +20,7 @@ pub(crate) fn search_notes<'a, 'b>( ); let mut stmt = req.storage.db.prepare(&sql)?; - let ids: Vec = stmt + let ids: Vec<_> = stmt .query_map(&args, |row| row.get(0))? .collect::>()?; diff --git a/rslib/src/search/parser.rs b/rslib/src/search/parser.rs index 4018cc56c..a42370152 100644 --- a/rslib/src/search/parser.rs +++ b/rslib/src/search/parser.rs @@ -2,7 +2,7 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html use crate::err::{AnkiError, Result}; -use crate::types::ObjID; +use crate::notetypes::NoteTypeID; use nom::branch::alt; use nom::bytes::complete::{escaped, is_not, tag, take_while1}; use nom::character::complete::{anychar, char, one_of}; @@ -58,7 +58,7 @@ pub(super) enum SearchNode<'a> { AddedInDays(u32), CardTemplate(TemplateKind), Deck(Cow<'a, str>), - NoteTypeID(ObjID), + NoteTypeID(NoteTypeID), NoteType(Cow<'a, str>), Rated { days: u32, @@ -66,7 +66,7 @@ pub(super) enum SearchNode<'a> { }, Tag(Cow<'a, str>), Duplicates { - note_type_id: ObjID, + note_type_id: NoteTypeID, text: String, }, State(StateKind), @@ -339,7 +339,7 @@ fn parse_rated(val: &str) -> ParseResult> { /// eg dupes:1231,hello fn parse_dupes(val: &str) -> ParseResult> { let mut it = val.splitn(2, ','); - let mid: ObjID = it.next().unwrap().parse()?; + let mid: NoteTypeID = it.next().unwrap().parse()?; let text = it.next().ok_or(ParseError {})?; Ok(SearchNode::Duplicates { note_type_id: mid, diff --git a/rslib/src/search/sqlwriter.rs b/rslib/src/search/sqlwriter.rs index 1f5e7ef0c..f1348ba58 100644 --- a/rslib/src/search/sqlwriter.rs +++ b/rslib/src/search/sqlwriter.rs @@ -7,11 +7,10 @@ use crate::decks::child_ids; use crate::decks::get_deck; use crate::err::{AnkiError, Result}; use crate::notes::field_checksum; +use crate::notetypes::NoteTypeID; use crate::text::matches_wildcard; use crate::text::without_combining; -use crate::{ - collection::RequestContext, text::strip_html_preserving_image_filenames, types::ObjID, -}; +use crate::{collection::RequestContext, text::strip_html_preserving_image_filenames}; use std::fmt::Write; struct SqlWriter<'a, 'b> { @@ -342,7 +341,7 @@ impl SqlWriter<'_, '_> { Ok(()) } - fn write_dupes(&mut self, ntid: ObjID, text: &str) { + fn write_dupes(&mut self, ntid: NoteTypeID, text: &str) { let text_nohtml = strip_html_preserving_image_filenames(text); let csum = field_checksum(text_nohtml.as_ref()); write!( diff --git a/rslib/src/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index 6b1a47057..036e42b5d 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -5,6 +5,7 @@ use crate::collection::CollectionOp; use crate::config::Config; use crate::err::Result; use crate::err::{AnkiError, DBErrorKind}; +use crate::notetypes::NoteTypeID; use crate::timestamp::{TimestampMillis, TimestampSecs}; use crate::{ decks::Deck, @@ -315,11 +316,12 @@ impl StorageContext<'_> { }) } - pub(crate) fn all_note_types(&self) -> Result> { + pub(crate) fn all_note_types(&self) -> Result> { let mut stmt = self.db.prepare("select models from col")?; let note_types = stmt - .query_and_then(NO_PARAMS, |row| -> Result> { - let v: HashMap = serde_json::from_str(row.get_raw(0).as_str()?)?; + .query_and_then(NO_PARAMS, |row| -> Result> { + let v: HashMap = + serde_json::from_str(row.get_raw(0).as_str()?)?; Ok(v) })? .next() diff --git a/rslib/src/types.rs b/rslib/src/types.rs index 760d5a3e8..de4425e35 100644 --- a/rslib/src/types.rs +++ b/rslib/src/types.rs @@ -7,8 +7,18 @@ pub type ObjID = i64; macro_rules! define_newtype { ( $name:ident, $type:ident ) => { #[repr(transparent)] - #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, - serde::Serialize, serde::Deserialize)] + #[derive( + Debug, + Clone, + Copy, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, + serde::Serialize, + serde::Deserialize, + )] pub struct $name(pub $type); impl std::fmt::Display for $name { @@ -20,7 +30,7 @@ macro_rules! define_newtype { impl std::str::FromStr for $name { type Err = std::num::ParseIntError; fn from_str(s: &std::primitive::str) -> std::result::Result { - $type::from_str(s).map(|n| $name(n)) + $type::from_str(s).map($name) } }