use alloc::boxed::Box;
use core::fmt;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum SourceMapError {
Oversize {
name: Box<str>,
len: u64,
},
SpaceExhausted {
needed: u64,
available: u64,
},
NotUtf8 {
name: Box<str>,
},
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
Io {
name: Box<str>,
kind: std::io::ErrorKind,
},
}
impl fmt::Display for SourceMapError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Oversize { name, len } => write!(
f,
"source `{name}` of {len} bytes exceeds the maximum source length",
),
Self::SpaceExhausted { needed, available } => write!(
f,
"source of {needed} bytes does not fit in the {available} bytes \
remaining in the global position space",
),
Self::NotUtf8 { name } => {
write!(f, "source `{name}` is not valid UTF-8")
}
#[cfg(feature = "std")]
Self::Io { name, kind } => write!(f, "source `{name}` could not be read: {kind}"),
}
}
}
impl core::error::Error for SourceMapError {}
#[cfg(test)]
mod tests {
extern crate alloc;
use alloc::boxed::Box;
use alloc::string::ToString;
use super::*;
#[test]
fn test_space_exhausted_display_names_both_figures() {
let err = SourceMapError::SpaceExhausted {
needed: 10,
available: 4,
};
let text = err.to_string();
assert!(text.contains("10 bytes"), "{text}");
assert!(text.contains("4 bytes"), "{text}");
}
#[test]
fn test_oversize_display_names_source_and_length() {
let err = SourceMapError::Oversize {
name: Box::from("big.rs"),
len: 5_000_000_000,
};
let text = err.to_string();
assert!(text.contains("big.rs"), "{text}");
assert!(text.contains("5000000000"), "{text}");
}
#[test]
fn test_not_utf8_display_names_source() {
let err = SourceMapError::NotUtf8 {
name: Box::from("blob.bin"),
};
assert!(err.to_string().contains("blob.bin"));
}
#[cfg(feature = "std")]
#[test]
fn test_io_display_names_source_and_kind() {
let err = SourceMapError::Io {
name: Box::from("missing.rs"),
kind: std::io::ErrorKind::NotFound,
};
let text = err.to_string();
assert!(text.contains("missing.rs"), "{text}");
}
#[test]
fn test_error_is_clonable_and_equatable() {
let a = SourceMapError::SpaceExhausted {
needed: 1,
available: 0,
};
let b = a.clone();
assert_eq!(a, b);
}
}