1use alloc::string::String;
4use core::fmt;
5
6use crate::surface::Model;
7
8#[derive(Clone, Debug, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12 GrantDenied { grant: String },
16 RateLimited,
18 Unavailable(String),
20 Invalid(String),
23 Json(String),
25 NotAttached { surface: String },
27 WrongModel {
29 surface: String,
30 expected: Model,
31 found: Model,
32 },
33}
34
35pub type Result<T, E = Error> = core::result::Result<T, E>;
37
38impl Error {
39 pub fn denied_grant(&self) -> Option<&str> {
41 match self {
42 Self::GrantDenied { grant } => Some(grant),
43 _ => None,
44 }
45 }
46
47 pub(crate) fn json(error: serde_json::Error) -> Self {
48 Self::Json(alloc::format!("{error}"))
49 }
50}
51
52impl fmt::Display for Error {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 Self::GrantDenied { grant } => write!(f, "grant denied: {grant}"),
56 Self::RateLimited => f.write_str("rate limited"),
57 Self::Unavailable(reason) => write!(f, "unavailable: {reason}"),
58 Self::Invalid(reason) => write!(f, "invalid: {reason}"),
59 Self::Json(reason) => write!(f, "json: {reason}"),
60 Self::NotAttached { surface } => write!(f, "surface {surface:?} has no buffer yet"),
61 Self::WrongModel {
62 surface,
63 expected,
64 found,
65 } => write!(
66 f,
67 "surface {surface:?} is declared as {} but used as {}",
68 found.as_str(),
69 expected.as_str()
70 ),
71 }
72 }
73}
74
75impl core::error::Error for Error {}