use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EmbedInput {
Single(String),
Multiple(Vec<String>),
}
impl EmbedInput {
pub fn single(text: impl Into<String>) -> Self {
Self::Single(text.into())
}
pub fn multiple<I, S>(texts: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
Self::Multiple(texts.into_iter().map(|s| s.into()).collect())
}
pub fn len(&self) -> usize {
match self {
Self::Single(_) => 1,
Self::Multiple(v) => v.len(),
}
}
pub fn is_empty(&self) -> bool {
match self {
Self::Single(s) => s.is_empty(),
Self::Multiple(v) => v.is_empty(),
}
}
}
impl From<String> for EmbedInput {
fn from(s: String) -> Self {
Self::Single(s)
}
}
impl From<&str> for EmbedInput {
fn from(s: &str) -> Self {
Self::Single(s.to_string())
}
}
impl From<Vec<String>> for EmbedInput {
fn from(v: Vec<String>) -> Self {
Self::Multiple(v)
}
}
impl<const N: usize> From<[&str; N]> for EmbedInput {
fn from(arr: [&str; N]) -> Self {
Self::Multiple(arr.iter().map(|s| (*s).to_string()).collect())
}
}