bikeshedding: amount -> match_limit

+ convert from u32 in backend method
This commit is contained in:
Damien Elmes 2021-09-12 11:55:30 +10:00
parent e85c93f3e7
commit c9d3e5462e
4 changed files with 5 additions and 6 deletions

View file

@ -63,7 +63,7 @@ message FindAndReplaceTagRequest {
message CompleteTagRequest { message CompleteTagRequest {
// a partial tag, optionally delimited with :: // a partial tag, optionally delimited with ::
string input = 1; string input = 1;
uint32 amount = 2; uint32 match_limit = 2;
} }
message CompleteTagResponse { message CompleteTagResponse {

View file

@ -91,7 +91,7 @@ impl TagsService for Backend {
fn complete_tag(&self, input: pb::CompleteTagRequest) -> Result<pb::CompleteTagResponse> { fn complete_tag(&self, input: pb::CompleteTagRequest) -> Result<pb::CompleteTagResponse> {
self.with_col(|col| { self.with_col(|col| {
let tags = col.complete_tag(&input.input, input.amount)?; let tags = col.complete_tag(&input.input, input.match_limit as usize)?;
Ok(pb::CompleteTagResponse { tags }) Ok(pb::CompleteTagResponse { tags })
}) })
} }

View file

@ -2,19 +2,18 @@
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use regex::Regex; use regex::Regex;
use std::convert::TryInto;
use crate::prelude::*; use crate::prelude::*;
impl Collection { impl Collection {
pub fn complete_tag(&self, input: &str, amount: u32) -> Result<Vec<String>> { pub fn complete_tag(&self, input: &str, limit: usize) -> Result<Vec<String>> {
let filters: Vec<_> = input let filters: Vec<_> = input
.split("::") .split("::")
.map(component_to_regex) .map(component_to_regex)
.collect::<Result<_, _>>()?; .collect::<Result<_, _>>()?;
let mut tags = vec![]; let mut tags = vec![];
self.storage.get_tags_by_predicate(|tag| { self.storage.get_tags_by_predicate(|tag| {
if tags.len() <= amount.try_into().unwrap() && filters_match(&filters, tag) { if tags.len() <= limit && filters_match(&filters, tag) {
tags.push(tag.to_string()); tags.push(tag.to_string());
} }
// we only need the tag name // we only need the tag name

View file

@ -58,7 +58,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const data = await postRequest( const data = await postRequest(
"/_anki/completeTag", "/_anki/completeTag",
Tags.CompleteTagRequest.encode( Tags.CompleteTagRequest.encode(
Tags.CompleteTagRequest.create({ input, amount: 500 }) Tags.CompleteTagRequest.create({ input, matchLimit: 500 })
).finish() ).finish()
); );
const response = Tags.CompleteTagResponse.decode(data); const response = Tags.CompleteTagResponse.decode(data);