Error

Enum Error 

Source
pub enum Error {
    Audio(AudioError),
    Stt(SttError),
    Io(Error),
    Other(String),
}
Expand description

统一错误类型

这是整个语音工具库的主要错误类型,封装了所有可能的错误情况。 使用特性标志来控制不同错误类型的可用性。

Variants§

§

Audio(AudioError)

音频处理错误

当音频格式转换、重采样或元数据提取失败时返回此错误。 需要 audio 特性标志。

§示例

#[cfg(feature = "audio")]
fn handle_audio_error(err: voice_toolkit::Error) {
    if let voice_toolkit::Error::Audio(audio_err) = err {
        println!("音频处理失败: {}", audio_err);
    }
}
§

Stt(SttError)

语音转文本错误

当 Whisper 模型加载、文件转录或流式处理失败时返回此错误。 需要 stt 特性标志。

§示例

#[cfg(feature = "stt")]
fn handle_stt_error(err: voice_toolkit::Error) {
    if let voice_toolkit::Error::Stt(stt_err) = err {
        println!("语音识别失败: {}", stt_err);
    }
}
§

Io(Error)

IO错误

当文件读取、写入或其他 IO 操作失败时返回此错误。 这是常见的错误类型,通常由文件不存在、权限不足等原因引起。

§示例

fn handle_io_error(err: voice_toolkit::Error) {
    if let voice_toolkit::Error::Io(io_err) = err {
        match io_err.kind() {
            std::io::ErrorKind::NotFound => println!("文件不存在"),
            std::io::ErrorKind::PermissionDenied => println!("权限不足"),
            _ => println!("IO错误: {}", io_err),
        }
    }
}
§

Other(String)

其他错误

用于处理未分类的其他错误情况。通常用于包装字符串错误消息 或其他不常见的情况。

§示例

fn handle_other_error(err: voice_toolkit::Error) {
    if let voice_toolkit::Error::Other(msg) = err {
        println!("其他错误: {}", msg);
    }
}

Implementations§

Source§

impl Error

错误辅助函数

Source

pub fn other<S: Into<String>>(msg: S) -> Self

创建其他错误

这是一个便利函数,用于创建 Error::Other 变体。 适用于需要从字符串或其他可转换为字符串的类型创建错误的情况。

§参数
  • msg - 错误消息,可以是任何可转换为 String 的类型
§返回值

返回 Error::Other 变体,包含提供的错误消息。

§示例
use voice_toolkit::Error;
 
fn validate_input(input: &str) -> Result<(), Error> {
    if input.is_empty() {
        return Err(Error::other("输入不能为空"));
    }
    Ok(())
}
 
// 也可以直接使用字符串字面量
let error = Error::other("自定义错误消息");
§使用场景
  • 验证输入参数
  • 业务逻辑错误
  • 不适合归类到其他错误类型的情况

Trait Implementations§

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<&str> for Error

从&str转换

Source§

fn from(err: &str) -> Self

Converts to this type from the input type.
Source§

impl From<AudioError> for Error

从音频错误转换

Source§

fn from(err: AudioError) -> Self

Converts to this type from the input type.
Source§

impl From<Error> for Error

Source§

fn from(source: Error) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Error

从字符串转换

Source§

fn from(err: String) -> Self

Converts to this type from the input type.
Source§

impl From<SttError> for Error

从STT错误转换

Source§

fn from(err: SttError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Error

§

impl !RefUnwindSafe for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl !UnwindSafe for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.