Parse arbitrary commands

This commit is contained in:
Joscha 2022-01-14 22:08:27 +01:00
parent fe1bf309b8
commit 0dcb75b103
6 changed files with 37 additions and 23 deletions

View file

@ -8,7 +8,7 @@ use directories::ProjectDirs;
use structopt::StructOpt; use structopt::StructOpt;
use crate::eval::{self, DateRange, Entry, EntryMode}; use crate::eval::{self, DateRange, Entry, EntryMode};
use crate::files::arguments::{CliDate, CliIdent, CliRange}; use crate::files::cli::{CliDate, CliIdent, CliRange};
use crate::files::{self, Files, ParseError}; use crate::files::{self, Files, ParseError};
use self::error::{Error, Result}; use self::error::{Error, Result};

View file

@ -1,6 +1,6 @@
use chrono::NaiveDate; use chrono::NaiveDate;
use crate::files::arguments::{CliDate, CliDatum, CliRange}; use crate::files::cli::{CliDate, CliDatum, CliRange};
use crate::files::{FileSource, Files}; use crate::files::{FileSource, Files};
use self::command::{CommandState, EvalCommand}; use self::command::{CommandState, EvalCommand};

View file

@ -11,7 +11,7 @@ use self::commands::{Command, Done, File, Log};
pub use self::error::{Error, ParseError, Result}; pub use self::error::{Error, ParseError, Result};
use self::primitives::Spanned; use self::primitives::Spanned;
pub mod arguments; pub mod cli;
pub mod commands; pub mod commands;
mod error; mod error;
mod format; mod format;

View file

@ -5,10 +5,21 @@ use chrono::NaiveDate;
use pest::iterators::Pair; use pest::iterators::Pair;
use pest::Parser; use pest::Parser;
use super::commands::Delta; use super::commands::{Command, Delta};
use super::parse::{self, Result, Rule, TodayfileParser}; use super::parse::{self, Result, Rule, TodayfileParser};
use super::ParseError; use super::ParseError;
fn from_str_via_parse<P, R>(s: &str, rule: Rule, parse: P) -> result::Result<R, ParseError<()>>
where
P: FnOnce(Pair<'_, Rule>) -> Result<R>,
{
let mut pairs = TodayfileParser::parse(rule, s).map_err(|e| ParseError::new((), e))?;
let p = pairs.next().unwrap();
assert_eq!(pairs.next(), None);
parse(p).map_err(|e| ParseError::new((), e))
}
#[derive(Debug)] #[derive(Debug)]
pub enum CliDatum { pub enum CliDatum {
Date(NaiveDate), Date(NaiveDate),
@ -50,12 +61,7 @@ impl FromStr for CliDate {
type Err = ParseError<()>; type Err = ParseError<()>;
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> { fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
let mut pairs = from_str_via_parse(s, Rule::cli_date, parse_cli_date)
TodayfileParser::parse(Rule::cli_date, s).map_err(|e| ParseError::new((), e))?;
let p = pairs.next().unwrap();
assert_eq!(pairs.next(), None);
parse_cli_date(p).map_err(|e| ParseError::new((), e))
} }
} }
@ -79,12 +85,7 @@ impl FromStr for CliIdent {
type Err = ParseError<()>; type Err = ParseError<()>;
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> { fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
let mut pairs = from_str_via_parse(s, Rule::cli_ident, parse_cli_ident)
TodayfileParser::parse(Rule::cli_ident, s).map_err(|e| ParseError::new((), e))?;
let p = pairs.next().unwrap();
assert_eq!(pairs.next(), None);
parse_cli_ident(p).map_err(|e| ParseError::new((), e))
} }
} }
@ -151,11 +152,23 @@ impl FromStr for CliRange {
type Err = ParseError<()>; type Err = ParseError<()>;
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> { fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
let mut pairs = from_str_via_parse(s, Rule::cli_range, parse_cli_range)
TodayfileParser::parse(Rule::cli_range, s).map_err(|e| ParseError::new((), e))?; }
let p = pairs.next().unwrap(); }
assert_eq!(pairs.next(), None);
#[derive(Debug)]
parse_cli_range(p).map_err(|e| ParseError::new((), e)) pub struct CliCommand(pub Command);
fn parse_cli_command(p: Pair<'_, Rule>) -> Result<CliCommand> {
assert_eq!(p.as_rule(), Rule::cli_command);
let p = p.into_inner().next().unwrap();
Ok(CliCommand(parse::parse_command(p)?.value))
}
impl FromStr for CliCommand {
type Err = ParseError<()>;
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
from_str_via_parse(s, Rule::cli_command, parse_cli_command)
} }
} }

View file

@ -156,3 +156,4 @@ cli_ident = { SOI ~ (cli_date | number) ~ EOI }
cli_range_start = { cli_datum ~ delta? } cli_range_start = { cli_datum ~ delta? }
cli_range_end = { cli_datum ~ delta? | delta } cli_range_end = { cli_datum ~ delta? | delta }
cli_range = { SOI ~ cli_range_start ~ ("--" ~ cli_range_end)? ~ EOI } cli_range = { SOI ~ cli_range_start ~ ("--" ~ cli_range_end)? ~ EOI }
cli_command = { SOI ~ command ~ EOI }

View file

@ -818,7 +818,7 @@ fn parse_log(p: Pair<'_, Rule>) -> Result<Log> {
Ok(Log { date, desc }) Ok(Log { date, desc })
} }
fn parse_command(p: Pair<'_, Rule>) -> Result<Spanned<Command>> { pub fn parse_command(p: Pair<'_, Rule>) -> Result<Spanned<Command>> {
assert_eq!(p.as_rule(), Rule::command); assert_eq!(p.as_rule(), Rule::command);
let p = p.into_inner().next().unwrap(); let p = p.into_inner().next().unwrap();