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
use azure_sdk_core::errors::AzureError;
use serde::ser::Serialize;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct ToJsonVector {
    last_serialized_string: Option<String>,
    serialized_string: String,
}

impl ToJsonVector {
    pub fn new() -> Self {
        Self {
            last_serialized_string: None,
            serialized_string: String::from("["),
        }
    }

    // this hack builds the json by hands, concatenating the single json
    // serialization of the items. Ideally we should collect the &dyn Serialize
    // trait objects and serialize in one shot at the end but unfortunately
    // Serialize cannot be made a trait object so no dynamic dispatch :(
    pub fn push<T>(&mut self, t: T) -> Result<&mut Self, AzureError>
    where
        T: Serialize,
    {
        if self.serialized_string.len() > 1 {
            self.serialized_string.push_str(", ");
        }

        let serialized_string = serde_json::to_string(&t)?;
        self.serialized_string.push_str(&serialized_string);
        self.last_serialized_string = Some(serialized_string);

        Ok(self)
    }

    pub(crate) fn to_json(&self) -> String {
        format!("{}]", self.serialized_string)
    }

    #[allow(dead_code)]
    pub(crate) fn last_serialized_string(&self) -> Option<&str> {
        match self.last_serialized_string.as_ref() {
            Some(last_serialized_str) => Some(last_serialized_str),
            None => None,
        }
    }
}

impl std::default::Default for ToJsonVector {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> std::convert::TryFrom<&[T]> for ToJsonVector
where
    T: Serialize,
{
    type Error = AzureError;

    fn try_from(slice: &[T]) -> Result<Self, Self::Error> {
        let mut to_json_vector = ToJsonVector::new();
        for item in slice {
            to_json_vector.push(item)?;
        }
        Ok(to_json_vector)
    }
}

impl<T> std::convert::TryFrom<&Vec<T>> for ToJsonVector
where
    T: Serialize,
{
    type Error = AzureError;

    fn try_from(v: &Vec<T>) -> Result<Self, Self::Error> {
        let mut to_json_vector = ToJsonVector::new();
        for item in v {
            to_json_vector.push(item)?;
        }
        Ok(to_json_vector)
    }
}

impl std::convert::From<&str> for ToJsonVector {
    fn from(t: &str) -> Self {
        let mut pk = Self::new();
        let _ = pk.push(t).unwrap();
        pk
    }
}

impl std::convert::From<&String> for ToJsonVector {
    fn from(t: &String) -> Self {
        let mut pk = Self::new();
        let _ = pk.push(t).unwrap();
        pk
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::convert::TryInto;

    #[test]
    fn serialize() {
        let owned = "owned".to_owned();

        let serialized = ToJsonVector::new()
            .push("aaa")
            .unwrap()
            .push(&owned)
            .unwrap()
            .push(100u64)
            .unwrap()
            .to_json();
        assert_eq!(serialized, "[\"aaa\", \"owned\", 100]");

        let mut vector = vec!["pollo", "arrosto"];
        let to_json_vector: ToJsonVector = (&vector).try_into().unwrap();
        assert_eq!(to_json_vector.to_json(), "[\"pollo\", \"arrosto\"]");

        vector.push("limone");
        let slice: &[&str] = &vector;
        let to_json_vector: ToJsonVector = slice.try_into().unwrap();
        assert_eq!(
            to_json_vector.to_json(),
            "[\"pollo\", \"arrosto\", \"limone\"]"
        );
    }
}