Use new format syntax in most places

This commit is contained in:
Joscha 2022-01-15 01:09:34 +01:00
parent a3a0e0b9cf
commit f82b368fe1
10 changed files with 120 additions and 127 deletions

View file

@ -45,8 +45,8 @@ where
Error::Eval(e) => e.eprint(files, config), Error::Eval(e) => e.eprint(files, config),
Error::ArgumentParse { file, error } => error.eprint(file, config), Error::ArgumentParse { file, error } => error.eprint(file, config),
Error::ArgumentEval { file, error } => error.eprint(file, config), Error::ArgumentEval { file, error } => error.eprint(file, config),
Error::NoSuchEntry(n) => eprintln!("No entry with number {}", n), Error::NoSuchEntry(n) => eprintln!("No entry with number {n}"),
Error::NoSuchLog(date) => eprintln!("No log for {}", date), Error::NoSuchLog(date) => eprintln!("No log for {date}"),
Error::NotATask(ns) => { Error::NotATask(ns) => {
if ns.is_empty() { if ns.is_empty() {
eprintln!("Not a task."); eprintln!("Not a task.");

View file

@ -185,7 +185,7 @@ impl LineLayout {
let extra = if *d == 1 { let extra = if *d == 1 {
"yesterday".to_string() "yesterday".to_string()
} else { } else {
format!("{} days ago", d) format!("{d} days ago")
}; };
self.line_entry(entries, *i, Times::Untimed, Some(extra)); self.line_entry(entries, *i, Times::Untimed, Some(extra));
} }
@ -194,7 +194,7 @@ impl LineLayout {
} }
DayEntry::ReminderWhile(i, d) => { DayEntry::ReminderWhile(i, d) => {
let plural = if *d == 1 { "" } else { "s" }; let plural = if *d == 1 { "" } else { "s" };
let extra = format!("{} day{} left", d, plural); let extra = format!("{d} day{plural} left");
self.line_entry(entries, *i, Times::Untimed, Some(extra)); self.line_entry(entries, *i, Times::Untimed, Some(extra));
} }
DayEntry::Undated(i) => { DayEntry::Undated(i) => {
@ -208,7 +208,7 @@ impl LineLayout {
let extra = if *d == 1 { let extra = if *d == 1 {
"tomorrow".to_string() "tomorrow".to_string()
} else { } else {
format!("in {} days", d) format!("in {d} days")
}; };
self.line_entry(entries, *i, Times::Untimed, Some(extra)); self.line_entry(entries, *i, Times::Untimed, Some(extra));
} }

View file

@ -63,28 +63,25 @@ impl ShowLines {
}; };
// '=' symbols before the spans start // '=' symbols before the spans start
let p1 = format!("{:=<w$}=", "", w = self.num_width); let p1 = styled(&format!("{:=<w$}=", "", w = self.num_width));
// Spans and filler '=' symbols // Spans and filler '=' symbols
let p2 = self.display_spans(spans, styled("=")); let p2 = self.display_spans(spans, styled("="));
// The rest of the line until after the date // The rest of the line until after the date
let p3 = format!("=== {:9} {}", weekday, date,); let p3 = styled(&format!("=== {weekday:9} {date}"));
// The "has log" marker (if any) // The "has log" marker (if any)
let p4 = Self::display_marker(has_log, " "); let p4 = Self::display_marker(has_log, " ");
// The rest of the line // The rest of the line
let p5 = format!(" ===={:=<w$}", "", w = self.num_width + self.span_width); let p5 = styled(&format!(
" ===={:=<w$}",
self.push(&format!( "",
"{}{}{}{}{}\n", w = self.num_width + self.span_width
styled(&p1),
p2,
styled(&p3),
p4,
styled(&p5)
)); ));
self.push(&format!("{p1}{p2}{p3}{p4}{p5}\n"));
} }
fn display_line_now(&mut self, spans: &[Option<SpanSegment>], time: Time) { fn display_line_now(&mut self, spans: &[Option<SpanSegment>], time: Time) {
@ -109,7 +106,7 @@ impl ShowLines {
extra: &Option<String>, extra: &Option<String>,
) { ) {
let num = match number { let num = match number {
Some(n) => format!("{}", n), Some(n) => format!("{n}"),
None => "".to_string(), None => "".to_string(),
}; };
@ -137,9 +134,9 @@ impl ShowLines {
SpanSegment::Middle(SpanStyle::Dotted) => "".bright_black(), SpanSegment::Middle(SpanStyle::Dotted) => "".bright_black(),
SpanSegment::End(_) => "".bright_black(), SpanSegment::End(_) => "".bright_black(),
}; };
result.push_str(&format!("{}", colored_str)); result.push_str(&format!("{colored_str}"));
} else { } else {
result.push_str(&format!("{}", empty)); result.push_str(&format!("{empty}"));
} }
} }
result result
@ -148,8 +145,8 @@ impl ShowLines {
fn display_time(time: Times) -> ColoredString { fn display_time(time: Times) -> ColoredString {
match time { match time {
Times::Untimed => "".into(), Times::Untimed => "".into(),
Times::At(t) => format!(" {}", t).bright_black(), Times::At(t) => format!(" {t}").bright_black(),
Times::FromTo(t1, t2) => format!(" {}--{}", t1, t2).bright_black(), Times::FromTo(t1, t2) => format!(" {t1}--{t2}").bright_black(),
} }
} }
@ -164,7 +161,7 @@ impl ShowLines {
fn display_extra(extra: &Option<String>) -> ColoredString { fn display_extra(extra: &Option<String>) -> ColoredString {
match extra { match extra {
None => "".into(), None => "".into(),
Some(extra) => format!(" ({})", extra).bright_black(), Some(extra) => format!(" ({extra})").bright_black(),
} }
} }

View file

@ -16,7 +16,8 @@ fn fmt_where(files: &Files, command: &Sourced<'_, Spanned<Command>>) -> String {
let line = files let line = files
.line_index(command.source.file(), command.value.span.start) .line_index(command.source.file(), command.value.span.start)
.expect("file exists and line is valid"); .expect("file exists and line is valid");
format!("Line {} in {}", line + 1, name) let line = line + 1; // 1-indexed for human consumption
format!("Line {line} in {name}")
} }
fn print_desc(command: &Sourced<'_, Spanned<Command>>) { fn print_desc(command: &Sourced<'_, Spanned<Command>>) {
@ -42,11 +43,11 @@ fn show_entry(files: &Files, entry: &Entry) {
let what = match entry.kind { let what = match entry.kind {
EntryKind::Task => "Task".to_string(), EntryKind::Task => "Task".to_string(),
EntryKind::TaskDone(date) => format!("Task, done {}", date), EntryKind::TaskDone(date) => format!("Task, done {date}"),
EntryKind::TaskCanceled(date) => format!("Task, canceled {}", date), EntryKind::TaskCanceled(date) => format!("Task, canceled {date}"),
EntryKind::Note => "Note".to_string(), EntryKind::Note => "Note".to_string(),
EntryKind::Birthday(None) => "Birthday, age unknown".to_string(), EntryKind::Birthday(None) => "Birthday, age unknown".to_string(),
EntryKind::Birthday(Some(age)) => format!("Birthday, age {}", age), EntryKind::Birthday(Some(age)) => format!("Birthday, age {age}"),
}; };
println!("{} {}", "What:".bright_black(), what); println!("{} {}", "What:".bright_black(), what);
@ -76,7 +77,7 @@ fn show_ident(files: &Files, entries: &[Entry], layout: &LineLayout, ident: Iden
match ident { match ident {
Ident::Number(n) => match layout.look_up_number(n) { Ident::Number(n) => match layout.look_up_number(n) {
Ok(index) => show_entry(files, &entries[index]), Ok(index) => show_entry(files, &entries[index]),
Err(e) => println!("{}", e), Err(e) => println!("{e}"),
}, },
Ident::Date(date) => match files.log(date) { Ident::Date(date) => match files.log(date) {
Some(log) => show_log(files, log), Some(log) => show_log(files, log),

View file

@ -11,7 +11,7 @@ pub trait Eprint<'a, F: Files<'a>> {
) { ) {
let mut out = StandardStream::stderr(termcolor::ColorChoice::Auto); let mut out = StandardStream::stderr(termcolor::ColorChoice::Auto);
if let Err(e) = term::emit(&mut out, config, files, diagnostic) { if let Err(e) = term::emit(&mut out, config, files, diagnostic) {
panic!("Error while reporting error: {}", e); panic!("Error while reporting error: {e}");
} }
} }

View file

@ -391,7 +391,7 @@ mod tests {
if let Ok(result) = expr.eval((), date) { if let Ok(result) = expr.eval((), date) {
assert_eq!(result, target); assert_eq!(result, target);
} else { } else {
panic!("formula produced error for day {}", date); panic!("formula produced error for day {date}");
} }
} }

View file

@ -106,8 +106,8 @@ impl<'a, F: Files<'a>> Eprint<'a, F> for Error<F::FileId> {
Label::primary(*index, span).with_message("At this step") Label::primary(*index, span).with_message("At this step")
]) ])
.with_notes(vec![ .with_notes(vec![
format!("Date before applying delta: {}", start_str), format!("Date before applying delta: {start_str}"),
format!("Date before applying this step: {}", prev_str), format!("Date before applying this step: {prev_str}"),
]) ])
} }
Error::DeltaNoTime { Error::DeltaNoTime {
@ -121,8 +121,8 @@ impl<'a, F: Files<'a>> Eprint<'a, F> for Error<F::FileId> {
Label::primary(*index, span).with_message("At this step") Label::primary(*index, span).with_message("At this step")
]) ])
.with_notes(vec![ .with_notes(vec![
format!("Date before applying delta: {}", start), format!("Date before applying delta: {start}"),
format!("Date before applying this step: {}", prev), format!("Date before applying this step: {prev}"),
]), ]),
Error::RepeatDidNotMoveForwards { Error::RepeatDidNotMoveForwards {
index, index,
@ -132,7 +132,7 @@ impl<'a, F: Files<'a>> Eprint<'a, F> for Error<F::FileId> {
} => Diagnostic::error() } => Diagnostic::error()
.with_message("Repeat delta did not move forwards") .with_message("Repeat delta did not move forwards")
.with_labels(vec![Label::primary(*index, span).with_message("This delta")]) .with_labels(vec![Label::primary(*index, span).with_message("This delta")])
.with_notes(vec![format!("Moved from {} to {}", from, to)]), .with_notes(vec![format!("Moved from {from} to {to}")]),
Error::RemindDidNotMoveBackwards { Error::RemindDidNotMoveBackwards {
index, index,
span, span,
@ -141,7 +141,7 @@ impl<'a, F: Files<'a>> Eprint<'a, F> for Error<F::FileId> {
} => Diagnostic::error() } => Diagnostic::error()
.with_message("Remind delta did not move backwards") .with_message("Remind delta did not move backwards")
.with_labels(vec![Label::primary(*index, span).with_message("This delta")]) .with_labels(vec![Label::primary(*index, span).with_message("This delta")])
.with_notes(vec![format!("Moved from {} to {}", from, to)]), .with_notes(vec![format!("Moved from {from} to {to}")]),
Error::MoveWithoutSource { index, span } => Diagnostic::error() Error::MoveWithoutSource { index, span } => Diagnostic::error()
.with_message("Tried to move nonexistent entry") .with_message("Tried to move nonexistent entry")
.with_labels(vec![Label::primary(*index, span).with_message("Here")]), .with_labels(vec![Label::primary(*index, span).with_message("Here")]),
@ -153,13 +153,13 @@ impl<'a, F: Files<'a>> Eprint<'a, F> for Error<F::FileId> {
.with_labels(vec![ .with_labels(vec![
Label::primary(*index, span).with_message("This expression") Label::primary(*index, span).with_message("This expression")
]) ])
.with_notes(vec![format!("At date: {}", date)]), .with_notes(vec![format!("At date: {date}")]),
Error::ModByZero { index, span, date } => Diagnostic::error() Error::ModByZero { index, span, date } => Diagnostic::error()
.with_message("Tried to modulo by zero") .with_message("Tried to modulo by zero")
.with_labels(vec![ .with_labels(vec![
Label::primary(*index, span).with_message("This expression") Label::primary(*index, span).with_message("This expression")
]) ])
.with_notes(vec![format!("At date: {}", date)]), .with_notes(vec![format!("At date: {date}")]),
Error::Easter { Error::Easter {
index, index,
span, span,
@ -170,10 +170,7 @@ impl<'a, F: Files<'a>> Eprint<'a, F> for Error<F::FileId> {
.with_labels(vec![ .with_labels(vec![
Label::primary(*index, span).with_message("This expression") Label::primary(*index, span).with_message("This expression")
]) ])
.with_notes(vec![ .with_notes(vec![format!("At date: {date}"), format!("Reason: {msg}")]),
format!("At date: {}", date),
format!("Reason: {}", msg),
]),
}; };
Self::eprint_diagnostic(files, config, &diagnostic); Self::eprint_diagnostic(files, config, &diagnostic);
} }

