]> code.octet-stream.net Git - m17rt/blob - m17app/src/error.rs
Add error handling
[m17rt] / m17app / src / error.rs
1 use std::{fmt::Display, path::PathBuf};
2
3 use thiserror::Error;
4
5 /// Errors from the M17 Rust Toolkit
6 #[derive(Debug, Error)]
7 pub enum M17Error {
8 #[error("given callsign contains at least one character invalid in M17: {0}")]
9 InvalidCallsignCharacters(char),
10
11 #[error("given callsign is {0} characters long; maximum is 9")]
12 CallsignTooLong(usize),
13
14 #[error("error during soundcard initialisation")]
15 SoundcardInit,
16
17 #[error("unable to locate sound card '{0}' - is it in use?")]
18 SoundcardNotFound(String),
19
20 #[error("unable to set up RTL-SDR receiver")]
21 RtlSdrInit,
22
23 #[error(
24 "provided packet payload is too large: provided {provided} bytes, capacity {capacity}"
25 )]
26 PacketTooLarge { provided: usize, capacity: usize },
27
28 #[error("provided path to RRC file could not be opened: {0}")]
29 InvalidRrcPath(PathBuf),
30
31 #[error("failed to read from RRC file: {0}")]
32 RrcReadFailed(PathBuf),
33
34 #[error("tried to start app more than once")]
35 InvalidStart,
36
37 #[error("tried to close app that is not started")]
38 InvalidClose,
39
40 #[error("adapter error for id {0}: {1}")]
41 Adapter(usize, #[source] AdapterError),
42
43 #[error("soundmodem component error: {0}")]
44 Soundmodem(#[source] SoundmodemError),
45 }
46
47 /// Arbitrary error type returned from adapters, which may be user-implemented
48 pub type AdapterError = Box<dyn std::error::Error + Sync + Send + 'static>;
49
50 /// Arbitrary error type returned from soundmodem components, which may be user-implemented
51 pub type SoundmodemError = Box<dyn std::error::Error + Sync + Send + 'static>;
52
53 /// Iterator over potentially multiple errors
54 #[derive(Debug, Error)]
55 pub struct M17Errors(pub(crate) Vec<M17Error>);
56 impl Iterator for M17Errors {
57 type Item = M17Error;
58
59 fn next(&mut self) -> Option<Self::Item> {
60 self.0.pop()
61 }
62 }
63
64 impl Display for M17Errors {
65 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66 let mut displays = vec![];
67 for e in &self.0 {
68 displays.push(e.to_string());
69 }
70 write!(f, "[{}]", displays.join(", "))
71 }
72 }
73
74 #[derive(Debug, Error)]
75 pub enum M17SoundmodemError {}