|
|
|
@ -1,5 +1,5 @@ |
|
|
|
|
//! A set of error types for cassiopeia
|
|
|
|
|
|
|
|
|
|
use crate::cass::{Date, Time}; |
|
|
|
|
use std::fmt::{self, Display, Formatter}; |
|
|
|
|
use std::{error::Error, io}; |
|
|
|
|
|
|
|
|
@ -12,18 +12,28 @@ use std::{error::Error, io}; |
|
|
|
|
#[derive(Debug)] |
|
|
|
|
pub enum UserError { |
|
|
|
|
/// Trying to start a session when one exists
|
|
|
|
|
ActiveSessionExists, |
|
|
|
|
ActiveSessionExists(Time), |
|
|
|
|
/// Trying to stop a session when none exists
|
|
|
|
|
NoActiveSession, |
|
|
|
|
NoActiveSession(Time), |
|
|
|
|
/// Trying to create a second invoice on the same day
|
|
|
|
|
SameDayInvoice, |
|
|
|
|
SameDayInvoice(Date), |
|
|
|
|
/// No work was done since the last invoice
|
|
|
|
|
NoWorkInvoice, |
|
|
|
|
NoWorkInvoice(Date), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Display for UserError { |
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
|
|
|
|
write!(f, "You're doing it wrong!") |
|
|
|
|
write!( |
|
|
|
|
f, |
|
|
|
|
"{}", |
|
|
|
|
match self { |
|
|
|
|
Self::ActiveSessionExists(t) => |
|
|
|
|
format!("Session since '{}' in progress", t.to_string()), |
|
|
|
|
Self::NoActiveSession(t) => format!("No session before '{}'", t.to_string()), |
|
|
|
|
Self::SameDayInvoice(d) => format!("More than one invoice on '{}'", d.to_string()), |
|
|
|
|
Self::NoWorkInvoice(d) => format!("No work done before '{}'", d.to_string()), |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -59,13 +69,33 @@ pub enum ParseError { |
|
|
|
|
BadTimestamp { line: usize, tokn: String }, |
|
|
|
|
/// A bad date was found
|
|
|
|
|
BadDate { line: usize, tokn: String }, |
|
|
|
|
/// An unexpected sequence was found
|
|
|
|
|
Unexpected { line: usize, tokn: String }, |
|
|
|
|
/// An unknown parse error occured
|
|
|
|
|
Unknown, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Display for ParseError { |
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
|
|
|
|
write!(f, "The parsed file was bad :(") |
|
|
|
|
write!( |
|
|
|
|
f, |
|
|
|
|
"{}", |
|
|
|
|
match self { |
|
|
|
|
Self::User(err) => format!("User error: {}", err), |
|
|
|
|
Self::NoSuchFile => "The file does not exist".into(), |
|
|
|
|
Self::BadPermissions => "Insufficient permissions to read the file".into(), |
|
|
|
|
Self::FileNotWritable => "Insufficient permissions to write the file".into(), |
|
|
|
|
Self::FileUnknown(file) => format!("System I/O error: {}", file), |
|
|
|
|
Self::BadKeyword { line, tokn } => |
|
|
|
|
format!("Invalid keyword '{}' on line {}", tokn, line), |
|
|
|
|
Self::BadTimestamp { line, tokn } => |
|
|
|
|
format!("Invalid timestamp '{}' on line {}", tokn, line), |
|
|
|
|
Self::BadDate { line, tokn } => format!("Invalid date '{}' on line {}", tokn, line), |
|
|
|
|
Self::Unexpected { line, tokn } => |
|
|
|
|
format!("Unexpected token '{}' on line {}", tokn, line), |
|
|
|
|
Self::Unknown => "An unknown error occured".into(), |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|