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
错误辅助函数
impl Error
错误辅助函数
Sourcepub fn other<S: Into<String>>(msg: S) -> Self
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 Error for Error
impl Error for Error
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
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
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
Source§impl From<AudioError> for Error
从音频错误转换
impl From<AudioError> for Error
从音频错误转换
Source§fn from(err: AudioError) -> Self
fn from(err: AudioError) -> 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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more