1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! The error every fallible SDK call returns.
use alloc::string::String;
use core::fmt;
use crate::surface::Model;
/// Why a host call or a surface operation did not run.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// The manifest does not carry the grant the call needs. `grant` names
/// it (`values.read:git.main`, `account.read`, ...): add it to
/// `standard-plugin.json` with a reason.
GrantDenied { grant: String },
/// The per-plugin rate limit for this call kind was reached.
RateLimited,
/// The backing service is not reachable right now.
Unavailable(String),
/// The host rejected the arguments (an unknown surface id, an oversized
/// value, a key outside the plugin's namespace).
Invalid(String),
/// A payload did not serialize or deserialize.
Json(String),
/// The surface has no buffer yet: the viewer has not laid it out.
NotAttached { surface: String },
/// The manifest declares the surface in the other model.
WrongModel {
surface: String,
expected: Model,
found: Model,
},
}
/// `Result<T, Error>`.
pub type Result<T, E = Error> = core::result::Result<T, E>;
impl Error {
/// The missing grant, for a [`Error::GrantDenied`].
pub fn denied_grant(&self) -> Option<&str> {
match self {
Self::GrantDenied { grant } => Some(grant),
_ => None,
}
}
pub(crate) fn json(error: serde_json::Error) -> Self {
Self::Json(alloc::format!("{error}"))
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::GrantDenied { grant } => write!(f, "grant denied: {grant}"),
Self::RateLimited => f.write_str("rate limited"),
Self::Unavailable(reason) => write!(f, "unavailable: {reason}"),
Self::Invalid(reason) => write!(f, "invalid: {reason}"),
Self::Json(reason) => write!(f, "json: {reason}"),
Self::NotAttached { surface } => write!(f, "surface {surface:?} has no buffer yet"),
Self::WrongModel {
surface,
expected,
found,
} => write!(
f,
"surface {surface:?} is declared as {} but used as {}",
found.as_str(),
expected.as_str()
),
}
}
}
impl core::error::Error for Error {}