error.rs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. use std::fmt::Display;
  2. use serde::{Deserialize, Serialize};
  3. use crate::CodeLocation;
  4. /// An error produced when interperting the rsx
  5. #[derive(Debug, Serialize, Deserialize)]
  6. pub enum Error {
  7. ParseError(ParseError),
  8. RecompileRequiredError(RecompileReason),
  9. }
  10. #[derive(Debug, Serialize, Deserialize)]
  11. pub enum RecompileReason {
  12. CapturedVariable(String),
  13. CapturedExpression(String),
  14. CapturedComponent(String),
  15. CapturedListener(String),
  16. }
  17. #[derive(Debug, Serialize, Deserialize)]
  18. pub struct ParseError {
  19. pub message: String,
  20. pub location: CodeLocation,
  21. }
  22. impl ParseError {
  23. pub fn new(error: syn::Error, mut location: CodeLocation) -> Self {
  24. let message = error.to_string();
  25. let syn_call_site = error.span().start();
  26. location.line += syn_call_site.line as u32;
  27. if syn_call_site.line == 0 {
  28. location.column += syn_call_site.column as u32;
  29. } else {
  30. location.column = syn_call_site.column as u32;
  31. }
  32. location.column += 1;
  33. ParseError { message, location }
  34. }
  35. }
  36. impl Display for Error {
  37. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  38. match self {
  39. Error::ParseError(error) => write!(
  40. f,
  41. "parse error:\n--> at {}:{}:{}\n\t{:?}\n",
  42. error.location.file_path, error.location.line, error.location.column, error.message
  43. ),
  44. Error::RecompileRequiredError(reason) => {
  45. write!(f, "recompile required: {:?}\n", reason)
  46. }
  47. }
  48. }
  49. }