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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Ffi-safe equivalents of `serde_json` types.

use std::{
    convert::{TryFrom, TryInto},
    fmt::{self, Debug, Display},
};

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{error::Error as JsonError, value::RawValue};

use crate::std_types::{RStr, RString};

/// An ffi-safe equivalent of `&serde_json::value::RawValue`
///
/// # Example
///
/// This defines a function that serializes a struct,
/// and deserializes the json into another one with `RawValueRef` fields.
///
/// ```
/// use abi_stable::{
///     external_types::RawValueRef,
///     sabi_extern_fn,
///     std_types::{RBoxError, RErr, ROk, RResult, RStr, RString},
/// };
///
/// use serde::{Deserialize, Serialize};
///
/// use std::collections::HashMap;
///
/// const JSON: &'static str = r##"{"hello":"world"}"##;
///
/// let value = RawValueRef::try_from_str(JSON).unwrap();
///
/// assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
///
/// #[derive(Serialize)]
/// pub struct Pair {
///     pub first: Vec<u32>,
///     pub second: HashMap<RString, RString>,
/// }
///
/// #[derive(Debug, Deserialize)]
/// pub struct PairDeserialize<'a> {
///     #[serde(borrow)]
///     pub first: RawValueRef<'a>,
///
///     #[serde(borrow)]
///     pub second: RawValueRef<'a>,
/// }
///
/// #[sabi_extern_fn]
/// fn deserialize_data_structure<'de>(
///     input: RStr<'de>,
/// ) -> RResult<PairDeserialize<'de>, RBoxError> {
///     match serde_json::from_str::<PairDeserialize>(input.into()) {
///         Ok(x) => ROk(x),
///         Err(x) => RErr(RBoxError::new(x)),
///     }
/// }
///
/// # fn main(){
///
/// let json = serde_json::to_string(&Pair {
///     first: vec![0, 1, 2],
///     second: vec![(RString::from("hello"), "world".into())]
///         .into_iter()
///         .collect(),
/// })
/// .unwrap();
///
/// let pair = deserialize_data_structure(json.as_str().into()).unwrap();
///
/// assert_eq!(pair.first.get(), "[0,1,2]");
/// assert_eq!(pair.second.get(), r##"{"hello":"world"}"##);
///
/// # }
///
/// ```
#[repr(transparent)]
#[derive(StableAbi, Copy, Clone)]
pub struct RawValueRef<'a> {
    ref_: RStr<'a>,
}

impl<'a> RawValueRef<'a> {
    /// Converts a `&str` to a `RawValueRef<'a>` without checking whether it is valid JSON.
    ///
    /// # Safety
    ///
    /// `input` must be valid JSON and contain no leading or trailing whitespace.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::external_types::RawValueRef;
    ///
    /// const JSON: &'static str = r##"{"huh":"that is interesting"}"##;
    ///
    /// const VALUE: RawValueRef<'_> = unsafe { RawValueRef::from_str_unchecked(JSON) };
    ///
    /// assert_eq!(serde_json::to_string(&VALUE).unwrap().as_str(), JSON);
    ///
    /// ```
    pub const unsafe fn from_str_unchecked(input: &'a str) -> RawValueRef<'a> {
        Self {
            ref_: RStr::from_str(input),
        }
    }

    /// Converts a `RStr<'a>` to a `RawValueRef<'a>` without checking whether it is valid JSON.
    ///
    /// # Safety
    ///
    /// `input` must be valid JSON and contain no leading or trailing whitespace.
    ///
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::{external_types::RawValueRef, std_types::RStr};
    ///
    /// const JSON: &'static str = r##"{"huh":"that is interesting"}"##;
    ///
    /// let json_rstr = RStr::from(JSON);
    /// let value = unsafe { RawValueRef::from_rstr_unchecked(json_rstr) };
    ///
    /// assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
    /// ```
    ///
    ///
    pub const unsafe fn from_rstr_unchecked(input: RStr<'a>) -> RawValueRef<'a> {
        Self { ref_: input }
    }

    /// Attempts to convert a `&'a str` into a `RawValueRef<'a>`.
    ///
    /// Fails in the same cases as parsing a `&'a RawValue` from a string does.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::{external_types::RawValueRef, std_types::RStr};
    ///
    /// const JSON: &'static str = r##"{"nope":"oof"}"##;
    ///
    /// let raw = RawValueRef::try_from_str(JSON).unwrap();
    ///
    /// assert_eq!(raw.get(), JSON);
    ///
    /// ```
    #[inline]
    pub fn try_from_str(input: &'a str) -> Result<Self, JsonError> {
        input.try_into()
    }

    /// Gets the json being serialized,as a `&str`.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::external_types::RawValueRef;
    ///
    /// const JSON: &'static str = r##"{"huh":1007}"##;
    ///
    /// let raw = serde_json::from_str::<RawValueRef<'static>>(JSON).unwrap();
    ///
    /// assert_eq!(raw.get(), JSON);
    ///
    /// ```
    #[inline]
    pub fn get(&self) -> &'a str {
        self.ref_.as_str()
    }

    /// Gets the json being serialized,as a `RStr<'a>`.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::{external_types::RawValueRef, std_types::RStr};
    ///
    /// const JSON: &'static str = r##"{"bugs":"life"}"##;
    ///
    /// let raw = serde_json::from_str::<RawValueRef<'static>>(JSON).unwrap();
    ///
    /// assert_eq!(raw.get_rstr(), RStr::from(JSON));
    ///
    /// ```
    #[inline]
    pub const fn get_rstr(&self) -> RStr<'a> {
        self.ref_
    }
}

