Skip to content

Commit

Permalink
generate std Error and display messages for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lebedec committed Mar 13, 2023
1 parent 79c0bc8 commit a5b2924
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libfmod"
version = "2.2.606"
version = "2.2.607"
edition = "2021"
license = "MIT"
description = "A library wrapper for integrating FMOD Engine in Rust applications."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ For example for FMOD Engine 2.02.06 you should use:

```toml
[dependencies]
libfmod = "2.2.606"
libfmod = "2.2.607"
```

#### FMOD Development Libraries
Expand Down
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(unused_unsafe)]

use std::ffi::{c_void, CStr, CString, IntoStringError, NulError};
use std::fmt::{Display, Formatter};
use std::mem::size_of;
use std::os::raw::c_char;
use std::ptr::{null, null_mut};
Expand All @@ -24,6 +25,41 @@ pub enum Error {
NotDspFft,
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::Fmod {
function,
code,
message,
} => {
write!(f, "{}: {} ({})", function, message, code)
}
Error::EnumBindgen { enumeration, value } => {
write!(
f,
"FMOD returns unexpected value {} for {} enum",
value, enumeration
)
}
Error::String(_) => {
write!(f, "invalid UTF-8 when converting C string")
}
Error::StringNul(_) => {
write!(
f,
"nul byte was found in the middle, C strings can't contain it"
)
}
Error::NotDspFft => {
write!(f, "trying get FFT from DSP which not FFT")
}
}
}
}

impl std::error::Error for Error {}

impl From<NulError> for Error {
fn from(error: NulError) -> Self {
Error::StringNul(error)
Expand Down
15 changes: 15 additions & 0 deletions tests/manual/dsp_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ fn test_fft_dsp() -> Result<(), Error> {
system.release()
}

#[test]
fn test_fft_from_type_mismatched_dsp() -> Result<(), Error> {
let system = System::create()?;
system.init(512, FMOD_INIT_NORMAL, None)?;

let sound = system.create_sound("./data/beep.wav", FMOD_DEFAULT, None)?;
let channel = system.play_sound(sound, None, false)?;
let echo_dsp = system.create_dsp_by_type(DspType::Echo)?;
channel.add_dsp(0, echo_dsp)?;
let fft = DspParameterFft::try_from(echo_dsp);
assert_eq!("trying get FFT from DSP which not FFT", format!("{}", fft.err().unwrap()));

system.release()
}

#[test]
fn test_dps_description_name() -> Result<(), Error> {
let system = System::create()?;
Expand Down

0 comments on commit a5b2924

Please sign in to comment.