hide NativeName inner value, and require explicit accessors

This commit is contained in:
Damien Elmes 2021-04-18 09:29:35 +10:00
parent f924539574
commit 1acc679e8f
9 changed files with 26 additions and 19 deletions

View File

@ -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")

View File

@ -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 {

View File

@ -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"

View File

@ -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<N: Into<String>>(name: N) -> Self {
NativeDeckName(name.into())
}
pub fn from_human_name(name: &str) -> Self {
NativeDeckName(
name.split("::")

View File

@ -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())?;

View File

@ -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()
))

View File

@ -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

View File

@ -24,7 +24,7 @@ fn row_to_deck(row: &Row) -> Result<Deck> {
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;

View File

@ -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();