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
//! An copy of https://github.com/ralpha/serde_deserializer_best_effort

use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;

/// A trait that is used as an alternative to interleaved tag detection
/// see: https://github.com/serde-rs/serde/issues/1725
pub trait DeserializeBestEffort<'de>: Deserialize<'de> {}

/// A trait that is used as an alternative to interleaved tag detection
/// see: https://github.com/serde-rs/serde/issues/1725
pub trait DeserializeBestEffortTypes<'de, T>
where
    T: DeserializeBestEffortTypes<'de, T>,
{
    fn add_data(&mut self, key: &str, next_value: T);
}

impl<'de> DeserializeBestEffortTypes<'de, i32> for i32 {
    fn add_data(&mut self, _key: &str, next_value: i32) {
        *self = next_value;
    }
}

impl<'de> DeserializeBestEffortTypes<'de, u32> for u32 {
    fn add_data(&mut self, _key: &str, next_value: u32) {
        *self = next_value;
    }
}

impl<'de> DeserializeBestEffortTypes<'de, String> for String {
    fn add_data(&mut self, _key: &str, next_value: String) {
        *self = next_value;
    }
}

impl<'de> DeserializeBestEffortTypes<'de, Value> for Value {
    fn add_data(&mut self, _key: &str, next_value: Value) {
        *self = next_value;
    }
}

impl<'de, S: ::std::hash::BuildHasher> DeserializeBestEffortTypes<'de, Value>
    for HashMap<String, Value, S>
{
    fn add_data(&mut self, key: &str, next_value: Value) {
        self.insert(key.to_string(), next_value);
    }
}

impl<'de, T> DeserializeBestEffortTypes<'de, T> for T
where
    T: DeserializeBestEffort<'de> + Default,
{
    fn add_data(&mut self, _key: &str, next_value: T) {
        *self = next_value;
    }
}

impl<'de, T> DeserializeBestEffortTypes<'de, T> for Vec<T>
where
    T: DeserializeBestEffortTypes<'de, T> + Default,
{
    fn add_data(&mut self, _key: &str, next_value: T) {
        self.push(next_value);
    }
}

impl<'de, T> DeserializeBestEffortTypes<'de, T> for Option<T>
where
    T: DeserializeBestEffortTypes<'de, T> + Default,
{
    fn add_data(&mut self, _key: &str, next_value: T) {
        *self = Some(next_value);
    }
}

impl<'de> DeserializeBestEffortTypes<'de, String> for Option<()> {
    fn add_data(&mut self, _key: &str, _next_value: String) {
        *self = Some(());
    }
}

impl<'de, T> DeserializeBestEffortTypes<'de, T> for Option<Vec<T>>
where
    T: DeserializeBestEffortTypes<'de, T> + Default,
{
    fn add_data(&mut self, _key: &str, next_value: T) {
        if self.is_none() {
            *self = Some(Vec::new());
        }
        if let Some(list) = self {
            list.push(next_value);
        }
    }
}

impl<'de> DeserializeBestEffortTypes<'de, String> for Option<Vec<Option<()>>> {
    fn add_data(&mut self, _key: &str, _next_value: String) {
        if self.is_none() {
            *self = Some(Vec::new());
        }
        if let Some(list) = self {
            list.push(Some(()));
        }
    }
}