Skip to main content

wackasm_opencomputers/
error.rs

1use std::fmt;
2
3/// Why a component call failed.
4#[derive(Clone, Debug, PartialEq)]
5pub enum Error {
6    /// No component with that address is connected to this computer.
7    NoSuchComponent,
8    /// The component has no such method.
9    NoSuchMethod,
10    /// The component refused the call (a bad argument, or an exception
11    /// inside it), or reported failure the Lua way: `nil, "reason"`.
12    Failed(String),
13    /// The reply wasn't the shape the method's documentation promised.
14    UnexpectedReply(String),
15    /// No program by that name on any disk (see [`crate::process::Command`]).
16    ProgramNotFound(String),
17}
18
19impl fmt::Display for Error {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Error::NoSuchComponent => f.write_str("no such component"),
23            Error::NoSuchMethod => f.write_str("no such method"),
24            Error::Failed(reason) => f.write_str(reason),
25            Error::UnexpectedReply(what) => write!(f, "unexpected reply: {what}"),
26            Error::ProgramNotFound(name) => write!(f, "{name}: command not found"),
27        }
28    }
29}
30
31impl std::error::Error for Error {}
32
33pub type Result<T> = std::result::Result<T, Error>;