Skip to main content

link_cli/protocol/
error.rs

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