Skip to main content

sim_lib_doc_core/
error.rs

1//! Error type shared by the office document core.
2
3use std::fmt;
4
5use sim_kernel::CapabilityName;
6
7/// Error reported by the office document core.
8#[derive(Clone, Debug)]
9pub enum OfficeError {
10    /// A kernel factory failed while building a shape value.
11    ShapeBuild(String),
12    /// A capability was required but absent from the active context.
13    CapabilityDenied(CapabilityName),
14    /// The kernel reported an error that does not have a narrower office-core
15    /// category yet.
16    Kernel(String),
17    /// A compressed office package exceeded one of the bounded reader limits.
18    PackageTooLarge {
19        /// The limit that rejected the package.
20        limit: &'static str,
21        /// Package or entry details useful to the caller.
22        detail: String,
23    },
24    /// A domain edit could not be applied.
25    DomainEdit(String),
26    /// A document site could not be resolved or run.
27    Site(String),
28    /// A document surface scene or intent could not be projected or decoded.
29    Surface(String),
30}
31
32impl fmt::Display for OfficeError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::ShapeBuild(message) => write!(f, "could not build document shape: {message}"),
36            Self::CapabilityDenied(capability) => write!(f, "capability denied: {capability}"),
37            Self::Kernel(message) => write!(f, "kernel error: {message}"),
38            Self::PackageTooLarge { limit, detail } => {
39                write!(f, "office package exceeds {limit} limit: {detail}")
40            }
41            Self::DomainEdit(message) => write!(f, "domain edit failed: {message}"),
42            Self::Site(message) => write!(f, "document site error: {message}"),
43            Self::Surface(message) => write!(f, "document surface error: {message}"),
44        }
45    }
46}
47
48impl std::error::Error for OfficeError {}
49
50impl From<sim_kernel::Error> for OfficeError {
51    fn from(error: sim_kernel::Error) -> Self {
52        match error {
53            sim_kernel::Error::CapabilityDenied { capability } => {
54                Self::CapabilityDenied(capability)
55            }
56            other => Self::Kernel(other.to_string()),
57        }
58    }
59}