coqui_stt/
token_metadata.rs1use std::borrow::{Borrow, Cow};
2use std::ffi::CStr;
3use std::fmt::{Debug, Display, Formatter};
4
5#[repr(transparent)]
7pub struct TokenMetadata {
8 ptr: coqui_stt_sys::TokenMetadata,
9}
10
11unsafe impl Send for TokenMetadata {}
12unsafe impl Sync for TokenMetadata {}
13
14impl TokenMetadata {
15 #[inline]
17 #[must_use]
18 pub fn text(&self) -> Cow<str> {
19 let cstr = unsafe { CStr::from_ptr(self.ptr.text) };
21 cstr.to_string_lossy()
22 }
23
24 #[inline]
26 #[must_use]
27 pub const fn timestep(&self) -> u32 {
28 self.ptr.timestep
29 }
30
31 #[inline]
33 #[must_use]
34 pub const fn start_time(&self) -> f32 {
35 self.ptr.start_time
36 }
37
38 #[inline]
43 #[must_use]
44 pub fn to_owned(&self) -> OwnedTokenMetadata {
45 let coqui_stt_sys::TokenMetadata {
46 timestep,
47 start_time,
48 ..
49 } = self.ptr;
50
51 let text = self.text().to_string();
52
53 OwnedTokenMetadata {
54 text,
55 timestep,
56 start_time,
57 }
58 }
59}
60
61impl Debug for TokenMetadata {
62 #[allow(clippy::missing_inline_in_public_items)]
63 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
64 f.debug_struct("TokenMetadata")
65 .field("text", &self.text())
66 .field("timestep", &self.timestep())
67 .field("start_time", &self.start_time())
68 .finish()
69 }
70}
71
72impl Display for TokenMetadata {
73 #[inline]
74 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
75 f.write_str(self.text().borrow())
76 }
77}
78
79#[non_exhaustive]
81#[derive(Clone, Debug)]
82pub struct OwnedTokenMetadata {
83 pub text: String,
85 pub timestep: u32,
87 pub start_time: f32,
89}
90
91impl Display for OwnedTokenMetadata {
92 #[inline]
93 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
94 f.write_str(&self.text)
95 }
96}