use serde::{ser::SerializeSeq, Serialize};
#[derive(Debug, PartialEq, Clone)]
pub struct Options {
start: Option<String>,
end: Option<String>,
accessor: Option<String>,
tags: Vec<String>,
methods: Vec<Method>,
models: Vec<String>,
multichannel: Option<bool>,
interim_results: Option<bool>,
punctuate: Option<bool>,
ner: Option<bool>,
utterances: Option<bool>,
replace: Option<bool>,
profanity_filter: Option<bool>,
keywords: Option<bool>,
diarize: Option<bool>,
search: Option<bool>,
redact: Option<bool>,
alternatives: Option<bool>,
numerals: Option<bool>,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[non_exhaustive]
pub enum Method {
#[allow(missing_docs)]
Sync,
#[allow(missing_docs)]
Async,
#[allow(missing_docs)]
Streaming,
}
#[derive(Debug, PartialEq, Clone)]
pub struct OptionsBuilder(Options);
pub(crate) struct SerializableOptions<'a>(&'a Options);
impl Options {
pub fn builder() -> OptionsBuilder {
OptionsBuilder::new()
}
pub fn urlencoded(&self) -> Result<String, serde_urlencoded::ser::Error> {
serde_urlencoded::to_string(SerializableOptions::from(self))
}
}
impl OptionsBuilder {
pub fn new() -> Self {
Self(Options {
start: None,
end: None,
accessor: None,
tags: Vec::new(),
methods: Vec::new(),
models: Vec::new(),
multichannel: None,
interim_results: None,
punctuate: None,
ner: None,
utterances: None,
replace: None,
profanity_filter: None,
keywords: None,
diarize: None,
search: None,
redact: None,
alternatives: None,
numerals: None,
})
}
pub fn start(mut self, start: impl Into<String>) -> Self {
self.0.start = Some(start.into());
self
}
pub fn end(mut self, end: impl Into<String>) -> Self {
self.0.end = Some(end.into());
self
}
pub fn accessor(mut self, accessor: impl Into<String>) -> Self {
self.0.accessor = Some(accessor.into());
self
}
pub fn tag<'a>(mut self, tag: impl IntoIterator<Item = &'a str>) -> Self {
self.0.tags.extend(tag.into_iter().map(String::from));
self
}
pub fn method(mut self, method: impl IntoIterator<Item = Method>) -> Self {
self.0.methods.extend(method);
self
}
pub fn model<'a>(mut self, model: impl IntoIterator<Item = &'a str>) -> Self {
self.0.models.extend(model.into_iter().map(String::from));
self
}
pub fn multichannel(mut self, multichannel: bool) -> Self {
self.0.multichannel = Some(multichannel);
self
}
pub fn interim_results(mut self, interim_results: bool) -> Self {
self.0.interim_results = Some(interim_results);
self
}
pub fn punctuate(mut self, punctuate: bool) -> Self {
self.0.punctuate = Some(punctuate);
self
}
pub fn ner(mut self, ner: bool) -> Self {
self.0.ner = Some(ner);
self
}
pub fn utterances(mut self, utterances: bool) -> Self {
self.0.utterances = Some(utterances);
self
}
pub fn replace(mut self, replace: bool) -> Self {
self.0.replace = Some(replace);
self
}
pub fn profanity_filter(mut self, profanity_filter: bool) -> Self {
self.0.profanity_filter = Some(profanity_filter);
self
}
pub fn keywords(mut self, keywords: bool) -> Self {
self.0.keywords = Some(keywords);
self
}
pub fn diarize(mut self, diarize: bool) -> Self {
self.0.diarize = Some(diarize);
self
}
pub fn search(mut self, search: bool) -> Self {
self.0.search = Some(search);
self
}
pub fn redact(mut self, redact: bool) -> Self {
self.0.redact = Some(redact);
self
}
pub fn alternatives(mut self, alternatives: bool) -> Self {
self.0.alternatives = Some(alternatives);
self
}
pub fn numerals(mut self, numerals: bool) -> Self {
self.0.numerals = Some(numerals);
self
}
pub fn build(self) -> Options {
self.0
}
}
impl Default for OptionsBuilder {
fn default() -> Self {
Self::new()
}
}
impl<'a> From<&'a Options> for SerializableOptions<'a> {
fn from(options: &'a Options) -> Self {
Self(options)
}
}
impl Serialize for SerializableOptions<'_> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(None)?;
let Options {
start,
end,
accessor,
tags,
methods,
models,
multichannel,
interim_results,
punctuate,
ner,
utterances,
replace,
profanity_filter,
keywords,
diarize,
search,
redact,
alternatives,
numerals,
} = self.0;
if let Some(start) = start {
seq.serialize_element(&("start", start))?;
}
if let Some(end) = end {
seq.serialize_element(&("end", end))?;
}
if let Some(accessor) = accessor {
seq.serialize_element(&("accessor", accessor))?;
}
for element in tags {
seq.serialize_element(&("tag", element))?;
}
for element in methods {
seq.serialize_element(&("method", AsRef::<str>::as_ref(element)))?;
}
for element in models {
seq.serialize_element(&("model", element))?;
}
if let Some(multichannel) = multichannel {
seq.serialize_element(&("multichannel", multichannel))?;
}
if let Some(interim_results) = interim_results {
seq.serialize_element(&("interim_results", interim_results))?;
}
if let Some(punctuate) = punctuate {
seq.serialize_element(&("punctuate", punctuate))?;
}
if let Some(ner) = ner {
seq.serialize_element(&("ner", ner))?;
}
if let Some(utterances) = utterances {
seq.serialize_element(&("utterances", utterances))?;
}
if let Some(replace) = replace {
seq.serialize_element(&("replace", replace))?;
}
if let Some(replace) = replace {
seq.serialize_element(&("replace", replace))?;
}
if let Some(profanity_filter) = profanity_filter {
seq.serialize_element(&("profanity_filter", profanity_filter))?;
}
if let Some(keywords) = keywords {
seq.serialize_element(&("keywords", keywords))?;
}
if let Some(diarize) = diarize {
seq.serialize_element(&("diarize", diarize))?;
}
if let Some(search) = search {
seq.serialize_element(&("search", search))?;
}
if let Some(redact) = redact {
seq.serialize_element(&("redact", redact))?;
}
if let Some(alternatives) = alternatives {
seq.serialize_element(&("alternatives", alternatives))?;
}
if let Some(numerals) = numerals {
seq.serialize_element(&("numerals", numerals))?;
}
seq.end()
}
}
impl AsRef<str> for Method {
fn as_ref(&self) -> &str {
use Method::*;
match self {
Sync => "sync",
Async => "async",
Streaming => "streaming",
}
}
}
mod serialize_options_tests {
}