From 944b9412f440c72f0581ac6ce217e435a018177b Mon Sep 17 00:00:00 2001 From: kageru Date: Tue, 31 Jan 2023 11:47:59 +0100 Subject: [PATCH] properly display quoted queries --- src/main.rs | 12 ++++++------ src/parser.rs | 14 ++++++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7be0bfc..dfcca32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ static CARDS: LazyLock> = LazyLock::new(|| { .data }); static CARDS_BY_ID: LazyLock> = - LazyLock::new(|| CARDS.iter().map(|c| (c.id, Card { text: c.text.replace("\r", "").replace('\n', "
"), ..c.clone() })).collect()); + LazyLock::new(|| CARDS.iter().map(|c| (c.id, Card { text: c.text.replace('\r', "").replace('\n', "
"), ..c.clone() })).collect()); static SEARCH_CARDS: LazyLock> = LazyLock::new(|| CARDS.iter().map(SearchCard::from).collect()); #[actix_web::main] @@ -109,14 +109,14 @@ fn render_searchbox(res: &mut String, query: &Option) -> std::fmt::Resul "#, match &query { - Some(q) => q, - None => "", + Some(q) => q.replace('"', """), + None => String::new(), } ) } fn render_results(res: &mut String, query: &str) -> Result<(), Box> { - let query = match parser::parse_filters(query) { + let (raw_filters, query) = match parser::parse_filters(query) { Ok(q) => q, Err(e) => { write!(res, "Could not parse query: {e:?}")?; @@ -126,7 +126,7 @@ fn render_results(res: &mut String, query: &str) -> Result<(), Box = SEARCH_CARDS .iter() - .filter(|card| query.iter().all(|(_, q)| q(card))) + .filter(|card| query.iter().all(|q| q(card))) .map(|c| CARDS_BY_ID.get(&c.id).unwrap()) .take(RESULT_LIMIT) .collect(); @@ -134,7 +134,7 @@ fn render_results(res: &mut String, query: &str) -> Result<(), BoxShowing {} results where {} (took {:?})", matches.len(), - query.iter().map(|(f, _)| f.to_string()).join(" and "), + raw_filters.iter().map(|f| f.to_string()).join(" and "), now.elapsed() )?; if matches.is_empty() { diff --git a/src/parser.rs b/src/parser.rs index 4fedbc3..f6c9a06 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -14,11 +14,11 @@ use nom::{ IResult, }; -pub fn parse_filters(input: &str) -> Result, String> { +pub fn parse_filters(input: &str) -> Result<(Vec, Vec), String> { parse_raw_filters(input).map_err(|e| format!("Error while parsing filters “{input}”: {e:?}")).and_then(|(rest, mut v)| { if rest.is_empty() { v.sort_unstable_by_key(|RawCardFilter(f, _, _)| *f as u8); - v.into_iter().map(|r| build_filter(r.clone()).map(|f| (r, f))).collect() + Ok((v.clone(), v.into_iter().map(|r| build_filter(r)).collect::, _>>()?)) } else { Err(format!("Input was not fully parsed. Left over: “{rest}”")) } @@ -180,8 +180,14 @@ pub enum Value { impl Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self { - Self::String(s) => f.write_str(s), - Self::Numerical(n) => write!(f, "{}", n), + Self::String(s) => { + if s.contains(' ') { + write!(f, "\"{s}\"") + } else { + f.write_str(s) + } + } + Self::Numerical(n) => write!(f, "{n}"), } } }