transcribe_cpp_types/error.rs
1//! The plain-data side of transcribe-cpp's errors: [`ErrorKind`] and the
2//! [`ErrorReport`] snapshot. `transcribe_cpp::Error` itself (with its native
3//! status mapping) stays in transcribe-cpp, which converts it into an
4//! [`ErrorReport`].
5
6use crate::result::Transcript;
7
8/// The category of a `transcribe_cpp::Error`, without its payload. One kind
9/// per variant.
10///
11/// Stable for matching and, under the `serde` feature, serialized as a
12/// snake_case string (`"aborted"`, `"model_file_not_found"`, ...). A kind
13/// this version does not know deserializes as [`ErrorKind::Other`].
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15#[non_exhaustive]
16#[cfg_attr(
17 feature = "serde",
18 derive(serde::Serialize, serde::Deserialize),
19 serde(rename_all = "snake_case")
20)]
21pub enum ErrorKind {
22 InvalidArgument,
23 NotImplemented,
24 ModelFileNotFound,
25 ModelLoad,
26 OutOfMemory,
27 Backend,
28 Unsupported,
29 BadStructSize,
30 InputTooLong,
31 Aborted,
32 OutputTruncated,
33 OutputRepetition,
34 UnsupportedRole,
35 VersionMismatch,
36 Nul,
37 Busy,
38 InputTooShort,
39 #[cfg_attr(feature = "serde", serde(other))]
40 Other,
41}
42
43/// An owned, plain-data snapshot of a `transcribe_cpp::Error`: what crosses a
44/// process or IPC boundary (e.g. a Tauri command's error).
45///
46/// `transcribe_cpp::Error` itself serializes to exactly this shape under the
47/// `serde` feature, and converts into it with `From`; deserialize into
48/// `ErrorReport` on the receiving side (an `Error` cannot be rebuilt from it).
49/// JSON looks like:
50///
51/// ```json
52/// { "kind": "aborted", "message": "operation aborted: ...", "status": 13, "partial": { "text": "..." } }
53/// ```
54///
55/// `partial` is `null` unless the error carried a partial transcript. It is
56/// always written (never skipped) so non-self-describing formats such as
57/// postcard round-trip.
58#[derive(Debug, Clone, PartialEq)]
59#[non_exhaustive]
60#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
61pub struct ErrorReport {
62 /// The error category.
63 pub kind: ErrorKind,
64 /// The error's `Display` text.
65 pub message: String,
66 /// `transcribe_cpp::Error::raw_status`: the `transcribe_status`, or 0 for
67 /// Rust-side errors.
68 #[cfg_attr(feature = "serde", serde(default))]
69 pub status: i32,
70 /// `transcribe_cpp::Error::partial`, if any.
71 #[cfg_attr(feature = "serde", serde(default))]
72 pub partial: Option<Transcript>,
73}
74
75impl ErrorReport {
76 /// A report of `kind` with `message`, no native status (0) and no partial
77 /// transcript: for errors raised outside the engine, such as a host
78 /// rejecting a request before it reaches a worker. Set
79 /// [`status`](Self::status) and [`partial`](Self::partial) afterwards if
80 /// you have them.
81 ///
82 /// ```
83 /// use transcribe_cpp_types::{ErrorKind, ErrorReport};
84 /// let report = ErrorReport::new(ErrorKind::InputTooLong, "frame of 70000 samples exceeds 65536");
85 /// assert_eq!((report.status, report.partial), (0, None));
86 /// ```
87 pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
88 ErrorReport {
89 kind,
90 message: message.into(),
91 status: 0,
92 partial: None,
93 }
94 }
95}