tauri_plugin_tts/
error.rs1use serde::{ser::Serializer, Serialize};
2
3use crate::models::ValidationError;
4
5pub type Result<T> = std::result::Result<T, Error>;
6
7#[derive(Debug, thiserror::Error)]
8pub enum Error {
9 #[error(transparent)]
10 Io(#[from] std::io::Error),
11
12 #[cfg(mobile)]
13 #[error(transparent)]
14 PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
15
16 #[cfg(desktop)]
17 #[error("TTS error: {0}")]
18 Tts(#[from] tts::Error),
19
20 #[error("Failed to acquire lock on TTS engine")]
21 LockError,
22
23 #[error("TTS engine mutex was poisoned - internal state may be corrupted")]
24 MutexPoisoned,
25
26 #[error("TTS not initialized")]
27 NotInitialized,
28
29 #[error("Validation error: {0}")]
30 Validation(#[from] ValidationError),
31
32 #[error("TTS operation failed: {0}")]
33 OperationFailed(String),
34}
35
36impl Error {
37 pub fn code(&self) -> &'static str {
38 match self {
39 Error::Io(_) => "IO_ERROR",
40 #[cfg(mobile)]
41 Error::PluginInvoke(_) => "PLUGIN_INVOKE_ERROR",
42 #[cfg(desktop)]
43 Error::Tts(_) => "TTS_ENGINE_ERROR",
44 Error::LockError => "LOCK_ERROR",
45 Error::MutexPoisoned => "MUTEX_POISONED",
46 Error::NotInitialized => "NOT_INITIALIZED",
47 Error::Validation(_) => "VALIDATION_ERROR",
48 Error::OperationFailed(_) => "OPERATION_FAILED",
49 }
50 }
51}
52
53impl Serialize for Error {
54 fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
55 where
56 S: Serializer,
57 {
58 use serde::ser::SerializeStruct;
59
60 let mut state = serializer.serialize_struct("Error", 2)?;
61 state.serialize_field("code", self.code())?;
62 state.serialize_field("message", &self.to_string())?;
63 state.end()
64 }
65}