use std::convert::TryFrom;
use serde_json::value::RawValue;
use crate::{Options, Serialized};
pub use serde::Serialize;
pub trait Sealed {}
pub struct NotYetSerialized<'a, T: Serialize>(pub &'a T);
impl<'a, T: Serialize> From<&'a T> for NotYetSerialized<'a, T> {
fn from(input: &'a T) -> Self {
Self(input)
}
}
pub struct SerializedOnce(Box<RawValue>);
impl<'a, T: Serialize> TryFrom<NotYetSerialized<'a, T>> for SerializedOnce {
type Error = serde_json::Error;
fn try_from(value: NotYetSerialized<'_, T>) -> Result<Self, Self::Error> {
serde_json::to_string(value.0)
.and_then(RawValue::from_string)
.map(Self)
}
}
impl SerializedOnce {
pub fn into_javascript_string_literal(self, options: &Options) -> Serialized {
Serialized::new(&self.0, options)
}
}
impl Serialized {
#[doc(hidden)]
pub unsafe fn from_string_unchecked(string: String) -> Self {
Self(string)
}
}