mod for_std;
use pex::StopBecause;
use std::ops::Range;
#[derive(Debug, Clone)]
pub struct DayanError {
kind: Box<DayanErrorKind>,
}
impl From<StopBecause> for DayanError {
fn from(value: StopBecause) -> Self {
Self { kind: Box::new(DayanErrorKind::SyntaxError { message: value.to_string(), range: value.range() }) }
}
}
#[derive(Debug, Clone)]
pub enum DayanErrorKind {
SyntaxError {
message: String,
range: Range<usize>,
},
TooLessArgument {
function: String,
count: u32,
except_min: Option<u32>,
except_max: Option<u32>,
},
TooMuchArgument {
function: String,
count: u32,
except_min: Option<u32>,
except_max: Option<u32>,
},
}
impl DayanError {
pub fn too_less_argument<S: ToString>(message: S, count: usize) -> Self {
DayanError {
kind: Box::new(DayanErrorKind::TooLessArgument {
function: message.to_string(),
count: count as u32,
except_min: None,
except_max: None,
}),
}
}
pub fn too_much_argument<S: ToString>(message: S, count: usize) -> Self {
DayanError {
kind: Box::new(DayanErrorKind::TooMuchArgument {
function: message.to_string(),
count: count as u32,
except_min: None,
except_max: None,
}),
}
}
pub fn with_min_argument(mut self, except: u32) -> Self {
match self.kind.as_mut() {
DayanErrorKind::TooLessArgument { except_min, .. } => {
*except_min = Some(except);
}
_ => {}
}
self
}
pub fn with_max_argument(mut self, except: u32) -> Self {
match self.kind.as_mut() {
DayanErrorKind::TooLessArgument { except_max, .. } => {
*except_max = Some(except);
}
_ => {}
}
self
}
}