Fix format and tests

This commit is contained in:
RumovZ 2020-12-22 11:32:53 +01:00
parent b712053c06
commit 873e96b60a
3 changed files with 10 additions and 8 deletions

View file

@ -32,7 +32,7 @@ use crate::{
}, },
sched::cutoff::local_minutes_west_for_stamp, sched::cutoff::local_minutes_west_for_stamp,
sched::timespan::{answer_button_time, time_span}, sched::timespan::{answer_button_time, time_span},
search::{SortMode, norm_search}, search::{norm_search, SortMode},
stats::studied_today, stats::studied_today,
sync::{ sync::{
get_remote_sync_meta, sync_abort, sync_login, FullSyncProgress, NormalSyncProgress, get_remote_sync_meta, sync_abort, sync_login, FullSyncProgress, NormalSyncProgress,

View file

@ -1,8 +1,8 @@
mod cards; mod cards;
mod notes; mod notes;
mod parser; mod parser;
mod writer;
mod sqlwriter; mod sqlwriter;
mod writer;
pub use cards::SortMode; pub use cards::SortMode;
pub use writer::norm_search; pub use writer::norm_search;

View file

@ -2,8 +2,8 @@
// 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 crate::{ use crate::{
err::Result,
decks::DeckID as DeckIDType, decks::DeckID as DeckIDType,
err::Result,
notetype::NoteTypeID as NoteTypeIDType, notetype::NoteTypeID as NoteTypeIDType,
search::parser::{parse, Node, PropertyKind, SearchNode, StateKind, TemplateKind}, search::parser::{parse, Node, PropertyKind, SearchNode, StateKind, TemplateKind},
}; };
@ -12,8 +12,8 @@ pub fn norm_search(input: &str) -> Result<String> {
Ok(write_nodes(&parse(input)?)) Ok(write_nodes(&parse(input)?))
} }
fn write_nodes(nodes: &Vec<Node>) -> String { fn write_nodes(nodes: &[Node]) -> String {
nodes.into_iter().map(|node| write_node(node)).collect() nodes.iter().map(|node| write_node(node)).collect()
} }
fn write_node(node: &Node) -> String { fn write_node(node: &Node) -> String {
@ -41,7 +41,9 @@ fn write_search_node(node: &SearchNode) -> String {
NoteType(s) => format!("note:{}", &escape_suffix(s)), NoteType(s) => format!("note:{}", &escape_suffix(s)),
Rated { days, ease } => write_rated(days, ease), Rated { days, ease } => write_rated(days, ease),
Tag(s) => format!("tag:{}", &escape_suffix(s)), Tag(s) => format!("tag:{}", &escape_suffix(s)),
Duplicates { note_type_id, text } => format!("dupes:{},{}", note_type_id, escape_suffix(text)), Duplicates { note_type_id, text } => {
format!("dupes:{},{}", note_type_id, escape_suffix(text))
}
State(k) => write_state(k), State(k) => write_state(k),
Flag(u) => format!("flag:{}", u), Flag(u) => format!("flag:{}", u),
NoteIDs(s) => format!("nid:{}", s), NoteIDs(s) => format!("nid:{}", s),
@ -58,7 +60,7 @@ fn escape(txt: &str) -> String {
let txt = txt.replace("\"", "\\\"").replace(":", "\\:"); let txt = txt.replace("\"", "\\\"").replace(":", "\\:");
if txt.chars().any(|c| " \u{3000}()".contains(c)) { if txt.chars().any(|c| " \u{3000}()".contains(c)) {
format!(r#""{}""#, txt) format!(r#""{}""#, txt)
} else if txt.chars().next() == Some('-') { } else if txt.starts_with('-') {
format!("\\{}", txt) format!("\\{}", txt)
} else { } else {
txt txt
@ -80,7 +82,7 @@ fn write_single_field(field: &str, text: &str, is_re: bool) -> String {
let txt = format!("{}:{}{}", field, re, text).replace("\"", "\\\""); let txt = format!("{}:{}{}", field, re, text).replace("\"", "\\\"");
if txt.chars().any(|c| " \u{3000}()".contains(c)) { if txt.chars().any(|c| " \u{3000}()".contains(c)) {
format!(r#""{}""#, txt) format!(r#""{}""#, txt)
} else if txt.chars().next() == Some('-') { } else if txt.starts_with('-') {
format!("\\{}", txt) format!("\\{}", txt)
} else { } else {
txt txt