coqui_stt/
token_metadata.rsuse std::borrow::{Borrow, Cow};
use std::ffi::CStr;
use std::fmt::{Debug, Display, Formatter};
#[repr(transparent)]
pub struct TokenMetadata {
ptr: coqui_stt_sys::TokenMetadata,
}
unsafe impl Send for TokenMetadata {}
unsafe impl Sync for TokenMetadata {}
impl TokenMetadata {
#[inline]
#[must_use]
pub fn text(&self) -> Cow<str> {
let cstr = unsafe { CStr::from_ptr(self.ptr.text) };
cstr.to_string_lossy()
}
#[inline]
#[must_use]
pub const fn timestep(&self) -> u32 {
self.ptr.timestep
}
#[inline]
#[must_use]
pub const fn start_time(&self) -> f32 {
self.ptr.start_time
}
#[inline]
#[must_use]
pub fn to_owned(&self) -> OwnedTokenMetadata {
let coqui_stt_sys::TokenMetadata {
timestep,
start_time,
..
} = self.ptr;
let text = self.text().to_string();
OwnedTokenMetadata {
text,
timestep,
start_time,
}
}
}
impl Debug for TokenMetadata {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TokenMetadata")
.field("text", &self.text())
.field("timestep", &self.timestep())
.field("start_time", &self.start_time())
.finish()
}
}
impl Display for TokenMetadata {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(self.text().borrow())
}
}
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct OwnedTokenMetadata {
pub text: String,
pub timestep: u32,
pub start_time: f32,
}
impl Display for OwnedTokenMetadata {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.text)
}
}