1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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)
}
}