Implement Display for Error and derive Clone for ErrorType
This commit is contained in:
parent
e5307d4c9f
commit
b77eff39e1
|
@ -1,5 +1,5 @@
|
||||||
/// Represents the type of minification error.
|
/// Represents the type of minification error.
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum ErrorType {
|
pub enum ErrorType {
|
||||||
ClosingTagMismatch { expected: String, got: String },
|
ClosingTagMismatch { expected: String, got: String },
|
||||||
NotFound(&'static str),
|
NotFound(&'static str),
|
||||||
|
@ -7,36 +7,41 @@ pub enum ErrorType {
|
||||||
UnexpectedClosingTag,
|
UnexpectedClosingTag,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ErrorType {
|
impl std::fmt::Display for ErrorType {
|
||||||
/// Generates an English message describing the error with any additional context.
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
pub fn message(self) -> String {
|
|
||||||
match self {
|
match self {
|
||||||
ErrorType::ClosingTagMismatch { expected, got } => {
|
ErrorType::ClosingTagMismatch { expected, got } => {
|
||||||
format!(
|
write!(f,
|
||||||
"Closing tag name does not match opening tag (expected \"{}\", got \"{}\").",
|
"closing tag name does not match opening tag (expected \"{}\", got \"{}\")",
|
||||||
expected, got
|
expected, got
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
ErrorType::NotFound(exp) => {
|
ErrorType::NotFound(exp) => {
|
||||||
format!("Expected {}.", exp)
|
write!(f, "expected {}", exp)
|
||||||
}
|
}
|
||||||
ErrorType::UnexpectedEnd => {
|
ErrorType::UnexpectedEnd => {
|
||||||
format!("Unexpected end of source code.")
|
f.write_str("unexpected end of source code")
|
||||||
}
|
}
|
||||||
ErrorType::UnexpectedClosingTag => {
|
ErrorType::UnexpectedClosingTag => {
|
||||||
format!("Unexpected closing tag.")
|
f.write_str("unexpected closing tag")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Details about a minification failure, including where it occurred and why.
|
/// Details about a minification failure, including where it occurred and why.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Error {
|
pub struct Error {
|
||||||
pub error_type: ErrorType,
|
pub error_type: ErrorType,
|
||||||
pub position: usize,
|
pub position: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "At {}, {}", self.position, self.error_type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// User-friendly details about a minification failure, including an English message description of
|
/// User-friendly details about a minification failure, including an English message description of
|
||||||
/// the reason, and generated printable contextual representation of the code where the error
|
/// the reason, and generated printable contextual representation of the code where the error
|
||||||
/// occurred.
|
/// occurred.
|
||||||
|
|
|
@ -189,7 +189,7 @@ pub fn copy(code: &[u8], cfg: &Cfg) -> Result<Vec<u8>, Error> {
|
||||||
pub fn with_friendly_error(code: &mut [u8], cfg: &Cfg) -> Result<usize, FriendlyError> {
|
pub fn with_friendly_error(code: &mut [u8], cfg: &Cfg) -> Result<usize, FriendlyError> {
|
||||||
in_place(code, cfg).map_err(|err| FriendlyError {
|
in_place(code, cfg).map_err(|err| FriendlyError {
|
||||||
position: err.position,
|
position: err.position,
|
||||||
message: err.error_type.message(),
|
message: err.error_type.to_string(),
|
||||||
code_context: debug_repr(code, err.position as isize, -1),
|
code_context: debug_repr(code, err.position as isize, -1),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue