sass_embedded_host_rust/
error.rs

1use std::fmt::Display;
2
3use crate::protocol::{
4  outbound_message::compile_response::CompileFailure, ProtocolError, SourceSpan,
5};
6
7pub type Result<T> = std::result::Result<T, Exception>;
8
9#[derive(Debug, Clone)]
10pub struct Exception {
11  message: String,
12  sass_message: Option<String>,
13  sass_stack: Option<String>,
14  span: Option<SourceSpan>,
15}
16
17impl Exception {
18  pub fn message(&self) -> &str {
19    &self.message
20  }
21
22  pub fn sass_message(&self) -> Option<&str> {
23    self.sass_message.as_deref()
24  }
25
26  pub fn sass_stack(&self) -> Option<&str> {
27    self.sass_stack.as_deref()
28  }
29
30  pub fn span(&self) -> Option<&SourceSpan> {
31    self.span.as_ref()
32  }
33}
34
35impl std::error::Error for Exception {}
36
37impl Display for Exception {
38  /// https://sass-lang.com/documentation/js-api/classes/Exception#toString
39  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40    write!(f, "{}", self.message)
41  }
42}
43
44impl From<CompileFailure> for Exception {
45  fn from(failure: CompileFailure) -> Self {
46    Self {
47      message: failure.formatted,
48      sass_message: Some(failure.message),
49      sass_stack: Some(failure.stack_trace),
50      span: failure.span,
51    }
52  }
53}
54
55impl From<ProtocolError> for Exception {
56  fn from(e: ProtocolError) -> Self {
57    Self::new(e.message)
58  }
59}
60
61impl Exception {
62  pub fn new(message: impl Into<String>) -> Self {
63    Self {
64      message: message.into(),
65      sass_message: None,
66      sass_stack: None,
67      span: None,
68    }
69  }
70}