Skip to main content

link_cli/protocol/
error.rs

1//! Errors raised by the LiNo network protocols.
2
3use std::io;
4use thiserror::Error;
5
6/// Everything that can go wrong while encoding, decoding or exchanging a
7/// LiNo message.
8#[derive(Debug, Error)]
9pub enum ProtocolError {
10    /// The underlying transport failed.
11    #[error("I/O error: {0}")]
12    Io(#[from] io::Error),
13
14    /// The peer sent bytes that are not a valid message.
15    #[error("malformed message: {0}")]
16    Malformed(String),
17
18    /// The message text is not valid LiNo.
19    #[error("invalid LiNo: {0}")]
20    InvalidLino(String),
21
22    /// The message exceeds one of the configured [`DecodeLimits`](super::DecodeLimits).
23    #[error("limit exceeded: {0}")]
24    LimitExceeded(String),
25
26    /// The document cannot be represented with the chosen options.
27    #[error("cannot encode: {0}")]
28    Unencodable(String),
29
30    /// The server answered with an `(error: …)` document.
31    #[error("server error: {0}")]
32    Remote(String),
33}
34
35impl ProtocolError {
36    pub(crate) fn malformed(message: impl Into<String>) -> Self {
37        Self::Malformed(message.into())
38    }
39}
40
41/// Result alias used throughout [`crate::protocol`].
42pub type ProtocolResult<T> = Result<T, ProtocolError>;