dioxus/packages/rsx/src/error.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

use std::fmt::Display;
use serde::{Deserialize, Serialize};
2022-06-12 01:06:50 +00:00
/// An error produced when interperting the rsx
2022-06-15 17:58:08 +00:00
#[derive(Debug, Serialize, Deserialize)]
2022-05-29 13:04:08 +00:00
pub enum Error {
2022-06-15 17:58:08 +00:00
ParseError(ParseError),
2022-05-29 13:04:08 +00:00
RecompileRequiredError(RecompileReason),
}
#[derive(Debug, Serialize, Deserialize)]
2022-05-29 13:04:08 +00:00
pub enum RecompileReason {
2022-10-02 21:12:24 +00:00
Variable(String),
Expression(String),
Component(String),
Listener(String),
Attribute(String),
2022-05-29 13:04:08 +00:00
}
2022-06-15 17:58:08 +00:00
#[derive(Debug, Serialize, Deserialize)]
pub struct CodeLocation {
pub line: u32,
pub column: u32,
pub file_path: &'static str,
}
2022-06-15 17:58:08 +00:00
#[derive(Debug, Serialize, Deserialize)]
pub struct ParseError {
pub message: String,
pub location: CodeLocation,
2022-06-15 17:58:08 +00:00
}
impl ParseError {
pub fn new(error: syn::Error, mut location: CodeLocation) -> Self {
2022-06-15 17:58:08 +00:00
let message = error.to_string();
let syn_call_site = error.span().start();
location.line += syn_call_site.line as u32;
2022-06-15 18:36:18 +00:00
if syn_call_site.line == 0 {
2022-06-15 17:58:08 +00:00
location.column += syn_call_site.column as u32;
2022-06-15 18:36:18 +00:00
} else {
2022-06-15 17:58:08 +00:00
location.column = syn_call_site.column as u32;
}
2022-06-15 18:36:18 +00:00
location.column += 1;
2022-06-15 17:58:08 +00:00
ParseError { message, location }
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
2022-07-03 03:45:32 +00:00
Error::ParseError(error) => writeln!(
f,
2022-07-03 03:45:32 +00:00
"parse error:\n--> at {}:{}:{}\n\t{:?}",
error.location.file_path, error.location.line, error.location.column, error.message
),
Error::RecompileRequiredError(reason) => {
2022-07-03 03:45:32 +00:00
writeln!(f, "recompile required: {:?}", reason)
}
}
}
}