mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
DeckID, CardID
This commit is contained in:
parent
f52e775354
commit
8abba00496
7 changed files with 20 additions and 14 deletions
|
@ -599,7 +599,9 @@ impl Backend {
|
||||||
SortMode::FromConfig
|
SortMode::FromConfig
|
||||||
};
|
};
|
||||||
let cids = search_cards(ctx, &input.search, order)?;
|
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(),
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
// 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::define_newtype;
|
||||||
use num_enum::TryFromPrimitive;
|
use num_enum::TryFromPrimitive;
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||||
|
|
||||||
|
define_newtype!(CardID, i64);
|
||||||
#[derive(Serialize_repr, Deserialize_repr, Debug, PartialEq, TryFromPrimitive, Clone, Copy)]
|
#[derive(Serialize_repr, Deserialize_repr, Debug, PartialEq, TryFromPrimitive, Clone, Copy)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum CardType {
|
pub enum CardType {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// 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::decks::DeckID;
|
||||||
use serde::Deserialize as DeTrait;
|
use serde::Deserialize as DeTrait;
|
||||||
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;
|
||||||
|
@ -22,7 +22,7 @@ pub struct Config {
|
||||||
rename = "curDeck",
|
rename = "curDeck",
|
||||||
deserialize_with = "deserialize_number_from_string"
|
deserialize_with = "deserialize_number_from_string"
|
||||||
)]
|
)]
|
||||||
pub(crate) current_deck_id: ObjID,
|
pub(crate) current_deck_id: DeckID,
|
||||||
pub(crate) rollover: Option<i8>,
|
pub(crate) rollover: Option<i8>,
|
||||||
pub(crate) creation_offset: Option<i32>,
|
pub(crate) creation_offset: Option<i32>,
|
||||||
pub(crate) local_offset: Option<i32>,
|
pub(crate) local_offset: Option<i32>,
|
||||||
|
|
|
@ -1,18 +1,21 @@
|
||||||
// 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!(DeckID, i64);
|
||||||
|
define_newtype!(DeckConfID, i64);
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct Deck {
|
pub struct Deck {
|
||||||
#[serde(deserialize_with = "deserialize_number_from_string")]
|
#[serde(deserialize_with = "deserialize_number_from_string")]
|
||||||
pub(crate) id: ObjID,
|
pub(crate) id: DeckID,
|
||||||
pub(crate) name: String,
|
pub(crate) name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn child_ids<'a>(decks: &'a [Deck], name: &str) -> impl Iterator<Item = ObjID> + 'a {
|
pub(crate) fn child_ids<'a>(decks: &'a [Deck], name: &str) -> impl Iterator<Item = DeckID> + 'a {
|
||||||
let prefix = format!("{}::", name.to_ascii_lowercase());
|
let prefix = format!("{}::", name.to_ascii_lowercase());
|
||||||
decks
|
decks
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -20,7 +23,7 @@ pub(crate) fn child_ids<'a>(decks: &'a [Deck], name: &str) -> impl Iterator<Item
|
||||||
.map(|d| d.id)
|
.map(|d| d.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_deck(decks: &[Deck], id: ObjID) -> Option<&Deck> {
|
pub(crate) fn get_deck(decks: &[Deck], id: DeckID) -> Option<&Deck> {
|
||||||
for d in decks {
|
for d in decks {
|
||||||
if d.id == id {
|
if d.id == id {
|
||||||
return Some(d);
|
return Some(d);
|
||||||
|
|
|
@ -2,12 +2,12 @@
|
||||||
// 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 super::{parser::Node, sqlwriter::node_to_sql};
|
use super::{parser::Node, sqlwriter::node_to_sql};
|
||||||
|
use crate::card::CardID;
|
||||||
use crate::card::CardType;
|
use crate::card::CardType;
|
||||||
use crate::collection::RequestContext;
|
use crate::collection::RequestContext;
|
||||||
use crate::config::SortKind;
|
use crate::config::SortKind;
|
||||||
use crate::err::Result;
|
use crate::err::Result;
|
||||||
use crate::search::parser::parse;
|
use crate::search::parser::parse;
|
||||||
use crate::types::ObjID;
|
|
||||||
use rusqlite::params;
|
use rusqlite::params;
|
||||||
|
|
||||||
pub(crate) enum SortMode {
|
pub(crate) enum SortMode {
|
||||||
|
@ -21,7 +21,7 @@ pub(crate) fn search_cards<'a, 'b>(
|
||||||
req: &'a mut RequestContext<'b>,
|
req: &'a mut RequestContext<'b>,
|
||||||
search: &'a str,
|
search: &'a str,
|
||||||
order: SortMode,
|
order: SortMode,
|
||||||
) -> Result<Vec<ObjID>> {
|
) -> Result<Vec<CardID>> {
|
||||||
let top_node = Node::Group(parse(search)?);
|
let top_node = Node::Group(parse(search)?);
|
||||||
let (sql, args) = node_to_sql(req, &top_node)?;
|
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 mut stmt = req.storage.db.prepare(&sql)?;
|
||||||
let ids: Vec<i64> = stmt
|
let ids: Vec<_> = stmt
|
||||||
.query_map(&args, |row| row.get(0))?
|
.query_map(&args, |row| row.get(0))?
|
||||||
.collect::<std::result::Result<_, _>>()?;
|
.collect::<std::result::Result<_, _>>()?;
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
use crate::collection::CollectionOp;
|
use crate::collection::CollectionOp;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
use crate::decks::DeckID;
|
||||||
use crate::err::Result;
|
use crate::err::Result;
|
||||||
use crate::err::{AnkiError, DBErrorKind};
|
use crate::err::{AnkiError, DBErrorKind};
|
||||||
use crate::notetypes::NoteTypeID;
|
use crate::notetypes::NoteTypeID;
|
||||||
|
@ -12,7 +13,7 @@ use crate::{
|
||||||
notetypes::NoteType,
|
notetypes::NoteType,
|
||||||
sched::cutoff::{sched_timing_today, SchedTimingToday},
|
sched::cutoff::{sched_timing_today, SchedTimingToday},
|
||||||
text::without_combining,
|
text::without_combining,
|
||||||
types::{ObjID, Usn},
|
types::Usn,
|
||||||
};
|
};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rusqlite::{functions::FunctionFlags, params, Connection, NO_PARAMS};
|
use rusqlite::{functions::FunctionFlags, params, Connection, NO_PARAMS};
|
||||||
|
@ -302,7 +303,7 @@ impl StorageContext<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn all_decks(&self) -> Result<HashMap<ObjID, Deck>> {
|
pub(crate) fn all_decks(&self) -> Result<HashMap<DeckID, Deck>> {
|
||||||
self.db
|
self.db
|
||||||
.query_row_and_then("select decks from col", NO_PARAMS, |row| -> Result<_> {
|
.query_row_and_then("select decks from col", NO_PARAMS, |row| -> Result<_> {
|
||||||
Ok(serde_json::from_str(row.get_raw(0).as_str()?)?)
|
Ok(serde_json::from_str(row.get_raw(0).as_str()?)?)
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
// 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
|
||||||
|
|
||||||
pub type ObjID = i64;
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! define_newtype {
|
macro_rules! define_newtype {
|
||||||
( $name:ident, $type:ident ) => {
|
( $name:ident, $type:ident ) => {
|
||||||
|
|
Loading…
Reference in a new issue