use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use typst::foundations::{Bytes, Datetime, Decimal, Dict, Str, Value};
pub trait ToValue {
fn into_value(self) -> Value;
}
pub trait ToDict {
fn into_dict(self) -> Dict;
}
impl ToValue for Value {
fn into_value(self) -> Value {
self
}
}
impl ToValue for Str {
fn into_value(self) -> Value {
Value::Str(self)
}
}
impl ToValue for Bytes {
fn into_value(self) -> Value {
Value::Bytes(self)
}
}
impl ToValue for Datetime {
fn into_value(self) -> Value {
Value::Datetime(self)
}
}
impl ToValue for Decimal {
fn into_value(self) -> Value {
Value::Decimal(self)
}
}
impl ToDict for Dict {
fn into_dict(self) -> Dict {
self
}
}
#[doc(hidden)]
pub fn merge_dict(target: &mut Dict, source: Dict) {
for (key, value) in source {
target.insert(key, value);
}
}
impl ToValue for Dict {
fn into_value(self) -> Value {
Value::Dict(self)
}
}
impl ToValue for () {
fn into_value(self) -> Value {
Value::None
}
}
impl ToValue for bool {
fn into_value(self) -> Value {
Value::Bool(self)
}
}
macro_rules! impl_into_value_int {
($($ty:ty),*) => {$(
impl ToValue for $ty {
fn into_value(self) -> Value {
match i64::try_from(self) {
Ok(int) => Value::Int(int),
Err(_) => Value::Float(self as f64),
}
}
}
)*};
}
impl_into_value_int!(i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, i128, u128);
impl ToValue for f32 {
fn into_value(self) -> Value {
Value::Float(self.into())
}
}
impl ToValue for f64 {
fn into_value(self) -> Value {
Value::Float(self)
}
}
impl ToValue for String {
fn into_value(self) -> Value {
Value::Str(self.into())
}
}
impl ToValue for &str {
fn into_value(self) -> Value {
Value::Str(self.into())
}
}
impl ToValue for Cow<'_, str> {
fn into_value(self) -> Value {
Value::Str(self.into_owned().into())
}
}
impl ToValue for char {
fn into_value(self) -> Value {
Value::Str(self.to_string().into())
}
}
impl<T: ToValue> ToValue for Option<T> {
fn into_value(self) -> Value {
match self {
Some(value) => value.into_value(),
None => Value::None,
}
}
}
impl<T: ToValue> ToValue for Vec<T> {
fn into_value(self) -> Value {
Value::Array(self.into_iter().map(ToValue::into_value).collect())
}
}
impl<T: ToValue, const N: usize> ToValue for [T; N] {
fn into_value(self) -> Value {
Value::Array(self.into_iter().map(ToValue::into_value).collect())
}
}
macro_rules! impl_into_dict_map {
($ty:ident $(, $bound:ident)?) => {
impl<K: Into<Str>, V: ToValue $(, $bound)?> ToDict for $ty<K, V $(, $bound)?> {
fn into_dict(self) -> Dict {
let mut dict = Dict::new();
for (key, value) in self {
dict.insert(key.into(), value.into_value());
}
dict
}
}
impl<K: Into<Str>, V: ToValue $(, $bound)?> ToValue for $ty<K, V $(, $bound)?> {
fn into_value(self) -> Value {
Value::Dict(self.into_dict())
}
}
};
}
impl_into_dict_map!(BTreeMap);
impl_into_dict_map!(HashMap, S);