diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 314b84383..470fc579d 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -599,7 +599,9 @@ impl Backend { SortMode::FromConfig }; let cids = search_cards(ctx, &input.search, order)?; - Ok(pb::SearchCardsOut { card_ids: cids }) + Ok(pb::SearchCardsOut { + card_ids: cids.into_iter().map(|v| v.0).collect(), + }) }) }) } diff --git a/rslib/src/card.rs b/rslib/src/card.rs index 1ee1386b0..c79d0958c 100644 --- a/rslib/src/card.rs +++ b/rslib/src/card.rs @@ -1,9 +1,11 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html +use crate::define_newtype; use num_enum::TryFromPrimitive; use serde_repr::{Deserialize_repr, Serialize_repr}; +define_newtype!(CardID, i64); #[derive(Serialize_repr, Deserialize_repr, Debug, PartialEq, TryFromPrimitive, Clone, Copy)] #[repr(u8)] pub enum CardType { diff --git a/rslib/src/config.rs b/rslib/src/config.rs index c768b1122..46c952828 100644 --- a/rslib/src/config.rs +++ b/rslib/src/config.rs @@ -1,7 +1,7 @@ // 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::decks::DeckID; use serde::Deserialize as DeTrait; use serde_aux::field_attributes::deserialize_number_from_string; use serde_derive::Deserialize; @@ -22,7 +22,7 @@ pub struct Config { rename = "curDeck", deserialize_with = "deserialize_number_from_string" )] - pub(crate) current_deck_id: ObjID, + pub(crate) current_deck_id: DeckID, pub(crate) rollover: Option, pub(crate) creation_offset: Option, pub(crate) local_offset: Option, diff --git a/rslib/src/decks.rs b/rslib/src/decks.rs index 16b80b261..2511d5af1 100644 --- a/rslib/src/decks.rs +++ b/rslib/src/decks.rs @@ -1,18 +1,21 @@ // 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!(DeckID, i64); +define_newtype!(DeckConfID, i64); + #[derive(Deserialize)] pub struct Deck { #[serde(deserialize_with = "deserialize_number_from_string")] - pub(crate) id: ObjID, + pub(crate) id: DeckID, pub(crate) name: String, } -pub(crate) fn child_ids<'a>(decks: &'a [Deck], name: &str) -> impl Iterator + 'a { +pub(crate) fn child_ids<'a>(decks: &'a [Deck], name: &str) -> impl Iterator + 'a { let prefix = format!("{}::", name.to_ascii_lowercase()); decks .iter() @@ -20,7 +23,7 @@ pub(crate) fn child_ids<'a>(decks: &'a [Deck], name: &str) -> impl Iterator Option<&Deck> { +pub(crate) fn get_deck(decks: &[Deck], id: DeckID) -> Option<&Deck> { for d in decks { if d.id == id { return Some(d); diff --git a/rslib/src/search/cards.rs b/rslib/src/search/cards.rs index 8ab045b7b..db9c8effa 100644 --- a/rslib/src/search/cards.rs +++ b/rslib/src/search/cards.rs @@ -2,12 +2,12 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html use super::{parser::Node, sqlwriter::node_to_sql}; +use crate::card::CardID; use crate::card::CardType; use crate::collection::RequestContext; use crate::config::SortKind; use crate::err::Result; use crate::search::parser::parse; -use crate::types::ObjID; use rusqlite::params; pub(crate) enum SortMode { @@ -21,7 +21,7 @@ pub(crate) fn search_cards<'a, 'b>( req: &'a mut RequestContext<'b>, search: &'a str, order: SortMode, -) -> Result> { +) -> Result> { let top_node = Node::Group(parse(search)?); let (sql, args) = node_to_sql(req, &top_node)?; @@ -50,7 +50,7 @@ pub(crate) fn search_cards<'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/storage/sqlite.rs b/rslib/src/storage/sqlite.rs index 036e42b5d..32a9d0524 100644 --- a/rslib/src/storage/sqlite.rs +++ b/rslib/src/storage/sqlite.rs @@ -3,6 +3,7 @@ use crate::collection::CollectionOp; use crate::config::Config; +use crate::decks::DeckID; use crate::err::Result; use crate::err::{AnkiError, DBErrorKind}; use crate::notetypes::NoteTypeID; @@ -12,7 +13,7 @@ use crate::{ notetypes::NoteType, sched::cutoff::{sched_timing_today, SchedTimingToday}, text::without_combining, - types::{ObjID, Usn}, + types::Usn, }; use regex::Regex; use rusqlite::{functions::FunctionFlags, params, Connection, NO_PARAMS}; @@ -302,7 +303,7 @@ impl StorageContext<'_> { } } - pub(crate) fn all_decks(&self) -> Result> { + pub(crate) fn all_decks(&self) -> Result> { self.db .query_row_and_then("select decks from col", NO_PARAMS, |row| -> Result<_> { Ok(serde_json::from_str(row.get_raw(0).as_str()?)?) diff --git a/rslib/src/types.rs b/rslib/src/types.rs index de4425e35..6d672d61e 100644 --- a/rslib/src/types.rs +++ b/rslib/src/types.rs @@ -1,8 +1,6 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -pub type ObjID = i64; - #[macro_export] macro_rules! define_newtype { ( $name:ident, $type:ident ) => {