return tags as a string list directly; we don't need usn or collapse state

This commit is contained in:
Damien Elmes 2021-02-02 18:32:07 +10:00
parent 52b256663f
commit 8e0f69b71c
4 changed files with 20 additions and 49 deletions

View File

@ -13,7 +13,7 @@ from __future__ import annotations
import pprint
import re
from typing import Collection, List, Match, Optional, Sequence, Tuple
from typing import Collection, List, Match, Optional, Sequence
import anki # pylint: disable=unused-import
import anki._backend.backend_pb2 as _pb
@ -28,19 +28,15 @@ class TagManager:
def __init__(self, col: anki.collection.Collection) -> None:
self.col = col.weakref()
# all tags
# legacy add-on code expects a List return type
def all(self) -> List[str]:
return [t.name for t in self.col._backend.all_tags()]
return list(self.col._backend.all_tags())
def __repr__(self) -> str:
d = dict(self.__dict__)
del d["col"]
return f"{super().__repr__()} {pprint.pformat(d, width=300)}"
# # List of (tag, usn)
def allItems(self) -> List[Tuple[str, int]]:
return [(t.name, t.usn) for t in self.col._backend.all_tags()]
def tree(self) -> TagTreeNode:
return self.col._backend.tag_tree()

View File

@ -41,6 +41,10 @@ message Bool {
bool val = 1;
}
message StringList {
repeated string vals = 1;
}
// IDs used in RPC calls
///////////////////////////////////////////////////////////
@ -212,7 +216,7 @@ service BackendService {
// tags
rpc ClearUnusedTags(Empty) returns (Empty);
rpc AllTags(Empty) returns (AllTagsOut);
rpc AllTags(Empty) returns (StringList);
rpc SetTagCollapsed(SetTagCollapsedIn) returns (Empty);
rpc ClearTag(String) returns (Empty);
rpc TagTree(Empty) returns (TagTreeNode);
@ -842,21 +846,11 @@ message AddOrUpdateDeckConfigLegacyIn {
bool preserve_usn_and_mtime = 2;
}
message AllTagsOut {
repeated Tag tags = 1;
}
message SetTagCollapsedIn {
string name = 1;
bool collapsed = 2;
}
message Tag {
string name = 1;
sint32 usn = 2;
bool collapsed = 3;
}
message GetChangedTagsOut {
repeated string tags = 1;
}

View File

@ -1399,16 +1399,17 @@ impl BackendService for Backend {
// tags
//-------------------------------------------------------------------
fn all_tags(&self, _input: Empty) -> BackendResult<pb::AllTagsOut> {
let tags: Vec<pb::Tag> = self.with_col(|col| {
Ok(col
.storage
.all_tags()?
.into_iter()
.map(|t| t.into())
.collect())
})?;
Ok(pb::AllTagsOut { tags })
fn all_tags(&self, _input: Empty) -> BackendResult<pb::StringList> {
Ok(pb::StringList {
vals: self.with_col(|col| {
Ok(col
.storage
.all_tags()?
.into_iter()
.map(|t| t.name)
.collect())
})?,
})
}
fn set_tag_collapsed(&self, input: pb::SetTagCollapsedIn) -> BackendResult<pb::Empty> {

View File

@ -2,7 +2,7 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use crate::{
backend_proto::{Tag as TagProto, TagTreeNode},
backend_proto::TagTreeNode,
collection::Collection,
err::{AnkiError, Result},
notes::{NoteID, TransformNoteOutput},
@ -21,26 +21,6 @@ pub struct Tag {
pub collapsed: bool,
}
impl From<Tag> for TagProto {
fn from(t: Tag) -> Self {
TagProto {
name: t.name,
usn: t.usn.0,
collapsed: t.collapsed,
}
}
}
impl From<TagProto> for Tag {
fn from(t: TagProto) -> Self {
Tag {
name: t.name,
usn: Usn(t.usn),
collapsed: t.collapsed,
}
}
}
impl Tag {
pub fn new(name: String, usn: Usn) -> Self {
Tag {