use crate::Value;
use litl::json;
use unicode_segmentation::UnicodeSegmentation;
#[derive(PartialEq, Eq, Debug, Clone)]
pub enum Input {
Plain(litl::Val),
Value(Value),
CollabText(String),
CollabList(Vec<Input>),
CollabMap(Vec<(String, Input)>),
}
impl Input {
pub fn collab_list<I: IntoIterator<Item = Input>>(iter: I) -> Self {
Input::CollabList(iter.into_iter().collect())
}
pub fn collab_map<I: IntoIterator<Item = (String, Input)>>(iter: I) -> Self {
Input::CollabMap(iter.into_iter().collect())
}
pub fn val_to_litl(&self) -> litl::Val {
match self {
Input::Plain(litl) => litl.clone(),
Input::Value(value) => value.val_to_litl(),
Input::CollabText(string) => json!({
"ranges": [],
"graphemes": string.graphemes(true).collect::<Vec<_>>(),
})
.into(),
Input::CollabList(items) => litl::Val::array(items.iter().map(|item| item.val_to_litl())),
Input::CollabMap(map) => {
litl::Val::object(map.iter().map(|(k, v)| (k.clone(), v.val_to_litl())))
}
}
}
}
impl From<litl::Val> for Input {
fn from(val: litl::Val) -> Self {
Input::Plain(val)
}
}
impl From<&str> for Input {
fn from(string: &str) -> Self {
Input::Plain(litl::Val::str(string))
}
}