impl<'a> Debug for RawValueRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Debug::fmt(&self.ref_, f)
    }
}

impl<'a> Display for RawValueRef<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.ref_, f)
    }
}

impl<'a> From<&'a RawValue> for RawValueRef<'a> {
    fn from(v: &'a RawValue) -> Self {
        Self {
            ref_: v.get().into(),
        }
    }
}

impl<'a> TryFrom<&'a str> for RawValueRef<'a> {
    type Error = JsonError;
    fn try_from(v: &'a str) -> Result<Self, JsonError> {
        serde_json::from_str::<&'a RawValue>(v).map(Self::from)
    }
}

impl<'a> Serialize for RawValueRef<'a> {
    fn serialize<Z>(&self, serializer: Z) -> Result<Z::Ok, Z::Error>
    where
        Z: Serializer,
    {
        unsafe { into_ref_rawvalue(self.ref_.as_str()).serialize(serializer) }
    }
}

impl<'de: 'a, 'a> Deserialize<'de> for RawValueRef<'a> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        <&'a RawValue>::deserialize(deserializer).map(Self::from)
    }
}

///////////////////////////////////////////////////////////////////////////////

/// An ffi-safe equivalent of `Box<serde_json::value::RawValue>`
///
///
/// # Example
///
/// This defines a function that serializes a struct,
/// and deserializes the json into another one with `RawValueBox` fields.
///
/// ```
/// use abi_stable::{
///     external_types::RawValueBox,
///     sabi_extern_fn,
///     std_types::{RBoxError, RErr, ROk, RResult, RStr, RString},
/// };
///
/// use serde::{Deserialize, Serialize};
///
/// const JSON: &'static str = r##"{"hello":"world"}"##;
///
/// let value = RawValueBox::try_from_string(JSON.to_string()).unwrap();
///
/// assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
///
/// #[derive(Serialize)]
/// pub struct Pair {
///     pub first: u64,
///     pub second: RString,
/// }
///
/// #[derive(Debug, Deserialize)]
/// pub struct PairDeserialize {
///     pub first: RawValueBox,
///     pub second: RawValueBox,
/// }
///
/// #[sabi_extern_fn]
/// fn deserialize_data_structure(
///     input: RStr<'_>,
/// ) -> RResult<PairDeserialize, RBoxError> {
///     match serde_json::from_str::<PairDeserialize>(input.into()) {
///         Ok(x) => ROk(x),
///         Err(x) => RErr(RBoxError::new(x)),
///     }
/// }
///
/// # fn main(){
///
/// let json = serde_json::to_string(&Pair {
///     first: 99,
///     second: "How many apples?".into(),
/// })
/// .unwrap();
///
/// let pair = deserialize_data_structure(json.as_str().into()).unwrap();
///
/// assert_eq!(pair.first.get(), "99");
/// assert_eq!(pair.second.get(), r##""How many apples?"}"##);
///
/// # }
///
///
/// ```
///
#[repr(transparent)]
#[derive(StableAbi, Clone)]
pub struct RawValueBox {
    string: RString,
}

impl RawValueBox {
    /// Converts a `String` to an `RawValueBox` without checking whether it is valid JSON.
    ///
    /// # Safety
    ///
    /// `input` must be valid JSON and contain no leading or trailing whitespace.
    ///
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::external_types::RawValueBox;
    ///
    /// const JSON: &'static str = r##"{"huh":"that is interesting"}"##;
    ///
    /// let value = unsafe { RawValueBox::from_string_unchecked(JSON.to_string()) };
    ///
    /// assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
    /// ```
    ///
    #[inline]
    pub unsafe fn from_string_unchecked(input: String) -> RawValueBox {
        Self {
            string: input.into(),
        }
    }

