voice_toolkit/
error.rs

1//! 统一错误处理模块
2//!
3//! 提供跨所有语音工具模块的统一错误类型
4
5use thiserror::Error;
6
7/// 统一错误类型
8#[derive(Error, Debug)]
9pub enum Error {
10    /// 音频处理错误
11    #[cfg(feature = "audio")]
12    #[error("音频错误: {0}")]
13    Audio(rs_voice_toolkit_audio::AudioError),
14
15    /// 语音转文本错误
16    #[cfg(feature = "stt")]
17    #[error("语音识别错误: {0}")]
18    Stt(rs_voice_toolkit_stt::SttError),
19
20    /// 文本转语音错误
21    #[cfg(feature = "tts")]
22    #[error("语音合成错误: {0}")]
23    Tts(rs_voice_toolkit_tts::TtsError),
24
25    /// IO错误
26    #[error("IO错误: {0}")]
27    Io(#[from] std::io::Error),
28
29    /// 其他错误
30    #[error("其他错误: {0}")]
31    Other(String),
32}
33
34/// 统一结果类型别名
35pub type Result<T> = std::result::Result<T, Error>;
36
37/// 错误辅助函数
38impl Error {
39    /// 创建其他错误
40    pub fn other<S: Into<String>>(msg: S) -> Self {
41        Error::Other(msg.into())
42    }
43}
44
45/// 从字符串转换
46impl From<String> for Error {
47    fn from(err: String) -> Self {
48        Error::Other(err)
49    }
50}
51
52/// 从&str转换
53impl From<&str> for Error {
54    fn from(err: &str) -> Self {
55        Error::Other(err.to_string())
56    }
57}
58
59/// 从STT错误转换
60#[cfg(feature = "stt")]
61impl From<rs_voice_toolkit_stt::SttError> for Error {
62    fn from(err: rs_voice_toolkit_stt::SttError) -> Self {
63        Error::Stt(err)
64    }
65}
66
67/// 从音频错误转换
68#[cfg(feature = "audio")]
69impl From<rs_voice_toolkit_audio::AudioError> for Error {
70    fn from(err: rs_voice_toolkit_audio::AudioError) -> Self {
71        Error::Audio(err)
72    }
73}
74
75/// 从TTS错误转换
76#[cfg(feature = "tts")]
77impl From<rs_voice_toolkit_tts::TtsError> for Error {
78    fn from(err: rs_voice_toolkit_tts::TtsError) -> Self {
79        Error::Tts(err)
80    }
81}