Move stuff to files module
This commit is contained in:
parent
5674bcdc00
commit
b4b8dd53e0
8 changed files with 31 additions and 20 deletions
|
|
@ -3,7 +3,7 @@ use std::result;
|
|||
|
||||
use chrono::{Datelike, NaiveDate};
|
||||
|
||||
use crate::commands::{Birthday, Command, File, Note, Spec, Task};
|
||||
use crate::files::commands::{Birthday, Command, File, Note, Spec, Task};
|
||||
|
||||
use self::entries::{DateRange, Entry, EntryKind, EntryMap};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,9 @@
|
|||
use std::collections::HashMap;
|
||||
use std::ops::RangeInclusive;
|
||||
use std::str::from_boxed_utf8_unchecked;
|
||||
|
||||
use chrono::{Datelike, NaiveDate};
|
||||
|
||||
use crate::commands::Time;
|
||||
use crate::files::commands::Time;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum EntryKind {
|
||||
|
|
|
|||
15
src/files.rs
15
src/files.rs
|
|
@ -4,8 +4,11 @@ use std::{fs, io, result};
|
|||
|
||||
use chrono_tz::Tz;
|
||||
|
||||
use crate::commands::File;
|
||||
use crate::parse;
|
||||
use self::commands::File;
|
||||
|
||||
pub mod commands;
|
||||
mod format;
|
||||
mod parse;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Files {
|
||||
|
|
@ -30,24 +33,24 @@ impl Files {
|
|||
timezone: None,
|
||||
};
|
||||
|
||||
new.load_file(path)?;
|
||||
new.load_file(path.to_owned())?;
|
||||
new.determine_timezone()?;
|
||||
|
||||
Ok(new)
|
||||
}
|
||||
|
||||
fn load_file(&mut self, path: &Path) -> Result<()> {
|
||||
fn load_file(&mut self, path: PathBuf) -> Result<()> {
|
||||
let canon_path = path.canonicalize()?;
|
||||
if self.files.contains_key(&canon_path) {
|
||||
// We've already loaded this exact file.
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(path)?;
|
||||
let content = fs::read_to_string(&path)?;
|
||||
let file = parse::parse(path, &content)?;
|
||||
self.files.insert(canon_path, file);
|
||||
|
||||
// TODO Also load all imported files
|
||||
// TODO Also load all included files
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use chrono_tz::Tz;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Time {
|
||||
|
|
@ -326,5 +329,8 @@ pub enum Command {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct File {
|
||||
pub name: PathBuf,
|
||||
pub includes: Vec<PathBuf>,
|
||||
pub timezone: Option<Tz>,
|
||||
pub commands: Vec<Command>,
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@ use std::fmt;
|
|||
|
||||
use chrono::Datelike;
|
||||
|
||||
use crate::commands::{
|
||||
use super::commands::{
|
||||
Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, Expr, File, FormulaSpec,
|
||||
Note, Spec, Task, Time, Var, Weekday, WeekdaySpec,
|
||||
};
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::result;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
|
|
@ -7,13 +7,13 @@ use pest::iterators::Pair;
|
|||
use pest::prec_climber::{Assoc, Operator, PrecClimber};
|
||||
use pest::{Parser, Span};
|
||||
|
||||
use crate::commands::{
|
||||
use super::commands::{
|
||||
Birthday, BirthdaySpec, Command, DateSpec, Delta, DeltaStep, Done, Expr, File, FormulaSpec,
|
||||
Note, Spec, Task, Time, Var, Weekday, WeekdaySpec,
|
||||
};
|
||||
|
||||
#[derive(pest_derive::Parser)]
|
||||
#[grammar = "parse/todayfile.pest"]
|
||||
#[grammar = "files/grammar.pest"]
|
||||
struct TodayfileParser;
|
||||
|
||||
pub type Error = pest::error::Error<Rule>;
|
||||
|
|
@ -703,8 +703,8 @@ fn parse_command(p: Pair<Rule>) -> Result<Command> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse(path: &Path, input: &str) -> Result<File> {
|
||||
let path = path.to_string_lossy();
|
||||
pub fn parse(path: PathBuf, input: &str) -> Result<File> {
|
||||
let pathstr = path.to_string_lossy();
|
||||
let mut pairs = TodayfileParser::parse(Rule::file, input)?;
|
||||
let file = pairs.next().unwrap();
|
||||
let commands = file
|
||||
|
|
@ -713,6 +713,12 @@ pub fn parse(path: &Path, input: &str) -> Result<File> {
|
|||
.take_while(|p| p.as_rule() == Rule::command)
|
||||
.map(parse_command)
|
||||
.collect::<Result<_>>()
|
||||
.map_err(|e| e.with_path(&path))?;
|
||||
Ok(File { commands })
|
||||
.map_err(|e| e.with_path(&pathstr))?;
|
||||
|
||||
Ok(File {
|
||||
name: path,
|
||||
includes: vec![],
|
||||
timezone: None,
|
||||
commands,
|
||||
})
|
||||
}
|
||||
|
|
@ -4,11 +4,8 @@ use structopt::StructOpt;
|
|||
|
||||
use crate::files::Files;
|
||||
|
||||
mod commands;
|
||||
mod eval;
|
||||
mod files;
|
||||
mod format;
|
||||
mod parse;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
pub struct Opt {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue