From 1acc679e8f540f54aa2bdfca2ccce625255bf39f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 18 Apr 2021 09:29:35 +1000 Subject: [PATCH] hide NativeName inner value, and require explicit accessors --- rslib/src/decks/add.rs | 6 +++--- rslib/src/decks/filtered.rs | 2 +- rslib/src/decks/mod.rs | 12 ++++++------ rslib/src/decks/name.rs | 6 +++++- rslib/src/decks/remove.rs | 3 ++- rslib/src/scheduler/filtered/mod.rs | 2 +- rslib/src/search/sqlwriter.rs | 6 ++++-- rslib/src/storage/deck/mod.rs | 6 +++--- rslib/src/sync/mod.rs | 2 +- 9 files changed, 26 insertions(+), 19 deletions(-) diff --git a/rslib/src/decks/add.rs b/rslib/src/decks/add.rs index 2fefb383c..6047046f1 100644 --- a/rslib/src/decks/add.rs +++ b/rslib/src/decks/add.rs @@ -84,7 +84,7 @@ impl Collection { pub(crate) fn recover_missing_deck(&mut self, did: DeckId, usn: Usn) -> Result<()> { let mut deck = Deck::new_normal(); deck.id = did; - deck.name = NativeDeckName(format!("recovered{}", did)); + deck.name = NativeDeckName::from_native_str(format!("recovered{}", did)); deck.set_modified(usn); self.add_or_update_single_deck_with_existing_id(&mut deck, usn) } @@ -95,7 +95,7 @@ impl Collection { /// Caller must have done necessarily validation on name. fn add_parent_deck(&mut self, machine_name: &str, usn: Usn) -> Result<()> { let mut deck = Deck::new_normal(); - deck.name = NativeDeckName(machine_name.into()); + deck.name = NativeDeckName::from_native_str(machine_name); deck.set_modified(usn); self.add_deck_undoable(&mut deck) } @@ -111,7 +111,7 @@ impl Collection { } let parent_count = parent_deck.name.components().count(); let need_create = parent_count != child_split.len() - 1; - deck.name = NativeDeckName(format!( + deck.name = NativeDeckName::from_native_str(format!( "{}\x1f{}", parent_deck.name, &child_split[parent_count..].join("\x1f") diff --git a/rslib/src/decks/filtered.rs b/rslib/src/decks/filtered.rs index e1a676328..ed797eb6a 100644 --- a/rslib/src/decks/filtered.rs +++ b/rslib/src/decks/filtered.rs @@ -23,7 +23,7 @@ impl Deck { filt.reschedule = true; Deck { id: DeckId(0), - name: NativeDeckName("".into()), + name: NativeDeckName::from_native_str(""), mtime_secs: TimestampSecs(0), usn: Usn(0), common: DeckCommon { diff --git a/rslib/src/decks/mod.rs b/rslib/src/decks/mod.rs index faf115962..f47cd3ca7 100644 --- a/rslib/src/decks/mod.rs +++ b/rslib/src/decks/mod.rs @@ -46,7 +46,7 @@ impl Deck { pub fn new_normal() -> Deck { Deck { id: DeckId(0), - name: NativeDeckName("".into()), + name: NativeDeckName::from_native_str(""), mtime_secs: TimestampSecs(0), usn: Usn(0), common: DeckCommon { @@ -207,7 +207,7 @@ mod test { let _ = col.get_or_create_normal_deck("foo::bar::baz")?; let mut top_deck = col.get_or_create_normal_deck("foo")?; - top_deck.name = NativeDeckName("other".into()); + top_deck.name = NativeDeckName::from_native_str("other"); col.add_or_update_deck(&mut top_deck)?; assert_eq!( sorted_names(&col), @@ -216,7 +216,7 @@ mod test { // should do the right thing in the middle of the tree as well let mut middle = col.get_or_create_normal_deck("other::bar")?; - middle.name = NativeDeckName("quux\x1ffoo".into()); + middle.name = NativeDeckName::from_native_str("quux\x1ffoo"); col.add_or_update_deck(&mut middle)?; assert_eq!( sorted_names(&col), @@ -229,7 +229,7 @@ mod test { // quux::foo -> quux::foo::baz::four // means quux::foo::baz2 should be quux::foo::baz::four::baz2 // and a new quux::foo should have been created - middle.name = NativeDeckName("quux\x1ffoo\x1fbaz\x1ffour".into()); + middle.name = NativeDeckName::from_native_str("quux\x1ffoo\x1fbaz\x1ffour"); col.add_or_update_deck(&mut middle)?; assert_eq!( sorted_names(&col), @@ -246,7 +246,7 @@ mod test { ); // should handle name conflicts - middle.name = NativeDeckName("other".into()); + middle.name = NativeDeckName::from_native_str("other"); col.add_or_update_deck(&mut middle)?; assert_eq!(middle.name.as_str(), "other+"); @@ -277,7 +277,7 @@ mod test { let mut col = open_test_collection(); let mut default = col.get_or_create_normal_deck("default")?; - default.name = NativeDeckName("one\x1ftwo".into()); + default.name = NativeDeckName::from_native_str("one\x1ftwo"); col.add_or_update_deck(&mut default)?; // create a non-default deck confusingly named "default" diff --git a/rslib/src/decks/name.rs b/rslib/src/decks/name.rs index 603bbe64c..41a05cbf0 100644 --- a/rslib/src/decks/name.rs +++ b/rslib/src/decks/name.rs @@ -5,9 +5,13 @@ use itertools::Itertools; use std::borrow::Cow; #[derive(Debug, Clone, PartialEq)] -pub struct NativeDeckName(pub String); +pub struct NativeDeckName(String); impl NativeDeckName { + pub fn from_native_str>(name: N) -> Self { + NativeDeckName(name.into()) + } + pub fn from_human_name(name: &str) -> Self { NativeDeckName( name.split("::") diff --git a/rslib/src/decks/remove.rs b/rslib/src/decks/remove.rs index 37b4ab627..befb770f8 100644 --- a/rslib/src/decks/remove.rs +++ b/rslib/src/decks/remove.rs @@ -37,7 +37,8 @@ impl Collection { // if the default deck is included, just ensure it's reset to the default // name, as we've already removed its cards let mut modified_default = deck.clone(); - modified_default.name = NativeDeckName(self.tr.deck_config_default_name().into()); + modified_default.name = + NativeDeckName::from_native_str(self.tr.deck_config_default_name()); self.prepare_deck_for_update(&mut modified_default, usn)?; modified_default.set_modified(usn); self.update_single_deck_undoable(&mut modified_default, deck.clone())?; diff --git a/rslib/src/scheduler/filtered/mod.rs b/rslib/src/scheduler/filtered/mod.rs index 153ec7cca..e39e1a25a 100644 --- a/rslib/src/scheduler/filtered/mod.rs +++ b/rslib/src/scheduler/filtered/mod.rs @@ -147,7 +147,7 @@ impl Collection { } fn get_next_filtered_deck_name(&self) -> NativeDeckName { - NativeDeckName(format!( + NativeDeckName::from_native_str(format!( "Filtered Deck {}", TimestampSecs::now().time_string() )) diff --git a/rslib/src/search/sqlwriter.rs b/rslib/src/search/sqlwriter.rs index ab133409d..10a5082fa 100644 --- a/rslib/src/search/sqlwriter.rs +++ b/rslib/src/search/sqlwriter.rs @@ -346,11 +346,13 @@ impl SqlWriter<'_> { .storage .get_deck(current_did)? .map(|d| d.name) - .unwrap_or_else(|| NativeDeckName("Default".into())) + .unwrap_or_else(|| NativeDeckName::from_native_str("Default")) .as_str(), ) } else { - NativeDeckName::from_human_name(&to_re(deck)).0 + NativeDeckName::from_human_name(&to_re(deck)) + .as_str() + .to_string() }; // convert to a regex that includes child decks diff --git a/rslib/src/storage/deck/mod.rs b/rslib/src/storage/deck/mod.rs index 1bbe43447..b0f34c5ee 100644 --- a/rslib/src/storage/deck/mod.rs +++ b/rslib/src/storage/deck/mod.rs @@ -24,7 +24,7 @@ fn row_to_deck(row: &Row) -> Result { let id = row.get(0)?; Ok(Deck { id, - name: NativeDeckName(row.get(1)?), + name: NativeDeckName::from_native_str(row.get_raw(1).as_str()?), mtime_secs: row.get(2)?, usn: row.get(3)?, common, @@ -346,7 +346,7 @@ impl SqliteStorage { let mut deck = Deck::new_normal(); deck.id.0 = 1; // fixme: separate key - deck.name = NativeDeckName(tr.deck_config_default_name().into()); + deck.name = NativeDeckName::from_native_str(tr.deck_config_default_name()); self.add_or_update_deck_with_existing_id(&deck) } @@ -361,7 +361,7 @@ impl SqliteStorage { deck.set_modified(usn); } loop { - let name = UniCase::new(deck.name.0.clone()); + let name = UniCase::new(deck.name.as_str().to_string()); if !names.contains(&name) { names.insert(name); break; diff --git a/rslib/src/sync/mod.rs b/rslib/src/sync/mod.rs index d0076d16e..cb145ecf7 100644 --- a/rslib/src/sync/mod.rs +++ b/rslib/src/sync/mod.rs @@ -1500,7 +1500,7 @@ mod test { })?; let mut deck = col2.storage.get_deck(deck.id)?.unwrap(); - deck.name = NativeDeckName("newer".into()); + deck.name = NativeDeckName::from_native_str("newer"); col2.add_or_update_deck(&mut deck)?; let mut nt = col2.storage.get_notetype(nt.id)?.unwrap();