1use std::rc::Rc;
4
5#[allow(non_snake_case)]
7pub mod ResponseType {
8 pub const Text: u32 = 0;
9 pub const ReadableStream: u32 = 1;
10 pub const ArrayBuffer: u32 = 2;
11 pub const JSON: u32 = 3;
12}
13
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16pub enum ErrorAction {
17 Drop,
18 Ignore,
19}
20
21impl ErrorAction {
22 pub fn as_str(self) -> &'static str {
23 match self {
24 ErrorAction::Drop => "drop",
25 ErrorAction::Ignore => "ignore",
26 }
27 }
28}
29
30#[derive(Clone, Debug, PartialEq)]
32pub enum ErrorSyntax {
33 Text(Rc<str>),
34 Token(Rc<crate::validation::parser::types::ValidationToken>),
36 Object(Rc<crate::plain::Plain>),
38}
39
40#[derive(Clone, Debug, PartialEq)]
42pub struct ErrorDescription {
43 pub action: ErrorAction,
44 pub message: String,
45 pub syntax: Option<ErrorSyntax>,
46 pub node: Option<u32>,
48 pub location: Option<(Option<Rc<str>>, u32, u32)>,
50 pub error: Option<String>,
52}
53
54impl ErrorDescription {
55 pub fn new(action: ErrorAction, message: impl Into<String>) -> Self {
56 Self { action, message: message.into(), syntax: None, node: None, location: None, error: None }
57 }
58
59 pub fn drop(message: impl Into<String>) -> Self {
60 Self::new(ErrorAction::Drop, message)
61 }
62
63 pub fn ignore(message: impl Into<String>) -> Self {
64 Self::new(ErrorAction::Ignore, message)
65 }
66
67 pub fn with_node(mut self, node: u32) -> Self {
68 self.node = if node == 0 { None } else { Some(node) };
69 self
70 }
71
72 pub fn with_syntax(mut self, syntax: impl Into<Rc<str>>) -> Self {
73 self.syntax = Some(ErrorSyntax::Text(syntax.into()));
74 self
75 }
76
77 pub fn with_location(mut self, src: Option<Rc<str>>, line: u32, column: u32) -> Self {
78 self.location = Some((src, line, column));
79 self
80 }
81}
82
83#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
85pub struct SourceLocation {
86 pub sta: u32,
87 pub end: u32,
88 pub src_id: u32,
89}
90
91impl ErrorDescription {
92 pub fn to_plain(&self) -> crate::plain::Plain {
94 use crate::plain::{Plain, PlainObject};
95 let mut o = PlainObject::new();
96 o.set("action", self.action.as_str());
97 o.set("message", self.message.as_str());
98 if let Some(syntax) = &self.syntax {
99 match syntax {
100 ErrorSyntax::Text(s) => o.set("syntax", s.clone()),
101 ErrorSyntax::Token(token) => o.set("syntax", token.to_plain()),
102 ErrorSyntax::Object(plain) => o.set("syntax", (**plain).clone()),
103 }
104 }
105 if let Some(node) = self.node {
106 o.set("node", node);
107 }
108 if let Some((src, line, column)) = &self.location {
109 o.set("location", Plain::Array(vec![src.clone().into(), (*line).into(), (*column).into()]));
110 }
111 if let Some(error) = &self.error {
112 let mut e = PlainObject::new();
113 e.set("message", error.as_str());
114 o.set("error", e);
115 }
116 Plain::Object(o)
117 }
118
119 pub fn from_plain(plain: &crate::plain::Plain) -> Self {
121 let action = if plain.get("action").as_str() == Some("ignore") { ErrorAction::Ignore } else { ErrorAction::Drop };
122 let mut error = ErrorDescription::new(action, plain.get("message").as_str().unwrap_or(""));
123 match plain.get("syntax") {
124 crate::plain::Plain::String(s) => error.syntax = Some(ErrorSyntax::Text(s.clone())),
125 p @ crate::plain::Plain::Object(_) => error.syntax = Some(ErrorSyntax::Object(Rc::new(p.clone()))),
126 _ => {}
127 }
128 if let Some(n) = plain.get("node").as_f64() {
129 error.node = Some(n as u32);
130 }
131 error
132 }
133}