audio_visualizer/error.rs
1/*
2MIT License
3
4Copyright (c) 2026 Philipp Schuster
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24//! The crate-wide [`Error`] type. All fallible operations of this crate
25//! return it instead of panicking.
26
27use std::fmt::{self, Display, Formatter};
28
29/// Errors that can occur while visualizing audio data.
30#[derive(Debug)]
31pub enum Error {
32 /// The input can't be visualized, e.g. it is empty or contains
33 /// non-finite values.
34 InvalidData(String),
35 /// Writing the output file failed.
36 Io(std::io::Error),
37 /// Rendering or encoding the chart failed.
38 Chart(charts_rs::Error),
39 /// Interacting with the audio backend failed, e.g. no input device found
40 /// or recording failed.
41 Audio(String),
42 /// Running the GUI window failed.
43 Gui(String),
44}
45
46impl Display for Error {
47 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
48 match self {
49 Self::InvalidData(msg) => write!(f, "invalid input data: {msg}"),
50 Self::Io(e) => write!(f, "I/O error: {e}"),
51 Self::Chart(e) => write!(f, "chart rendering error: {e}"),
52 Self::Audio(msg) => write!(f, "audio backend error: {msg}"),
53 Self::Gui(msg) => write!(f, "GUI error: {msg}"),
54 }
55 }
56}
57
58impl std::error::Error for Error {
59 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
60 match self {
61 Self::InvalidData(_) | Self::Audio(_) | Self::Gui(_) => None,
62 Self::Io(e) => Some(e),
63 Self::Chart(e) => Some(e),
64 }
65 }
66}
67
68impl From<std::io::Error> for Error {
69 fn from(e: std::io::Error) -> Self {
70 Self::Io(e)
71 }
72}
73
74impl From<charts_rs::Error> for Error {
75 fn from(e: charts_rs::Error) -> Self {
76 Self::Chart(e)
77 }
78}