Parse arbitrary commands
This commit is contained in:
parent
fe1bf309b8
commit
0dcb75b103
6 changed files with 37 additions and 23 deletions
|
|
@ -8,7 +8,7 @@ use directories::ProjectDirs;
|
|||
use structopt::StructOpt;
|
||||
|
||||
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 self::error::{Error, Result};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use chrono::NaiveDate;
|
||||
|
||||
use crate::files::arguments::{CliDate, CliDatum, CliRange};
|
||||
use crate::files::cli::{CliDate, CliDatum, CliRange};
|
||||
use crate::files::{FileSource, Files};
|
||||
|
||||
use self::command::{CommandState, EvalCommand};
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use self::commands::{Command, Done, File, Log};
|
|||
pub use self::error::{Error, ParseError, Result};
|
||||
use self::primitives::Spanned;
|
||||
|
||||
pub mod arguments;
|
||||
pub mod cli;
|
||||
pub mod commands;
|
||||
mod error;
|
||||
mod format;
|
||||
|
|
|
|||
|
|
@ -5,10 +5,21 @@ use chrono::NaiveDate;
|
|||
use pest::iterators::Pair;
|
||||
use pest::Parser;
|
||||
|
||||
use super::commands::Delta;
|
||||
use super::commands::{Command, Delta};
|
||||
use super::parse::{self, Result, Rule, TodayfileParser};
|
||||
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)]
|
||||
pub enum CliDatum {
|
||||
Date(NaiveDate),
|
||||
|
|
@ -50,12 +61,7 @@ impl FromStr for CliDate {
|
|||
type Err = ParseError<()>;
|
||||
|
||||
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
|
||||
let mut pairs =
|
||||
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))
|
||||
from_str_via_parse(s, Rule::cli_date, parse_cli_date)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,12 +85,7 @@ impl FromStr for CliIdent {
|
|||
type Err = ParseError<()>;
|
||||
|
||||
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
|
||||
let mut pairs =
|
||||
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))
|
||||
from_str_via_parse(s, Rule::cli_ident, parse_cli_ident)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -151,11 +152,23 @@ impl FromStr for CliRange {
|
|||
type Err = ParseError<()>;
|
||||
|
||||
fn from_str(s: &str) -> result::Result<Self, ParseError<()>> {
|
||||
let mut pairs =
|
||||
TodayfileParser::parse(Rule::cli_range, s).map_err(|e| ParseError::new((), e))?;
|
||||
let p = pairs.next().unwrap();
|
||||
assert_eq!(pairs.next(), None);
|
||||
from_str_via_parse(s, Rule::cli_range, parse_cli_range)
|
||||
}
|
||||
}
|
||||
|
||||
parse_cli_range(p).map_err(|e| ParseError::new((), e))
|
||||
#[derive(Debug)]
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -156,3 +156,4 @@ cli_ident = { SOI ~ (cli_date | number) ~ EOI }
|
|||
cli_range_start = { cli_datum ~ delta? }
|
||||
cli_range_end = { cli_datum ~ delta? | delta }
|
||||
cli_range = { SOI ~ cli_range_start ~ ("--" ~ cli_range_end)? ~ EOI }
|
||||
cli_command = { SOI ~ command ~ EOI }
|
||||
|
|
|
|||
|
|
@ -818,7 +818,7 @@ fn parse_log(p: Pair<'_, Rule>) -> Result<Log> {
|
|||
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);
|
||||
|
||||
let p = p.into_inner().next().unwrap();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue