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
use std;
use std::collections::HashMap;
use base64;
use serde::de::Unexpected;
use xml::escape::escape_str_pcdata;

#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    Int(i32),
    Bool(bool),
    String(String),
    Double(f64),
    DateTime(String),
    Base64(Vec<u8>),
    Array(Vec<Value>),
    Struct(HashMap<String, Value>),
}

impl Value {
    pub fn unexpected(&self) -> Unexpected {
        match *self {
            Value::Int(v) => Unexpected::Signed(v as i64),
            Value::Bool(v) => Unexpected::Bool(v),
            Value::String(ref v) => Unexpected::Str(v),
            Value::Double(v) => Unexpected::Float(v),
            Value::DateTime(_) => Unexpected::Other("dateTime.iso8601"),
            Value::Base64(ref v) => Unexpected::Bytes(v),
            Value::Array(_) => Unexpected::Seq,
            Value::Struct(_) => Unexpected::Map,
        }
    }
}

pub type Params = Vec<Value>;

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct Fault {
    #[serde(rename = "faultCode")]
    pub code: i32,
    #[serde(rename = "faultString")]
    pub message: String,
}

impl Fault {
    pub fn new<T>(code: i32, message: T) -> Fault
    where
        T: Into<String>,
    {
        Fault {
            code,
            message: message.into(),
        }
    }
}

pub type Response = std::result::Result<Params, Fault>;

#[derive(Clone, Debug, PartialEq)]
pub struct Call {
    pub name: String,
    pub params: Params,
}

pub trait ToXml {
    fn to_xml(&self) -> String;
}

impl ToXml for Call {
    fn to_xml(&self) -> String {
        format!(
            include_str!("templates/call.xml"),
            name = self.name,
            params = self.params
                .iter()
                .map(|param| format!("<param>{}</param>", param.to_xml()))
                .collect::<String>()
        )
    }
}

impl ToXml for Response {
    fn to_xml(&self) -> String {
        match *self {
            Ok(ref params) => {
                format!(
                    include_str!("templates/response_success.xml"),
                    params = params
                        .iter()
                        .map(|param| format!("<param>{}</param>", param.to_xml()))
                        .collect::<String>()
                )
            }
            Err(Fault { code, ref message }) => {
                format!(
                    include_str!("templates/response_fault.xml"),
                    code = code,
                    message = message
                )
            }
        }
    }
}

impl ToXml for Value {
    fn to_xml(&self) -> String {
        match *self {
            Value::Int(v) => format!("<value><i4>{}</i4></value>", v),
            Value::Bool(v) => {
                format!(
                    "<value><boolean>{}</boolean></value>",
                    if v { 1 } else { 0 }
                )
            }
            Value::String(ref v) => {
                format!("<value><string>{}</string></value>", escape_str_pcdata(v))
            }
            Value::Double(v) => format!("<value><double>{}</double></value>", v),
            Value::DateTime(ref v) => {
                format!("<value><dateTime.iso8601>{}</dateTime.iso8601></value>", v)
            }
            Value::Base64(ref v) => {
                format!("<value><base64>{}</base64></value>", base64::encode(v))
            }
            Value::Array(ref v) => {
                format!(
                    "<value><array><data>{}</data></array></value>",
                    v.iter().map(Value::to_xml).collect::<String>()
                )
            }
            Value::Struct(ref v) => {
                format!(
                    "<value><struct>{}</struct></value>",
                    v.iter()
                        .map(|(key, value)| {
                            format!("<member><name>{}</name>{}</member>", key, value.to_xml())
                        })
                        .collect::<String>()
                )
            }
        }

    }
}