use transcribe_cpp_sys as sys;
use crate::family::{StreamExtRaw, StreamExtension};
use crate::result::owned_str;
use crate::types::CommitPolicy;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(default)
)]
pub struct StreamOptions {
pub commit_policy: CommitPolicy,
pub stable_prefix_agreement_n: u32,
pub family: Option<StreamExtension>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(default)
)]
pub struct StreamUpdate {
pub result_changed: bool,
pub is_final: bool,
pub revision: i32,
pub input_received_ms: i64,
pub audio_committed_ms: i64,
pub buffered_ms: i64,
pub committed_changed: bool,
pub tentative_changed: bool,
}
impl StreamUpdate {
pub(crate) fn from_raw(raw: &sys::transcribe_stream_update) -> Self {
StreamUpdate {
result_changed: raw.result_changed,
is_final: raw.is_final,
revision: raw.revision,
input_received_ms: raw.input_received_ms,
audio_committed_ms: raw.audio_committed_ms,
buffered_ms: raw.buffered_ms,
committed_changed: raw.committed_changed,
tentative_changed: raw.tentative_changed,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(default)
)]
pub struct StreamText {
pub full: String,
pub committed: String,
pub tentative: String,
}
impl StreamText {
pub fn display(&self) -> String {
format!("{}{}", self.committed, self.tentative)
}
pub(crate) fn from_raw(raw: &sys::transcribe_stream_text) -> Self {
StreamText {
full: owned_str(raw.full_text),
committed: owned_str(raw.committed_text),
tentative: owned_str(raw.tentative_text),
}
}
}
pub(crate) fn build_stream_params(
o: &StreamOptions,
) -> (sys::transcribe_stream_params, Option<StreamExtRaw>) {
let mut params: sys::transcribe_stream_params = unsafe { std::mem::zeroed() };
unsafe { sys::transcribe_stream_params_init(&mut params) };
params.commit_policy = o.commit_policy.to_raw();
params.stable_prefix_agreement_n = o.stable_prefix_agreement_n;
let family = o.family.as_ref().map(StreamExtension::materialize);
params.family = family.as_ref().map_or(std::ptr::null(), |f| f.ext_ptr());
(params, family)
}