Skip to content

Commit

Permalink
Create Error type to works with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAwiteb committed Feb 25, 2023
1 parent a9c8b35 commit 16e8024
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use std::fmt;

/// Result type for the library
pub type Result<T> = std::result::Result<T, Error>;

/// Error enum, used to return errors from the library.
#[derive(Debug)]
pub enum Error {
/// Error from the hound crate
Hound(hound::Error),
/// IO error, such as file not found
Io(std::io::Error),
/// Larg file size error (maxnimum file size is 4 GB)
LargeFileSize,
}

impl From<hound::Error> for Error {
fn from(err: hound::Error) -> Self {
Error::Hound(err)
}
}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Hound(err) => write!(f, "Hound error: {}", err),
Error::Io(err) => write!(f, "IO error: {}", err),
Error::LargeFileSize => write!(f, "File size is too large, maximum file size is 4 GB"),
}
}
}
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use std::{
path,
};

mod errors;
pub use errors::*;

/// The sample rate to use when encoding
pub const SAMPLE_RATE: u32 = 202860;
/// The result type for this crate, which is just a re-export of `hound::Result`
pub use hound::Result;

/// Encode file to wav file by converting the bytes to a sine wave, then writing the sine wave to a wav file
/// # Arguments
Expand Down

0 comments on commit 16e8024

Please sign in to comment.