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
pub mod contact;
pub mod domain;
pub mod host;
pub mod message;
use serde::{ser::SerializeStruct, ser::Serializer, Deserialize, Serialize};
use std::error::Error;
use std::time::SystemTime;
use crate::epp::object::{
ElementName, EmptyTag, EppObject, Extension, Options, ServiceExtension, Services, StringValue,
StringValueTrait,
};
use crate::epp::xml::{EPP_CONTACT_XMLNS, EPP_DOMAIN_XMLNS, EPP_HOST_XMLNS, EPP_LANG, EPP_VERSION};
use epp_client_macros::*;
pub type Command<T> = CommandWithExtension<T, EmptyTag>;
pub type EppHello = EppObject<Hello>;
pub type EppLogin = EppObject<Command<Login>>;
pub type EppLogout = EppObject<Command<Logout>>;
#[derive(Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "command")]
pub struct CommandWithExtension<T: ElementName, E: ElementName> {
pub command: T,
pub extension: Option<Extension<E>>,
#[serde(rename = "clTRID")]
pub client_tr_id: StringValue,
}
impl<T: ElementName + Serialize, E: ElementName + Serialize> Serialize
for CommandWithExtension<T, E>
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let command_name = self.command.element_name();
let mut state = serializer.serialize_struct("command", 3)?;
state.serialize_field(command_name, &self.command)?;
state.serialize_field("extension", &self.extension)?;
state.serialize_field("clTRID", &self.client_tr_id)?;
state.end()
}
}
impl<T: ElementName> Command<T> {
pub fn new(command: T, client_tr_id: &str) -> Command<T> {
Command {
command: command,
extension: None,
client_tr_id: client_tr_id.to_string_value(),
}
}
}
impl<T: ElementName, E: ElementName> CommandWithExtension<T, E> {
pub fn build(command: T, ext: E, client_tr_id: &str) -> CommandWithExtension<T, E> {
CommandWithExtension {
command: command,
extension: Some(Extension { data: ext }),
client_tr_id: client_tr_id.to_string_value(),
}
}
}
pub fn generate_client_tr_id(username: &str) -> Result<String, Box<dyn Error>> {
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
Ok(format!("{}:{}", username, timestamp.as_secs()))
}
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "hello")]
pub struct Hello;
impl EppHello {
pub fn new() -> EppHello {
EppObject::build(Hello {})
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "login")]
pub struct Login {
#[serde(rename(serialize = "clID", deserialize = "clID"))]
username: StringValue,
#[serde(rename = "pw", default)]
password: StringValue,
options: Options,
#[serde(rename = "svcs")]
services: Services,
}
impl EppLogin {
pub fn new(
username: &str,
password: &str,
ext_uris: &Option<Vec<String>>,
client_tr_id: &str,
) -> EppLogin {
let ext_uris = match ext_uris {
Some(uris) => Some(
uris.iter()
.map(|u| u.to_string_value())
.collect::<Vec<StringValue>>(),
),
None => None,
};
let login = Login {
username: username.to_string_value(),
password: password.to_string_value(),
options: Options {
version: EPP_VERSION.to_string_value(),
lang: EPP_LANG.to_string_value(),
},
services: Services {
obj_uris: vec![
EPP_HOST_XMLNS.to_string_value(),
EPP_CONTACT_XMLNS.to_string_value(),
EPP_DOMAIN_XMLNS.to_string_value(),
],
svc_ext: Some(ServiceExtension { ext_uris: ext_uris }),
},
};
EppObject::build(Command::<Login> {
command: login,
extension: None,
client_tr_id: client_tr_id.to_string_value(),
})
}
pub fn options(&mut self, options: Options) {
self.data.command.options = options;
}
pub fn services(&mut self, services: Services) {
self.data.command.services = services;
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, ElementName)]
#[element_name(name = "logout")]
pub struct Logout;
impl EppLogout {
pub fn new(client_tr_id: &str) -> EppLogout {
EppObject::build(Command::<Logout> {
command: Logout,
extension: None,
client_tr_id: client_tr_id.to_string_value(),
})
}
}