mirror of https://git.meli.delivery/meli/meli
ui: add query parsers
parent
8488ce21bf
commit
749d453f00
@ -0,0 +1,334 @@
|
||||
/*
|
||||
* meli - melib crate.
|
||||
*
|
||||
* Copyright 2019 Manos Pitsidianakis
|
||||
*
|
||||
* This file is part of meli.
|
||||
*
|
||||
* meli is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* meli is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::structs::StackVec;
|
||||
|
||||
pub type Result<'a, Output> = std::result::Result<(&'a str, Output), &'a str>;
|
||||
|
||||
pub trait Parser<'a, Output> {
|
||||
fn parse(&self, input: &'a str) -> Result<'a, Output>;
|
||||
|
||||
fn parse_complete(&self, input: &'a str) -> Result<'a, Output> {
|
||||
match self.parse(input) {
|
||||
r @ Ok(("", _)) => r,
|
||||
r @ Err(_) => r,
|
||||
Ok(_) => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
fn map<F, NewOutput>(self, map_fn: F) -> BoxedParser<'a, NewOutput>
|
||||
where
|
||||
Self: Sized + 'a,
|
||||
Output: 'a,
|
||||
NewOutput: 'a,
|
||||
F: Fn(Output) -> NewOutput + 'a,
|
||||
{
|
||||
BoxedParser::new(map(self, map_fn))
|
||||
}
|
||||
|
||||
fn and_then<F, NextParser, NewOutput>(self, f: F) -> BoxedParser<'a, NewOutput>
|
||||
where
|
||||
Self: Sized + 'a,
|
||||
Output: 'a,
|
||||
NewOutput: 'a,
|
||||
NextParser: Parser<'a, NewOutput> + 'a,
|
||||
F: Fn(Output) -> NextParser + 'a,
|
||||
{
|
||||
BoxedParser::new(and_then(self, f))
|
||||
}
|
||||
|
||||
fn seq<P, NewOutput>(self, p: P) -> BoxedParser<'a, NewOutput>
|
||||
where
|
||||
Self: Sized + 'a,
|
||||
Output: 'a,
|
||||
NewOutput: 'a,
|
||||
P: Parser<'a, NewOutput> + 'a,
|
||||
{
|
||||
BoxedParser::new(seq(self, p))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn left<'a, P1, P2, R1, R2>(parser1: P1, parser2: P2) -> impl Parser<'a, R1>
|
||||
where
|
||||
P1: Parser<'a, R1>,
|
||||
P2: Parser<'a, R2>,
|
||||
{
|
||||
map(pair(parser1, parser2), |(left, _right)| left)
|
||||
}
|
||||
|
||||
pub fn right<'a, P1, P2, R1, R2>(parser1: P1, parser2: P2) -> impl Parser<'a, R2>
|
||||
where
|
||||
P1: Parser<'a, R1>,
|
||||
P2: Parser<'a, R2>,
|
||||
{
|
||||
map(pair(parser1, parser2), |(_left, right)| right)
|
||||
}
|
||||
|
||||
impl<'a, F, Output> Parser<'a, Output> for F
|
||||
where
|
||||
F: Fn(&'a str) -> Result<Output>,
|
||||
{
|
||||
fn parse(&self, input: &'a str) -> Result<'a, Output> {
|
||||
self(input)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn map<'a, P, F, A, B>(parser: P, map_fn: F) -> impl Parser<'a, B>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
F: Fn(A) -> B,
|
||||
{
|
||||
move |input| {
|
||||
parser
|
||||
.parse(input)
|
||||
.map(|(next_input, result)| (next_input, map_fn(result)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_literal<'a>(expected: &'static str) -> impl Parser<'a, ()> {
|
||||
move |input: &'a str| match input.get(0..expected.len()) {
|
||||
Some(next) if next == expected => Ok((&input[expected.len()..], ())),
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn match_literal_anycase<'a>(expected: &'static str) -> impl Parser<'a, ()> {
|
||||
move |input: &'a str| match input.get(0..expected.len()) {
|
||||
Some(next) if next.eq_ignore_ascii_case(expected) => Ok((&input[expected.len()..], ())),
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn one_or_more<'a, P, A>(parser: P) -> impl Parser<'a, Vec<A>>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
{
|
||||
move |mut input| {
|
||||
let mut result = Vec::new();
|
||||
|
||||
if let Ok((next_input, first_item)) = parser.parse(input) {
|
||||
input = next_input;
|
||||
result.push(first_item);
|
||||
} else {
|
||||
return Err(input);
|
||||
}
|
||||
|
||||
while let Ok((next_input, next_item)) = parser.parse(input) {
|
||||
input = next_input;
|
||||
result.push(next_item);
|
||||
}
|
||||
|
||||
Ok((input, result))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn zero_or_more<'a, P, A>(parser: P) -> impl Parser<'a, Vec<A>>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
{
|
||||
move |mut input| {
|
||||
let mut result = Vec::new();
|
||||
|
||||
while let Ok((next_input, next_item)) = parser.parse(input) {
|
||||
input = next_input;
|
||||
result.push(next_item);
|
||||
}
|
||||
|
||||
Ok((input, result))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn pred<'a, P, A, F>(parser: P, predicate: F) -> impl Parser<'a, A>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
F: Fn(&A) -> bool,
|
||||
{
|
||||
move |input| {
|
||||
if let Ok((next_input, value)) = parser.parse(input) {
|
||||
if predicate(&value) {
|
||||
return Ok((next_input, value));
|
||||
}
|
||||
}
|
||||
Err(input)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn whitespace_char<'a>() -> impl Parser<'a, char> {
|
||||
pred(any_char, |c| c.is_whitespace())
|
||||
}
|
||||
|
||||
pub fn quoted_string<'a>() -> impl Parser<'a, String> {
|
||||
map(
|
||||
right(
|
||||
match_literal("\""),
|
||||
left(
|
||||
zero_or_more(pred(any_char, |c| *c != '"')),
|
||||
match_literal("\""),
|
||||
),
|
||||
),
|
||||
|chars| chars.into_iter().collect(),
|
||||
)
|
||||
}
|
||||
|
||||
pub struct BoxedParser<'a, Output> {
|
||||
parser: Box<dyn Parser<'a, Output> + 'a>,
|
||||
}
|
||||
|
||||
impl<'a, Output> BoxedParser<'a, Output> {
|
||||
fn new<P>(parser: P) -> Self
|
||||
where
|
||||
P: Parser<'a, Output> + 'a,
|
||||
{
|
||||
BoxedParser {
|
||||
parser: Box::new(parser),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Output> Parser<'a, Output> for BoxedParser<'a, Output> {
|
||||
fn parse(&self, input: &'a str) -> Result<'a, Output> {
|
||||
self.parser.parse(input)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn either<'a, P1, P2, A>(parser1: P1, parser2: P2) -> impl Parser<'a, A>
|
||||
where
|
||||
P1: Parser<'a, A>,
|
||||
P2: Parser<'a, A>,
|
||||
{
|
||||
move |input| match parser1.parse(input) {
|
||||
ok @ Ok(_) => ok,
|
||||
Err(_) => parser2.parse(input),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn whitespace_wrap<'a, P, A>(parser: P) -> impl Parser<'a, A>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
{
|
||||
right(space0(), left(parser, space0()))
|
||||
}
|
||||
|
||||
pub fn pair<'a, P1, P2, R1, R2>(parser1: P1, parser2: P2) -> impl Parser<'a, (R1, R2)>
|
||||
where
|
||||
P1: Parser<'a, R1>,
|
||||
P2: Parser<'a, R2>,
|
||||
{
|
||||
move |input| {
|
||||
parser1.parse(input).and_then(|(next_input, result1)| {
|
||||
parser2
|
||||
.parse(next_input)
|
||||
.map(|(last_input, result2)| (last_input, (result1, result2)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefix<'a, PN, P, R, RN>(pre: PN, parser: P) -> impl Parser<'a, R>
|
||||
where
|
||||
PN: Parser<'a, RN>,
|
||||
P: Parser<'a, R>,
|
||||
{
|
||||
move |input| {
|
||||
pre.parse(input).and_then(|(last_input, _)| {
|
||||
parser
|
||||
.parse(last_input)
|
||||
.map(|(rest, result)| (rest, result))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn suffix<'a, PN, P, R, RN>(parser: P, suf: PN) -> impl Parser<'a, R>
|
||||
where
|
||||
PN: Parser<'a, RN>,
|
||||
P: Parser<'a, R>,
|
||||
{
|
||||
move |input| {
|
||||
parser
|
||||
.parse(input)
|
||||
.and_then(|(last_input, result)| suf.parse(last_input).map(|(rest, _)| (rest, result)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn delimited<'a, PN, RN, P, R>(lparser: PN, mid: P, rparser: PN) -> impl Parser<'a, R>
|
||||
where
|
||||
PN: Parser<'a, RN>,
|
||||
P: Parser<'a, R>,
|
||||
{
|
||||
move |input| {
|
||||
lparser.parse(input).and_then(|(next_input, _)| {
|
||||
mid.parse(next_input).and_then(|(last_input, result)| {
|
||||
rparser.parse(last_input).map(|(rest, _)| (rest, result))
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn any_char(input: &str) -> Result<char> {
|
||||
match input.chars().next() {
|
||||
Some(next) => Ok((&input[next.len_utf8()..], next)),
|
||||
_ => Err(input),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn string<'a>() -> impl Parser<'a, String> {
|
||||
one_or_more(pred(any_char, |c| c.is_alphanumeric())).map(|r| r.into_iter().collect())
|
||||
}
|
||||
|
||||
pub fn space1<'a>() -> impl Parser<'a, Vec<char>> {
|
||||
one_or_more(whitespace_char())
|
||||
}
|
||||
|
||||
pub fn space0<'a>() -> impl Parser<'a, Vec<char>> {
|
||||
zero_or_more(whitespace_char())
|
||||
}
|
||||
|
||||
pub fn and_then<'a, P, F, A, B, NextP>(parser: P, f: F) -> impl Parser<'a, B>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
NextP: Parser<'a, B>,
|
||||
F: Fn(A) -> NextP,
|
||||
{
|
||||
move |input| match parser.parse(input) {
|
||||
Ok((next_input, result)) => f(result).parse(next_input),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn opt<'a, P, A>(opt_parser: P) -> impl Parser<'a, Option<A>>
|
||||
where
|
||||
P: Parser<'a, A>,
|
||||
{
|
||||
move |input| match opt_parser.parse(input) {
|
||||
Ok((next_input, result)) => Ok((next_input, Some(result))),
|
||||
Err(_) => Ok((input, None)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seq<'a, P1, P2, R1, R2>(parser1: P1, parser2: P2) -> impl Parser<'a, R2>
|
||||
where
|
||||
P1: Parser<'a, R1>,
|
||||
P2: Parser<'a, R2>,
|
||||
{
|
||||
move |input| match parser1.parse(input) {
|
||||
Ok((next_input, result)) => parser2.parse(next_input),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* meli - ui crate.
|
||||
*
|
||||
* Copyright 2017-2018 Manos Pitsidianakis
|
||||
*
|
||||
* This file is part of meli.
|
||||
*
|
||||
* meli is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* meli is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with meli. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
use crate::state::Context;
|
||||
use melib::{
|
||||
backends::FolderHash,
|
||||
email::{EnvelopeHash, Flag, UnixTimestamp},
|
||||
error::Result,
|
||||
thread::{SortField, SortOrder},
|
||||
StackVec,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Query {
|
||||
Before(UnixTimestamp),
|
||||
After(UnixTimestamp),
|
||||
Between(UnixTimestamp, UnixTimestamp),
|
||||
On(UnixTimestamp),
|
||||
/* * * * */
|
||||
From(String),
|
||||
To(String),
|
||||
Cc(String),
|
||||
Bcc(String),
|
||||
InReplyTo(String),
|
||||
References(String),
|
||||
AllAddresses(String),
|
||||
/* * * * */
|
||||
Body(String),
|
||||
Subject(String),
|
||||
AllText(String),
|
||||
/* * * * */
|
||||
Flag(Flag),
|
||||
}
|
||||
|
||||
pub fn filter(
|
||||
filter_term: &str,
|
||||
context: &Context,
|
||||
account_idx: usize,
|
||||
sort: (SortField, SortOrder),
|
||||
folder_hash: FolderHash,
|
||||
) -> Result<StackVec<EnvelopeHash>> {
|
||||
#[cfg(feature = "sqlite3")]
|
||||
{
|
||||
crate::sqlite3::search(filter_term, context, account_idx, sort, folder_hash)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "sqlite3"))]
|
||||
{
|
||||
let mut ret = StackVec::new();
|
||||
|
||||
let account = &context.accounts[account_idx];
|
||||
for env_hash in account.folders[folder_hash].as_result()?.envelopes {
|
||||
let envelope = &account.collection[&env_hash];
|
||||
if envelope.subject().contains(&filter_term) {
|
||||
ret.push(env_hash);
|
||||
continue;
|
||||
}
|
||||
if envelope.field_from_to_string().contains(&filter_term) {
|
||||
ret.push(env_hash);
|
||||
continue;
|
||||
}
|
||||
let op = account.operation(env_hash);
|
||||
let body = envelope.body(op)?;
|
||||
let decoded = decode_rec(&body, None);
|
||||
let body_text = String::from_utf8_lossy(&decoded);
|
||||
if body_text.contains(&filter_term) {
|
||||
ret.push(env_hash);
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue