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