View file

@ -40,7 +40,7 @@ impl<S> ParseError<S> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join(", "); .join(", ");
let last = Self::rule_name(rules[n - 1]); let last = Self::rule_name(rules[n - 1]);
format!("{} or {}", except_last, last) format!("{except_last} or {last}")
} }
} }
} }
@ -76,7 +76,7 @@ where
}; };
let name = files.name(self.file).expect("file exists"); let name = files.name(self.file).expect("file exists");
let diagnostic = Diagnostic::error() let diagnostic = Diagnostic::error()
.with_message(format!("Could not parse {}", name)) .with_message(format!("Could not parse {name}"))
.with_labels(vec![Label::primary(self.file, range)]) .with_labels(vec![Label::primary(self.file, range)])
.with_notes(self.notes()); .with_notes(self.notes());
Self::eprint_diagnostic(files, config, &diagnostic); Self::eprint_diagnostic(files, config, &diagnostic);
@ -135,16 +135,16 @@ impl<'a> Eprint<'a, Files> for Error {
fn eprint<'f: 'a>(&self, files: &'f Files, config: &Config) { fn eprint<'f: 'a>(&self, files: &'f Files, config: &Config) {
match self { match self {
Error::ResolvePath { path, error } => { Error::ResolvePath { path, error } => {
eprintln!("Could not resolve path {:?}:", path); eprintln!("Could not resolve path {path:?}:");
eprintln!(" {}", error); eprintln!(" {error}");
} }
Error::ReadFile { file, error } => { Error::ReadFile { file, error } => {
eprintln!("Could not read file {:?}:", file); eprintln!("Could not read file {file:?}:");
eprintln!(" {}", error); eprintln!(" {error}");
} }
Error::WriteFile { file, error } => { Error::WriteFile { file, error } => {
eprintln!("Could not write file {:?}:", file); eprintln!("Could not write file {file:?}:");
eprintln!(" {}", error); eprintln!(" {error}");
} }
Error::ResolveTz { Error::ResolveTz {
file, file,
@ -153,16 +153,16 @@ impl<'a> Eprint<'a, Files> for Error {
error, error,
} => { } => {
let diagnostic = Diagnostic::error() let diagnostic = Diagnostic::error()
.with_message(format!("Could not resolve time zone {}", tz)) .with_message(format!("Could not resolve time zone {tz}"))
.with_labels(vec![ .with_labels(vec![
Label::primary(*file, span).with_message("Time zone defined here") Label::primary(*file, span).with_message("Time zone defined here")
]) ])
.with_notes(vec![format!("{}", error)]); .with_notes(vec![format!("{error}")]);
Self::eprint_diagnostic(files, config, &diagnostic); Self::eprint_diagnostic(files, config, &diagnostic);
} }
Error::LocalTz { error } => { Error::LocalTz { error } => {
eprintln!("Could not determine local timezone:"); eprintln!("Could not determine local timezone:");
eprintln!(" {}", error); eprintln!(" {error}");
} }
Error::Parse { file, error } => { Error::Parse { file, error } => {
ParseError::new(*file, error.clone()).eprint(files, config) ParseError::new(*file, error.clone()).eprint(files, config)
@ -176,7 +176,7 @@ impl<'a> Eprint<'a, Files> for Error {
tz2, tz2,
} => { } => {
let diagnostic = Diagnostic::error() let diagnostic = Diagnostic::error()
.with_message(format!("Time zone conflict between {} and {}", tz1, tz2)) .with_message(format!("Time zone conflict between {tz1} and {tz2}"))
.with_labels(vec![ .with_labels(vec![
Label::primary(*file1, span1).with_message("Time zone defined here"), Label::primary(*file1, span1).with_message("Time zone defined here"),
Label::primary(*file2, span2).with_message("Time zone defined here"), Label::primary(*file2, span2).with_message("Time zone defined here"),
@ -211,7 +211,7 @@ impl<'a> Eprint<'a, Files> for Error {
date, date,
} => { } => {
let diagnostic = Diagnostic::error() let diagnostic = Diagnostic::error()
.with_message(format!("Duplicate log entries for {}", date)) .with_message(format!("Duplicate log entries for {date}"))
.with_labels(vec![ .with_labels(vec![
Label::primary(*file1, span1).with_message("Log defined here"), Label::primary(*file1, span1).with_message("Log defined here"),
Label::primary(*file2, span2).with_message("Log defined here"), Label::primary(*file2, span2).with_message("Log defined here"),

View file

@ -22,7 +22,7 @@ fn format_desc(f: &mut fmt::Formatter<'_>, desc: &[String]) -> fmt::Result {
if line.is_empty() { if line.is_empty() {
writeln!(f, "#")?; writeln!(f, "#")?;
} else { } else {
writeln!(f, "# {}", line)?; writeln!(f, "# {line}")?;
} }
} }
Ok(()) Ok(())
@ -76,29 +76,29 @@ impl fmt::Display for DateSpec {
// Start // Start
write!(f, "{}", self.start)?; write!(f, "{}", self.start)?;
for delta in &self.start_delta { for delta in &self.start_delta {
write!(f, " {}", delta)?; write!(f, " {delta}")?;
} }
for time in &self.start_time { for time in &self.start_time {
write!(f, " {}", time)?; write!(f, " {time}")?;
} }
// End // End
if self.end.is_some() || self.end_delta.is_some() || self.end_time.is_some() { if self.end.is_some() || self.end_delta.is_some() || self.end_time.is_some() {
write!(f, " --")?; write!(f, " --")?;
if let Some(date) = self.end { if let Some(date) = self.end {
write!(f, " {}", date)?; write!(f, " {date}")?;
} }
if let Some(delta) = &self.end_delta { if let Some(delta) = &self.end_delta {
write!(f, " {}", delta)?; write!(f, " {delta}")?;
} }
if let Some(time) = &self.end_time { if let Some(time) = &self.end_time {
write!(f, " {}", time)?; write!(f, " {time}")?;
} }
} }
// Repeat // Repeat
if let Some(repeat) = &self.repeat { if let Some(repeat) = &self.repeat {
write!(f, "; {}", repeat)?; write!(f, "; {repeat}")?;
} }
Ok(()) Ok(())
@ -110,20 +110,20 @@ impl fmt::Display for WeekdaySpec {
// Start // Start
write!(f, "{}", self.start)?; write!(f, "{}", self.start)?;
for time in &self.start_time { for time in &self.start_time {
write!(f, " {}", time)?; write!(f, " {time}")?;
} }
// End // End
if self.end.is_some() || self.end_delta.is_some() || self.end_time.is_some() { if self.end.is_some() || self.end_delta.is_some() || self.end_time.is_some() {
write!(f, " --")?; write!(f, " --")?;
if let Some(wd) = self.end { if let Some(wd) = self.end {
write!(f, " {}", wd)?; write!(f, " {wd}")?;
} }
if let Some(delta) = &self.end_delta { if let Some(delta) = &self.end_delta {
write!(f, " {}", delta)?; write!(f, " {delta}")?;
} }
if let Some(time) = &self.end_time { if let Some(time) = &self.end_time {
write!(f, " {}", time)?; write!(f, " {time}")?;
} }
} }
@ -140,25 +140,25 @@ impl fmt::Display for Var {
impl fmt::Display for Expr { impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Expr::Lit(i) => write!(f, "{}", i), Expr::Lit(i) => write!(f, "{i}"),
Expr::Var(v) => write!(f, "{}", v), Expr::Var(v) => write!(f, "{v}"),
Expr::Paren(e) => write!(f, "({})", e), Expr::Paren(e) => write!(f, "({e})"),
Expr::Neg(e) => write!(f, "-{}", e), Expr::Neg(e) => write!(f, "-{e}"),
Expr::Add(a, b) => write!(f, "{} + {}", a, b), Expr::Add(a, b) => write!(f, "{a} + {b}"),
Expr::Sub(a, b) => write!(f, "{} - {}", a, b), Expr::Sub(a, b) => write!(f, "{a} - {b}"),
Expr::Mul(a, b) => write!(f, "{} * {}", a, b), Expr::Mul(a, b) => write!(f, "{a} * {b}"),
Expr::Div(a, b) => write!(f, "{} / {}", a, b), Expr::Div(a, b) => write!(f, "{a} / {b}"),
Expr::Mod(a, b) => write!(f, "{} % {}", a, b), Expr::Mod(a, b) => write!(f, "{a} % {b}"),
Expr::Eq(a, b) => write!(f, "{} = {}", a, b), Expr::Eq(a, b) => write!(f, "{a} = {b}"),
Expr::Neq(a, b) => write!(f, "{} != {}", a, b), Expr::Neq(a, b) => write!(f, "{a} != {b}"),
Expr::Lt(a, b) => write!(f, "{} < {}", a, b), Expr::Lt(a, b) => write!(f, "{a} < {b}"),
Expr::Lte(a, b) => write!(f, "{} <= {}", a, b), Expr::Lte(a, b) => write!(f, "{a} <= {b}"),
Expr::Gt(a, b) => write!(f, "{} > {}", a, b), Expr::Gt(a, b) => write!(f, "{a} > {b}"),
Expr::Gte(a, b) => write!(f, "{} >= {}", a, b), Expr::Gte(a, b) => write!(f, "{a} >= {b}"),
Expr::Not(e) => write!(f, "!{}", e), Expr::Not(e) => write!(f, "!{e}"),
Expr::And(a, b) => write!(f, "{} & {}", a, b), Expr::And(a, b) => write!(f, "{a} & {b}"),
Expr::Or(a, b) => write!(f, "{} | {}", a, b), Expr::Or(a, b) => write!(f, "{a} | {b}"),
Expr::Xor(a, b) => write!(f, "{} ^ {}", a, b), Expr::Xor(a, b) => write!(f, "{a} ^ {b}"),
} }
} }
} }
@ -167,25 +167,25 @@ impl fmt::Display for FormulaSpec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Start // Start
if let Some(expr) = &self.start { if let Some(expr) = &self.start {
write!(f, "({})", expr)?; write!(f, "({expr})")?;
} else { } else {
write!(f, "*")?; write!(f, "*")?;
} }
for delta in &self.start_delta { for delta in &self.start_delta {
write!(f, " {}", delta)?; write!(f, " {delta}")?;
} }
for time in &self.start_time { for time in &self.start_time {
write!(f, " {}", time)?; write!(f, " {time}")?;
} }
// End // End
if self.end_delta.is_some() || self.end_time.is_some() { if self.end_delta.is_some() || self.end_time.is_some() {
write!(f, " --")?; write!(f, " --")?;
if let Some(delta) = &self.end_delta { if let Some(delta) = &self.end_delta {
write!(f, " {}", delta)?; write!(f, " {delta}")?;
} }
if let Some(time) = &self.end_time { if let Some(time) = &self.end_time {
write!(f, " {}", time)?; write!(f, " {time}")?;
} }
} }
@ -196,9 +196,9 @@ impl fmt::Display for FormulaSpec {
impl fmt::Display for Spec { impl fmt::Display for Spec {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Spec::Date(spec) => write!(f, "{}", spec), Spec::Date(spec) => write!(f, "{spec}"),
Spec::Weekday(spec) => write!(f, "{}", spec), Spec::Weekday(spec) => write!(f, "{spec}"),
Spec::Formula(spec) => write!(f, "{}", spec), Spec::Formula(spec) => write!(f, "{spec}"),
} }
} }
} }
@ -216,22 +216,22 @@ impl fmt::Display for BirthdaySpec {
impl fmt::Display for Statement { impl fmt::Display for Statement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Statement::Date(spec) => writeln!(f, "DATE {}", spec), Statement::Date(spec) => writeln!(f, "DATE {spec}"),
Statement::BDate(spec) => writeln!(f, "BDATE {}", spec), Statement::BDate(spec) => writeln!(f, "BDATE {spec}"),
Statement::From(Some(date)) => writeln!(f, "FROM {}", date), Statement::From(Some(date)) => writeln!(f, "FROM {date}"),
Statement::From(None) => writeln!(f, "FROM *"), Statement::From(None) => writeln!(f, "FROM *"),
Statement::Until(Some(date)) => writeln!(f, "UNTIL {}", date), Statement::Until(Some(date)) => writeln!(f, "UNTIL {date}"),
Statement::Until(None) => writeln!(f, "UNTIL *"), Statement::Until(None) => writeln!(f, "UNTIL *"),
Statement::Except(date) => writeln!(f, "EXCEPT {}", date), Statement::Except(date) => writeln!(f, "EXCEPT {date}"),
Statement::Move { Statement::Move {
from, to, to_time, .. from, to, to_time, ..
} => match (to, to_time) { } => match (to, to_time) {
(None, None) => unreachable!(), (None, None) => unreachable!(),
(Some(to), None) => writeln!(f, "MOVE {} TO {}", from, to), (Some(to), None) => writeln!(f, "MOVE {from} TO {to}"),
(None, Some(to_time)) => writeln!(f, "MOVE {} TO {}", from, to_time), (None, Some(to_time)) => writeln!(f, "MOVE {from} TO {to_time}"),
(Some(to), Some(to_time)) => writeln!(f, "MOVE {} TO {} {}", from, to, to_time), (Some(to), Some(to_time)) => writeln!(f, "MOVE {from} TO {to} {to_time}"),
}, },
Statement::Remind(Some(delta)) => writeln!(f, "REMIND {}", delta), Statement::Remind(Some(delta)) => writeln!(f, "REMIND {delta}"),
Statement::Remind(None) => writeln!(f, "REMIND *"), Statement::Remind(None) => writeln!(f, "REMIND *"),
} }
} }
@ -240,20 +240,20 @@ impl fmt::Display for Statement {
impl fmt::Display for DoneDate { impl fmt::Display for DoneDate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.simplified() { match self.simplified() {
DoneDate::Date { root } => write!(f, "{}", root), DoneDate::Date { root } => write!(f, "{root}"),
DoneDate::DateTime { root, root_time } => write!(f, "{} {}", root, root_time), DoneDate::DateTime { root, root_time } => write!(f, "{root} {root_time}"),
DoneDate::DateToDate { root, other } => write!(f, "{} -- {}", root, other), DoneDate::DateToDate { root, other } => write!(f, "{root} -- {other}"),
DoneDate::DateTimeToTime { DoneDate::DateTimeToTime {
root, root,
root_time, root_time,
other_time, other_time,
} => write!(f, "{} {} -- {}", root, root_time, other_time), } => write!(f, "{root} {root_time} -- {other_time}"),
DoneDate::DateTimeToDateTime { DoneDate::DateTimeToDateTime {
root, root,
root_time, root_time,
other, other,
other_time, other_time,
} => write!(f, "{} {} -- {} {}", root, root_time, other, other_time), } => write!(f, "{root} {root_time} -- {other} {other_time}"),
} }
} }
} }
@ -264,9 +264,9 @@ impl fmt::Display for Done {
DoneKind::Done => "DONE", DoneKind::Done => "DONE",
DoneKind::Canceled => "CANCELED", DoneKind::Canceled => "CANCELED",
}; };
write!(f, "{} [{}]", kind, self.done_at)?; write!(f, "{kind} [{}]", self.done_at)?;
if let Some(date) = &self.date { if let Some(date) = &self.date {
write!(f, " {}", date)?; write!(f, " {date}")?;
} }
writeln!(f) writeln!(f)
} }
@ -276,10 +276,10 @@ impl fmt::Display for Task {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "TASK {}", self.title)?; writeln!(f, "TASK {}", self.title)?;
for statement in &self.statements { for statement in &self.statements {
write!(f, "{}", statement)?; write!(f, "{statement}")?;
} }
for done in &self.done { for done in &self.done {
write!(f, "{}", done)?; write!(f, "{done}")?;
} }
format_desc(f, &self.desc)?; format_desc(f, &self.desc)?;
Ok(()) Ok(())
@ -290,26 +290,13 @@ impl fmt::Display for Note {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "NOTE {}", self.title)?; writeln!(f, "NOTE {}", self.title)?;
for statement in &self.statements { for statement in &self.statements {
write!(f, "{}", statement)?; write!(f, "{statement}")?;
} }
format_desc(f, &self.desc)?; format_desc(f, &self.desc)?;
Ok(()) Ok(())
} }
} }
impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Command::Include(name) => writeln!(f, "INCLUDE {}", name),
Command::Timezone(name) => writeln!(f, "TIMEZONE {}", name),
Command::Capture => writeln!(f, "CAPTURE"),
Command::Task(task) => write!(f, "{}", task),
Command::Note(note) => write!(f, "{}", note),
Command::Log(log) => write!(f, "{}", log),
}
}
}
impl fmt::Display for Log { impl fmt::Display for Log {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "LOG {}", self.date)?; writeln!(f, "LOG {}", self.date)?;
@ -318,6 +305,19 @@ impl fmt::Display for Log {
} }
} }
impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Command::Include(name) => writeln!(f, "INCLUDE {name}"),
Command::Timezone(name) => writeln!(f, "TIMEZONE {name}"),
Command::Capture => writeln!(f, "CAPTURE"),
Command::Task(task) => write!(f, "{task}"),
Command::Note(note) => write!(f, "{note}"),
Command::Log(log) => write!(f, "{log}"),
}
}
}
impl File { impl File {
fn sort(commands: &mut Vec<&Command>) { fn sort(commands: &mut Vec<&Command>) {
// Order of commands in a file: // Order of commands in a file:
@ -372,7 +372,7 @@ impl File {
let curr = &commands[i]; let curr = &commands[i];
let next = commands.get(i + 1); let next = commands.get(i + 1);
result.push_str(&format!("{}", curr)); result.push_str(&format!("{curr}"));
match (curr, next) { match (curr, next) {
(Command::Include(_), Some(Command::Include(_))) => {} (Command::Include(_), Some(Command::Include(_))) => {}

View file

@ -3,8 +3,6 @@
#![warn(clippy::all)] #![warn(clippy::all)]
#![warn(clippy::use_self)] #![warn(clippy::use_self)]
// TODO Switch to new format syntax project-wide
mod cli; mod cli;
mod error; mod error;
mod eval; mod eval;