mirror of
https://github.com/cccb/servicepoint.git
synced 2025-01-18 10:00:14 +01:00
use thiserror for more errors
This commit is contained in:
parent
0ca1b67cb6
commit
2e8a61b627
|
@ -4,7 +4,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use servicepoint::{bitvec::prelude::BitVec, *};
|
use servicepoint::*;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Cli {
|
struct Cli {
|
||||||
|
|
|
@ -212,21 +212,27 @@ pub enum Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Err values for [Command::try_from].
|
/// Err values for [Command::try_from].
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, thiserror::Error)]
|
||||||
pub enum TryFromPacketError {
|
pub enum TryFromPacketError {
|
||||||
/// the contained command code does not correspond to a known command
|
/// the contained command code does not correspond to a known command
|
||||||
|
#[error("The command code {0:?} does not correspond to a known command")]
|
||||||
InvalidCommand(u16),
|
InvalidCommand(u16),
|
||||||
/// the expected payload size was n, but size m was found
|
/// the expected payload size was n, but size m was found
|
||||||
|
#[error("the expected payload size was {0}, but size {1} was found")]
|
||||||
UnexpectedPayloadSize(usize, usize),
|
UnexpectedPayloadSize(usize, usize),
|
||||||
/// Header fields not needed for the command have been used.
|
/// Header fields not needed for the command have been used.
|
||||||
///
|
///
|
||||||
/// Note that these commands would usually still work on the actual display.
|
/// Note that these commands would usually still work on the actual display.
|
||||||
|
#[error("Header fields not needed for the command have been used")]
|
||||||
ExtraneousHeaderValues,
|
ExtraneousHeaderValues,
|
||||||
/// The contained compression code is not known. This could be of disabled features.
|
/// The contained compression code is not known. This could be of disabled features.
|
||||||
|
#[error("The compression code {0:?} does not correspond to a known compression algorithm.")]
|
||||||
InvalidCompressionCode(u16),
|
InvalidCompressionCode(u16),
|
||||||
/// Decompression of the payload failed. This can be caused by corrupted packets.
|
/// Decompression of the payload failed. This can be caused by corrupted packets.
|
||||||
|
#[error("The decompression of the payload failed")]
|
||||||
DecompressionFailed,
|
DecompressionFailed,
|
||||||
/// The given brightness value is out of bounds
|
/// The given brightness value is out of bounds
|
||||||
|
#[error("The given brightness value {0} is out of bounds.")]
|
||||||
InvalidBrightness(u8),
|
InvalidBrightness(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::fmt::Debug;
|
|
||||||
use crate::packet::Packet;
|
use crate::packet::Packet;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
/// A connection to the display.
|
/// A connection to the display.
|
||||||
///
|
///
|
||||||
|
@ -34,20 +34,26 @@ pub enum Connection {
|
||||||
/// [servicepoint-websocket-relay]: https://github.com/kaesaecracker/servicepoint-websocket-relay
|
/// [servicepoint-websocket-relay]: https://github.com/kaesaecracker/servicepoint-websocket-relay
|
||||||
#[cfg(feature = "protocol_websocket")]
|
#[cfg(feature = "protocol_websocket")]
|
||||||
WebSocket(
|
WebSocket(
|
||||||
std::sync::Arc<std::sync::Mutex<tungstenite::WebSocket<
|
std::sync::Arc<
|
||||||
tungstenite::stream::MaybeTlsStream<std::net::TcpStream>,
|
std::sync::Mutex<
|
||||||
>>>,
|
tungstenite::WebSocket<
|
||||||
|
tungstenite::stream::MaybeTlsStream<std::net::TcpStream>,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
),
|
),
|
||||||
|
|
||||||
/// A fake connection for testing that does not actually send anything.
|
/// A fake connection for testing that does not actually send anything.
|
||||||
Fake,
|
Fake,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum SendError {
|
pub enum SendError {
|
||||||
IoError(std::io::Error),
|
#[error("IO error occurred while sending")]
|
||||||
|
IoError(#[from] std::io::Error),
|
||||||
#[cfg(feature = "protocol_websocket")]
|
#[cfg(feature = "protocol_websocket")]
|
||||||
WebsocketError(tungstenite::Error),
|
#[error("WebSocket error occurred while sending")]
|
||||||
|
WebsocketError(#[from] tungstenite::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Connection {
|
impl Connection {
|
||||||
|
@ -103,7 +109,9 @@ impl Connection {
|
||||||
|
|
||||||
let request = ClientRequestBuilder::new(uri).into_client_request()?;
|
let request = ClientRequestBuilder::new(uri).into_client_request()?;
|
||||||
let (sock, _) = connect(request)?;
|
let (sock, _) = connect(request)?;
|
||||||
Ok(Self::WebSocket(std::sync::Arc::new(std::sync::Mutex::new(sock))))
|
Ok(Self::WebSocket(std::sync::Arc::new(std::sync::Mutex::new(
|
||||||
|
sock,
|
||||||
|
))))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send something packet-like to the display. Usually this is in the form of a Command.
|
/// Send something packet-like to the display. Usually this is in the form of a Command.
|
||||||
|
@ -157,7 +165,9 @@ impl Drop for Connection {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
#[cfg(feature = "protocol_websocket")]
|
#[cfg(feature = "protocol_websocket")]
|
||||||
if let Connection::WebSocket(sock) = self {
|
if let Connection::WebSocket(sock) = self {
|
||||||
_ = sock.try_lock().map(move |mut sock| sock.close(None).unwrap() );
|
_ = sock
|
||||||
|
.try_lock()
|
||||||
|
.map(move |mut sock| sock.close(None).unwrap());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,14 @@ use std::collections::HashMap;
|
||||||
/// The encoding is currently not enforced.
|
/// The encoding is currently not enforced.
|
||||||
pub type Cp437Grid = PrimitiveGrid<u8>;
|
pub type Cp437Grid = PrimitiveGrid<u8>;
|
||||||
|
|
||||||
/// Errors that can occur when loading CP-437.
|
/// The error occurring when loading an invalid character
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, thiserror::Error)]
|
||||||
pub enum Cp437LoadError {
|
#[error("The character {char:?} at position {index} is not a valid CP437 character")]
|
||||||
/// Invalid character in input prevented loading
|
pub struct InvalidCharError {
|
||||||
InvalidChar {
|
/// invalid character is at this position in input
|
||||||
/// invalid character is at this position in input
|
index: usize,
|
||||||
index: usize,
|
/// the invalid character
|
||||||
/// the invalid character
|
char: char,
|
||||||
char: char,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cp437Grid {
|
impl Cp437Grid {
|
||||||
|
@ -33,7 +31,7 @@ impl Cp437Grid {
|
||||||
value: &str,
|
value: &str,
|
||||||
width: usize,
|
width: usize,
|
||||||
wrap: bool,
|
wrap: bool,
|
||||||
) -> Result<Self, Cp437LoadError> {
|
) -> Result<Self, InvalidCharError> {
|
||||||
assert!(width > 0);
|
assert!(width > 0);
|
||||||
assert!(!value.is_empty());
|
assert!(!value.is_empty());
|
||||||
|
|
||||||
|
@ -43,7 +41,7 @@ impl Cp437Grid {
|
||||||
|
|
||||||
for (index, char) in value.chars().enumerate() {
|
for (index, char) in value.chars().enumerate() {
|
||||||
if !char.is_ascii() {
|
if !char.is_ascii() {
|
||||||
return Err(Cp437LoadError::InvalidChar { index, char });
|
return Err(InvalidCharError { index, char });
|
||||||
}
|
}
|
||||||
|
|
||||||
let is_lf = char == '\n';
|
let is_lf = char == '\n';
|
||||||
|
@ -199,7 +197,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn load_ascii_invalid() {
|
fn load_ascii_invalid() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Err(Cp437LoadError::InvalidChar {
|
Err(InvalidCharError {
|
||||||
char: '🥶',
|
char: '🥶',
|
||||||
index: 2
|
index: 2
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in a new issue