    /// Converts an `RString` to an `RawValueBox` without checking whether it is valid JSON.
    ///
    /// # Safety
    ///
    /// `input` must be valid JSON and contain no leading or trailing whitespace.
    ///
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::{external_types::RawValueBox, std_types::RString};
    ///
    /// const JSON: &'static str = r##"{"huh":"that is interesting"}"##;
    ///
    /// let json_rstring = RString::from(JSON);
    /// let value = unsafe { RawValueBox::from_rstring_unchecked(json_rstring) };
    ///
    /// assert_eq!(serde_json::to_string(&value).unwrap().as_str(), JSON);
    /// ```
    ///
    #[inline]
    pub const unsafe fn from_rstring_unchecked(input: RString) -> RawValueBox {
        Self { string: input }
    }

    /// Attempts to convert a `String` into a `RawValueBox`.
    ///
    /// Fails in the same cases as converting a String into a `Box<RawValue>` does.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::{external_types::RawValueBox, std_types::RString};
    ///
    /// const JSON: &'static str = r##"{"nope":"oof"}"##;
    ///
    /// let raw = RawValueBox::try_from_string(JSON.to_string()).unwrap();
    ///
    /// assert_eq!(raw.get(), JSON);
    ///
    /// ```
    #[inline]
    pub fn try_from_string(input: String) -> Result<Self, JsonError> {
        input.try_into()
    }

    /// Gets the json being serialized,as a `&str`.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::external_types::RawValueBox;
    ///
    /// const JSON: &'static str = r##"{"huh":1007}"##;
    ///
    /// let raw = serde_json::from_str::<RawValueBox>(JSON).unwrap();
    ///
    /// assert_eq!(raw.get(), JSON);
    ///
    /// ```
    #[inline]
    pub fn get(&self) -> &str {
        self.string.as_str()
    }

    /// Gets the json being serialized,as a `RStr<'a>`.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::{external_types::RawValueBox, std_types::RStr};
    ///
    /// const JSON: &'static str = r##"{"bugs":"life"}"##;
    ///
    /// let raw = serde_json::from_str::<RawValueBox>(JSON).unwrap();
    ///
    /// assert_eq!(raw.get_rstr(), RStr::from(JSON));
    ///
    /// ```
    #[inline]
    pub fn get_rstr(&self) -> RStr<'_> {
        self.get().into()
    }

    /// Gets a RawValueRef<'_> borrowing from this RawValueBox.
    ///
    /// # Example
    ///
    /// ```
    /// use abi_stable::external_types::{RawValueBox, RawValueRef};
    ///
    /// const JSON: &'static str = r##"{"bugs":"life"}"##;
    ///
    /// let raw = serde_json::from_str::<RawValueBox>(JSON).unwrap();
    ///
    /// assert_eq!(raw.get(), RawValueRef::try_from_str(JSON).unwrap().get());
    ///
    /// ```
    #[inline]
    pub fn as_raw_value_ref(&self) -> RawValueRef<'_> {
        unsafe { RawValueRef::from_str_unchecked(self.get()) }
    }
}

impl Debug for RawValueBox {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Debug::fmt(&self.string, f)
    }
}

impl Display for RawValueBox {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.string, f)
    }
}

impl From<Box<RawValue>> for RawValueBox {
    fn from(v: Box<RawValue>) -> Self {
        let string: String = from_boxed_rawvalue(v).into();
        Self {
            string: string.into(),
        }
    }
}

impl TryFrom<String> for RawValueBox {
    type Error = JsonError;
    fn try_from(v: String) -> Result<Self, JsonError> {
        RawValue::from_string(v).map(Self::from)
    }
}

impl Serialize for RawValueBox {
    fn serialize<Z>(&self, serializer: Z) -> Result<Z::Ok, Z::Error>
    where
        Z: Serializer,
    {
        unsafe { into_ref_rawvalue(&self.string).serialize(serializer) }
    }
}

impl<'de> Deserialize<'de> for RawValueBox {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        <Box<RawValue>>::deserialize(deserializer).map(Self::from)
    }
}

///////////////////////////////////////////////////////////////////////////////

fn from_boxed_rawvalue(x: Box<RawValue>) -> Box<str> {
    // This would become Undefined Behavior if
    // serde_json somehow changes RawValue to not be a transparent wrapper around `str`
    unsafe { Box::from_raw(Box::into_raw(x) as *mut str) }
}

const unsafe fn into_ref_rawvalue(x: &str) -> &RawValue {
    // This would become Undefined Behavior if
    // serde_json somehow changes RawValue to not be a transparent wrapper around `str`
    unsafe { &*(x as *const str as *const RawValue) }
}