From a17ddfdccd440cd2f7753b3873ccc96ecb8311d9 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 14 Apr 2020 09:58:30 +1000 Subject: [PATCH] native struct for CardTemplate --- proto/backend.proto | 2 +- rslib/src/notetype/mod.rs | 20 ++++-------- rslib/src/notetype/schema11.rs | 16 +++++---- rslib/src/notetype/stock.rs | 4 +-- rslib/src/notetype/templates.rs | 54 +++++++++++++++++++++++++++++++ rslib/src/storage/notetype/mod.rs | 8 ++--- 6 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 rslib/src/notetype/templates.rs diff --git a/proto/backend.proto b/proto/backend.proto index d2e0fb393..79a6f2225 100644 --- a/proto/backend.proto +++ b/proto/backend.proto @@ -513,7 +513,7 @@ message CardTemplateConfig { } message CardTemplate { - uint32 ord = 1; + OptionalUInt32 ord = 1; string name = 2; uint32 mtime_secs = 3; sint32 usn = 4; diff --git a/rslib/src/notetype/mod.rs b/rslib/src/notetype/mod.rs index 2a74d44c9..e142e290e 100644 --- a/rslib/src/notetype/mod.rs +++ b/rslib/src/notetype/mod.rs @@ -4,14 +4,16 @@ mod fields; mod schema11; mod stock; +mod templates; pub use crate::backend_proto::{ - card_requirement::CardRequirementKind, CardRequirement, CardTemplate, CardTemplateConfig, - NoteFieldConfig, NoteType as NoteTypeProto, NoteTypeConfig, NoteTypeKind, + card_requirement::CardRequirementKind, CardRequirement, CardTemplateConfig, NoteFieldConfig, + NoteType as NoteTypeProto, NoteTypeConfig, NoteTypeKind, }; pub use fields::NoteField; pub use schema11::{CardTemplateSchema11, NoteFieldSchema11, NoteTypeSchema11}; pub use stock::all_stock_notetypes; +pub use templates::CardTemplate; use crate::{ define_newtype, @@ -112,7 +114,7 @@ impl NoteType { .iter() .enumerate() .map(|(ord, tmpl)| { - let conf = tmpl.config.as_ref().unwrap(); + let conf = &tmpl.config; let normalized = without_legacy_template_directives(&conf.q_format); if let Ok(tmpl) = ParsedTemplate::from_text(normalized.as_ref()) { let mut req = match tmpl.requirements(&field_map) { @@ -167,15 +169,7 @@ impl NoteType { S2: Into, S3: Into, { - let mut config = CardTemplateConfig::default(); - config.q_format = qfmt.into(); - config.a_format = afmt.into(); - - let mut tmpl = CardTemplate::default(); - tmpl.name = name.into(); - tmpl.config = Some(config); - - self.templates.push(tmpl); + self.templates.push(CardTemplate::new(name, qfmt, afmt)); } pub(crate) fn prepare_for_adding(&mut self) { @@ -194,7 +188,7 @@ impl From for NoteTypeProto { usn: nt.usn.0, config: Some(nt.config), fields: nt.fields.into_iter().map(Into::into).collect(), - templates: nt.templates, + templates: nt.templates.into_iter().map(Into::into).collect(), } } } diff --git a/rslib/src/notetype/schema11.rs b/rslib/src/notetype/schema11.rs index 2e750301a..a7ae3e11f 100644 --- a/rslib/src/notetype/schema11.rs +++ b/rslib/src/notetype/schema11.rs @@ -276,11 +276,11 @@ pub struct CardTemplateSchema11 { impl From for CardTemplate { fn from(t: CardTemplateSchema11) -> Self { CardTemplate { - ord: t.ord as u32, + ord: Some(t.ord as u32), name: t.name, - mtime_secs: 0, - usn: 0, - config: Some(CardTemplateConfig { + mtime_secs: TimestampSecs(0), + usn: Usn(0), + config: CardTemplateConfig { q_format: t.qfmt, a_format: t.afmt, q_format_browser: t.bqfmt, @@ -289,17 +289,19 @@ impl From for CardTemplate { browser_font_name: t.bfont, browser_font_size: t.bsize as u32, other: other_to_bytes(&t.other), - }), + }, } } } +// fixme: make sure we don't call this when ord not set + impl From for CardTemplateSchema11 { fn from(p: CardTemplate) -> Self { - let conf = p.config.unwrap(); + let conf = p.config; CardTemplateSchema11 { name: p.name, - ord: p.ord as u16, + ord: p.ord.unwrap() as u16, qfmt: conf.q_format, afmt: conf.a_format, bqfmt: conf.q_format_browser, diff --git a/rslib/src/notetype/stock.rs b/rslib/src/notetype/stock.rs index 0b5e151cf..a610749ba 100644 --- a/rslib/src/notetype/stock.rs +++ b/rslib/src/notetype/stock.rs @@ -67,7 +67,7 @@ pub(crate) fn basic_typing(i18n: &I18n) -> NoteType { nt.name = i18n.tr(TR::NotetypesBasicTypeAnswerName).into(); let front = i18n.tr(TR::NotetypesFrontField); let back = i18n.tr(TR::NotetypesBackField); - let tmpl = nt.templates[0].config.as_mut().unwrap(); + let tmpl = &mut nt.templates[0].config; tmpl.q_format = format!("{}\n\n{{{{type:{}}}}}", fieldref(front.as_ref()), back); tmpl.a_format = format!( "{}\n\n
\n\n{{{{type:{}}}}}", @@ -101,7 +101,7 @@ pub(crate) fn basic_optional_reverse(i18n: &I18n) -> NoteType { nt.name = i18n.tr(TR::NotetypesBasicOptionalReversedName).into(); let addrev = i18n.tr(TR::NotetypesAddReverseField); nt.add_field(addrev.as_ref()); - let tmpl = nt.templates[1].config.as_mut().unwrap(); + let tmpl = &mut nt.templates[1].config; tmpl.q_format = format!("{{{{#{}}}}}{}{{{{/{}}}}}", addrev, tmpl.q_format, addrev); nt.prepare_for_adding(); nt diff --git a/rslib/src/notetype/templates.rs b/rslib/src/notetype/templates.rs new file mode 100644 index 000000000..954360264 --- /dev/null +++ b/rslib/src/notetype/templates.rs @@ -0,0 +1,54 @@ +// Copyright: Ankitects Pty Ltd and contributors +// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html + +use crate::{ + backend_proto::{CardTemplate as CardTemplateProto, CardTemplateConfig, OptionalUInt32}, + timestamp::TimestampSecs, + types::Usn, +}; + +pub struct CardTemplate { + pub ord: Option, + pub mtime_secs: TimestampSecs, + pub usn: Usn, + pub name: String, + pub config: CardTemplateConfig, +} + +impl From for CardTemplateProto { + fn from(t: CardTemplate) -> Self { + CardTemplateProto { + ord: t.ord.map(|n| OptionalUInt32 { val: n }), + mtime_secs: t.mtime_secs.0 as u32, + usn: t.usn.0, + name: t.name, + config: Some(t.config), + } + } +} + +impl CardTemplate { + pub fn new(name: S1, qfmt: S2, afmt: S3) -> Self + where + S1: Into, + S2: Into, + S3: Into, + { + CardTemplate { + ord: None, + name: name.into(), + mtime_secs: TimestampSecs(0), + usn: Usn(0), + config: CardTemplateConfig { + q_format: qfmt.into(), + a_format: afmt.into(), + q_format_browser: "".into(), + a_format_browser: "".into(), + target_deck_id: 0, + browser_font_name: "".into(), + browser_font_size: 0, + other: vec![], + }, + } + } +} diff --git a/rslib/src/storage/notetype/mod.rs b/rslib/src/storage/notetype/mod.rs index 7477f8fb8..0c77fc7a4 100644 --- a/rslib/src/storage/notetype/mod.rs +++ b/rslib/src/storage/notetype/mod.rs @@ -67,7 +67,7 @@ impl SqliteStorage { name: row.get(1)?, mtime_secs: row.get(2)?, usn: row.get(3)?, - config: Some(config), + config, }) })? .collect() @@ -130,11 +130,7 @@ impl SqliteStorage { .prepare_cached(include_str!("update_templates.sql"))?; for (ord, template) in templates.iter().enumerate() { let mut config_bytes = vec![]; - template - .config - .as_ref() - .unwrap() - .encode(&mut config_bytes)?; + template.config.encode(&mut config_bytes)?; stmt.execute(params![ ntid, ord as u32,