mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 22:12:21 -04:00
Remove Default impl of Tag
This commit is contained in:
parent
9c1d7c522a
commit
ee3c019804
3 changed files with 20 additions and 43 deletions
|
@ -140,13 +140,7 @@ impl SqliteStorage {
|
||||||
let tags = self
|
let tags = self
|
||||||
.db
|
.db
|
||||||
.prepare_cached("select tag, usn from tags")?
|
.prepare_cached("select tag, usn from tags")?
|
||||||
.query_and_then(NO_PARAMS, |r| {
|
.query_and_then(NO_PARAMS, |r| Ok(Tag::new(r.get(0)?, r.get(1)?)))?
|
||||||
Ok(Tag {
|
|
||||||
name: r.get(0)?,
|
|
||||||
usn: r.get(1)?,
|
|
||||||
..Default::default()
|
|
||||||
})
|
|
||||||
})?
|
|
||||||
.collect::<Result<Vec<Tag>>>()?;
|
.collect::<Result<Vec<Tag>>>()?;
|
||||||
self.db
|
self.db
|
||||||
.execute_batch(include_str!["../upgrades/schema17_upgrade.sql"])?;
|
.execute_batch(include_str!["../upgrades/schema17_upgrade.sql"])?;
|
||||||
|
|
|
@ -888,11 +888,7 @@ impl Collection {
|
||||||
|
|
||||||
fn merge_tags(&self, tags: Vec<String>, latest_usn: Usn) -> Result<()> {
|
fn merge_tags(&self, tags: Vec<String>, latest_usn: Usn) -> Result<()> {
|
||||||
for tag in tags {
|
for tag in tags {
|
||||||
self.register_tag(Tag {
|
self.register_tag(Tag::new(tag, latest_usn))?;
|
||||||
name: tag,
|
|
||||||
usn: latest_usn,
|
|
||||||
..Default::default()
|
|
||||||
})?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,16 +46,6 @@ impl PartialEq for Tag {
|
||||||
|
|
||||||
impl Eq for Tag {}
|
impl Eq for Tag {}
|
||||||
|
|
||||||
impl Default for Tag {
|
|
||||||
fn default() -> Self {
|
|
||||||
Tag {
|
|
||||||
name: "".to_string(),
|
|
||||||
usn: Usn(-1),
|
|
||||||
collapsed: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<Tag> for TagProto {
|
impl From<Tag> for TagProto {
|
||||||
fn from(t: Tag) -> Self {
|
fn from(t: Tag) -> Self {
|
||||||
TagProto {
|
TagProto {
|
||||||
|
@ -76,6 +66,16 @@ impl From<TagProto> for Tag {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Tag {
|
||||||
|
pub fn new(name: String, usn: Usn) -> Self {
|
||||||
|
Tag {
|
||||||
|
name,
|
||||||
|
usn,
|
||||||
|
collapsed: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn split_tags(tags: &str) -> impl Iterator<Item = &str> {
|
pub(crate) fn split_tags(tags: &str) -> impl Iterator<Item = &str> {
|
||||||
tags.split(is_tag_separator).filter(|tag| !tag.is_empty())
|
tags.split(is_tag_separator).filter(|tag| !tag.is_empty())
|
||||||
}
|
}
|
||||||
|
@ -127,10 +127,7 @@ fn fill_missing_tags(tags: Vec<Tag>) -> Vec<Tag> {
|
||||||
let split: Vec<&str> = (&tag.name).split("::").collect();
|
let split: Vec<&str> = (&tag.name).split("::").collect();
|
||||||
for i in 0..split.len() - 1 {
|
for i in 0..split.len() - 1 {
|
||||||
let comp = split[0..i + 1].join("::");
|
let comp = split[0..i + 1].join("::");
|
||||||
let t = Tag {
|
let t = Tag::new(comp.to_owned(), Usn(-1));
|
||||||
name: comp.to_owned(),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
if filled_tags.get(&comp).is_none() {
|
if filled_tags.get(&comp).is_none() {
|
||||||
filled_tags.insert(comp, t);
|
filled_tags.insert(comp, t);
|
||||||
}
|
}
|
||||||
|
@ -199,13 +196,9 @@ impl Collection {
|
||||||
let mut seen = HashSet::new();
|
let mut seen = HashSet::new();
|
||||||
let mut added = false;
|
let mut added = false;
|
||||||
|
|
||||||
let mut tags: Vec<_> = tags.iter().flat_map(|t| split_tags(t)).collect();
|
let tags: Vec<_> = tags.iter().flat_map(|t| split_tags(t)).collect();
|
||||||
for tag in &mut tags {
|
for tag in tags {
|
||||||
let t = self.register_tag(Tag {
|
let t = self.register_tag(Tag::new(tag.to_string(), usn))?;
|
||||||
name: tag.to_string(),
|
|
||||||
usn,
|
|
||||||
..Default::default()
|
|
||||||
})?;
|
|
||||||
if t.0.name.is_empty() {
|
if t.0.name.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -226,6 +219,8 @@ impl Collection {
|
||||||
Ok((tags, added))
|
Ok((tags, added))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Register tag if it doesn't exist.
|
||||||
|
/// Returns a tuple of the tag with its name normalized and a boolean indicating if it was added.
|
||||||
pub(crate) fn register_tag(&self, tag: Tag) -> Result<(Tag, bool)> {
|
pub(crate) fn register_tag(&self, tag: Tag) -> Result<(Tag, bool)> {
|
||||||
let normalized_name = normalize_tag_name(&tag.name);
|
let normalized_name = normalize_tag_name(&tag.name);
|
||||||
let mut t = Tag {
|
let mut t = Tag {
|
||||||
|
@ -250,11 +245,7 @@ impl Collection {
|
||||||
self.storage.clear_tags()?;
|
self.storage.clear_tags()?;
|
||||||
}
|
}
|
||||||
for tag in split_tags(tags) {
|
for tag in split_tags(tags) {
|
||||||
let t = self.register_tag(Tag {
|
let t = self.register_tag(Tag::new(tag.to_string(), usn))?;
|
||||||
name: tag.to_string(),
|
|
||||||
usn,
|
|
||||||
..Default::default()
|
|
||||||
})?;
|
|
||||||
changed |= t.1;
|
changed |= t.1;
|
||||||
}
|
}
|
||||||
Ok(changed)
|
Ok(changed)
|
||||||
|
@ -263,11 +254,7 @@ impl Collection {
|
||||||
pub(crate) fn set_tag_collapsed(&self, name: &str, collapsed: bool) -> Result<()> {
|
pub(crate) fn set_tag_collapsed(&self, name: &str, collapsed: bool) -> Result<()> {
|
||||||
if self.storage.get_tag(name)?.is_none() {
|
if self.storage.get_tag(name)?.is_none() {
|
||||||
// tag is missing, register it
|
// tag is missing, register it
|
||||||
let t = Tag {
|
self.register_tag(Tag::new(name.to_string(), self.usn()?))?;
|
||||||
name: name.to_owned(),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
self.register_tag(t)?;
|
|
||||||
}
|
}
|
||||||
self.storage.set_tag_collapsed(name, collapsed)
|
self.storage.set_tag_collapsed(name, collapsed)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue