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
use serde::{
    ser::{SerializeMap, SerializeSeq},
    Serialize, Serializer,
};

use crate::*;

pub fn serialize_usize<S>(n: &usize, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_str(&n.to_string())
}

pub fn serialize_description<S>(description: &Description, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    serializer.serialize_str(&description.0.join(" "))
}

pub fn serialize_enum<S>(variants: &[Variant], serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    let mut seq = serializer.serialize_seq(Some(variants.len()))?;

    for variant in variants {
        seq.serialize_element(variant.name)?;
    }

    seq.end()
}

pub fn serialize_redirect<S>(redirect: &Option<Redirect>, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    if let Some(redirect) = redirect {
        serializer.serialize_str(&redirect.to)
    } else {
        serializer.serialize_none()
    }
}

pub fn is_false(v: &bool) -> bool {
    !*v
}

impl Protocol<'_> {
    /// Serialize the `Protocol` data structure as a String of JSON.
    pub fn to_json(&self) -> serde_json::Result<String> {
        serde_json::to_string(self)
    }

    /// Serialize the `Protocol` data structure as a pretty-printed String of JSON.
    pub fn to_json_pretty(&self) -> serde_json::Result<String> {
        serde_json::to_string_pretty(self)
    }
}

impl Serialize for Type<'_> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut map = serializer.serialize_map(None)?;

        match self {
            Type::Integer => {
                map.serialize_entry("type", "integer")?;
            }
            Type::Number => {
                map.serialize_entry("type", "number")?;
            }
            Type::Boolean => {
                map.serialize_entry("type", "boolean")?;
            }
            Type::String => {
                map.serialize_entry("type", "string")?;
            }
            Type::Object => {
                map.serialize_entry("type", "object")?;
            }
            Type::Any => {
                map.serialize_entry("type", "any")?;
            }
            Type::Binary => {
                map.serialize_entry("type", "binary")?;
            }
            Type::Enum(variants) => {
                map.serialize_entry("type", "string")?;
                map.serialize_entry(
                    "enum",
                    &variants
                        .iter()
                        .map(|variant| variant.name)
                        .collect::<Vec<_>>(),
                )?;
            }
            Type::ArrayOf(ty) => {
                map.serialize_entry("type", "array")?;
                map.serialize_entry("items", &ty)?;
            }
            Type::Ref(id) => {
                map.serialize_entry("$ref", &id)?;
            }
        }

        map.end()
    }
}