Replace Vec with slice where possible

This commit is contained in:
Dirkjan Ochtman 2021-12-14 11:26:54 +01:00 committed by masalachai
parent d4f4949cd8
commit 99701608a2
8 changed files with 70 additions and 66 deletions

View File

@ -134,7 +134,12 @@ pub struct PostalInfo<'a> {
impl<'a> PostalInfo<'a> { impl<'a> PostalInfo<'a> {
/// Creates a new PostalInfo instance /// Creates a new PostalInfo instance
pub fn new(info_type: &str, name: &'a str, organization: &'a str, address: Address<'a>) -> Self { pub fn new(
info_type: &str,
name: &'a str,
organization: &'a str,
address: Address<'a>,
) -> Self {
Self { Self {
info_type: info_type.to_string(), info_type: info_type.to_string(),
name: name.into(), name: name.into(),

View File

@ -30,13 +30,11 @@ pub struct ContactCheck<'a> {
} }
impl<'a> ContactCheck<'a> { impl<'a> ContactCheck<'a> {
pub fn new(contact_ids: &[&'a str]) -> Self { pub fn new(contact_ids: &'a [&'a str]) -> Self {
let contact_ids = contact_ids.iter().map(|&d| d.into()).collect();
Self { Self {
list: ContactList { list: ContactList {
xmlns: XMLNS, xmlns: XMLNS,
contact_ids, contact_ids: contact_ids.iter().map(|&id| id.into()).collect(),
}, },
} }
} }

View File

@ -50,13 +50,13 @@ impl<'a> ContactUpdate<'a> {
} }
/// Sets the data for the &lt;add&gt; tag for the contact update request /// Sets the data for the &lt;add&gt; tag for the contact update request
pub fn add(&mut self, statuses: Vec<ObjectStatus>) { pub fn add(&mut self, status: &'a [ObjectStatus]) {
self.contact.add_statuses = Some(StatusList { status: statuses }); self.contact.add_statuses = Some(StatusList { status });
} }
/// Sets the data for the &lt;rem&gt; tag for the contact update request /// Sets the data for the &lt;rem&gt; tag for the contact update request
pub fn remove(&mut self, statuses: Vec<ObjectStatus>) { pub fn remove(&mut self, status: &'a [ObjectStatus]) {
self.contact.remove_statuses = Some(StatusList { status: statuses }); self.contact.remove_statuses = Some(StatusList { status });
} }
} }
@ -77,9 +77,9 @@ pub struct ContactChangeInfo<'a> {
/// Type for list of elements of the &lt;status&gt; tag for contact update request /// Type for list of elements of the &lt;status&gt; tag for contact update request
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct StatusList { pub struct StatusList<'a> {
#[serde(rename = "contact:status")] #[serde(rename = "contact:status")]
status: Vec<ObjectStatus>, status: &'a [ObjectStatus],
} }
/// Type for elements under the contact &lt;update&gt; tag /// Type for elements under the contact &lt;update&gt; tag
@ -90,9 +90,9 @@ pub struct ContactUpdateRequestData<'a> {
#[serde(rename = "contact:id")] #[serde(rename = "contact:id")]
id: StringValue<'a>, id: StringValue<'a>,
#[serde(rename = "contact:add")] #[serde(rename = "contact:add")]
add_statuses: Option<StatusList>, add_statuses: Option<StatusList<'a>>,
#[serde(rename = "contact:rem")] #[serde(rename = "contact:rem")]
remove_statuses: Option<StatusList>, remove_statuses: Option<StatusList<'a>>,
#[serde(rename = "contact:chg")] #[serde(rename = "contact:chg")]
change_info: Option<ContactChangeInfo<'a>>, change_info: Option<ContactChangeInfo<'a>>,
} }
@ -125,11 +125,11 @@ mod tests {
let voice = Phone::new("+33.47237942"); let voice = Phone::new("+33.47237942");
object.set_info("newemail@eppdev.net", postal_info, voice, "eppdev-387323"); object.set_info("newemail@eppdev.net", postal_info, voice, "eppdev-387323");
let add_statuses = vec![ObjectStatus { let add_statuses = &[ObjectStatus {
status: "clientTransferProhibited".to_string(), status: "clientTransferProhibited".to_string(),
}]; }];
object.add(add_statuses); object.add(add_statuses);
let remove_statuses = vec![ObjectStatus { let remove_statuses = &[ObjectStatus {
status: "clientDeleteProhibited".to_string(), status: "clientDeleteProhibited".to_string(),
}]; }];
object.remove(remove_statuses); object.remove(remove_statuses);

View File

@ -28,7 +28,7 @@ pub struct HostAttr<'a> {
pub struct HostAttrList<'a> { pub struct HostAttrList<'a> {
/// The list of &lt;hostAttr&gt; tags /// The list of &lt;hostAttr&gt; tags
#[serde(rename = "domain:hostAttr", alias = "hostAttr")] #[serde(rename = "domain:hostAttr", alias = "hostAttr")]
pub hosts: Vec<HostAttr<'a>>, pub hosts: &'a [HostAttr<'a>],
} }
/// The list of &lt;hostObj&gt; types for domain transactions. Typically under an &lt;ns&gt; tag /// The list of &lt;hostObj&gt; types for domain transactions. Typically under an &lt;ns&gt; tag
@ -36,7 +36,7 @@ pub struct HostAttrList<'a> {
pub struct HostObjList<'a> { pub struct HostObjList<'a> {
/// The list of &lt;hostObj&gt; tags /// The list of &lt;hostObj&gt; tags
#[serde(rename = "domain:hostObj", alias = "hostObj")] #[serde(rename = "domain:hostObj", alias = "hostObj")]
pub hosts: Vec<StringValue<'a>>, pub hosts: &'a [StringValue<'a>],
} }
/// Enum that can accept one type which corresponds to either the &lt;hostObj&gt; or &lt;hostAttr&gt; /// Enum that can accept one type which corresponds to either the &lt;hostObj&gt; or &lt;hostAttr&gt;

View File

@ -36,7 +36,7 @@ pub struct DomainCreateRequestData<'a> {
pub registrant: Option<StringValue<'a>>, pub registrant: Option<StringValue<'a>>,
/// The list of contacts for the domain /// The list of contacts for the domain
#[serde(rename = "domain:contact")] #[serde(rename = "domain:contact")]
pub contacts: Option<Vec<DomainContact>>, pub contacts: Option<&'a [DomainContact]>,
/// The auth info for the domain /// The auth info for the domain
#[serde(rename = "domain:authInfo")] #[serde(rename = "domain:authInfo")]
pub auth_info: DomainAuthInfo<'a>, pub auth_info: DomainAuthInfo<'a>,
@ -59,7 +59,7 @@ impl<'a> DomainCreate<'a> {
ns: Option<HostList<'a>>, ns: Option<HostList<'a>>,
registrant_id: Option<&'a str>, registrant_id: Option<&'a str>,
auth_password: &'a str, auth_password: &'a str,
contacts: Option<Vec<DomainContact>>, contacts: Option<&'a [DomainContact]>,
) -> Self { ) -> Self {
Self { Self {
domain: DomainCreateRequestData { domain: DomainCreateRequestData {
@ -113,7 +113,7 @@ mod tests {
fn command() { fn command() {
let xml = get_xml("request/domain/create.xml").unwrap(); let xml = get_xml("request/domain/create.xml").unwrap();
let contacts = vec![ let contacts = &[
DomainContact { DomainContact {
contact_type: "admin".to_string(), contact_type: "admin".to_string(),
id: "eppdev-contact-3".to_string(), id: "eppdev-contact-3".to_string(),
@ -148,7 +148,7 @@ mod tests {
fn command_with_host_obj() { fn command_with_host_obj() {
let xml = get_xml("request/domain/create_with_host_obj.xml").unwrap(); let xml = get_xml("request/domain/create_with_host_obj.xml").unwrap();
let contacts = vec![ let contacts = &[
DomainContact { DomainContact {
contact_type: "admin".to_string(), contact_type: "admin".to_string(),
id: "eppdev-contact-3".to_string(), id: "eppdev-contact-3".to_string(),
@ -163,14 +163,11 @@ mod tests {
}, },
]; ];
let ns = Some(HostList::HostObjList(HostObjList { let hosts = &["ns1.test.com".into(), "ns2.test.com".into()];
hosts: vec!["ns1.test.com".into(), "ns2.test.com".into()],
}));
let object = DomainCreate::new( let object = DomainCreate::new(
"eppdev-1.com", "eppdev-1.com",
1, 1,
ns, Some(HostList::HostObjList(HostObjList { hosts })),
Some("eppdev-contact-3"), Some("eppdev-contact-3"),
"epP4uthd#v", "epP4uthd#v",
Some(contacts), Some(contacts),
@ -187,7 +184,7 @@ mod tests {
fn command_with_host_attr() { fn command_with_host_attr() {
let xml = get_xml("request/domain/create_with_host_attr.xml").unwrap(); let xml = get_xml("request/domain/create_with_host_attr.xml").unwrap();
let contacts = vec![ let contacts = &[
DomainContact { DomainContact {
contact_type: "admin".to_string(), contact_type: "admin".to_string(),
id: "eppdev-contact-3".to_string(), id: "eppdev-contact-3".to_string(),
@ -202,8 +199,7 @@ mod tests {
}, },
]; ];
let host_attr = HostList::HostAttrList(HostAttrList { let hosts = &[
hosts: vec![
HostAttr { HostAttr {
name: "ns1.eppdev-1.com".into(), name: "ns1.eppdev-1.com".into(),
addresses: None, addresses: None,
@ -215,13 +211,12 @@ mod tests {
HostAddr::new_v6("2404:6800:4001:801::200e"), HostAddr::new_v6("2404:6800:4001:801::200e"),
]), ]),
}, },
], ];
});
let object = DomainCreate::new( let object = DomainCreate::new(
"eppdev-2.com", "eppdev-2.com",
1, 1,
Some(host_attr), Some(HostList::HostAttrList(HostAttrList { hosts })),
Some("eppdev-contact-3"), Some("eppdev-contact-3"),
"epP4uthd#v", "epP4uthd#v",
Some(contacts), Some(contacts),

View File

@ -64,10 +64,10 @@ pub struct DomainAddRemove<'a> {
pub ns: Option<HostList<'a>>, pub ns: Option<HostList<'a>>,
/// The list of contacts to add to or remove from the domain /// The list of contacts to add to or remove from the domain
#[serde(rename = "domain:contact")] #[serde(rename = "domain:contact")]
pub contacts: Option<Vec<DomainContact>>, pub contacts: Option<&'a [DomainContact]>,
/// The list of statuses to add to or remove from the domain /// The list of statuses to add to or remove from the domain
#[serde(rename = "domain:status")] #[serde(rename = "domain:status")]
pub statuses: Option<Vec<ObjectStatus>>, pub statuses: Option<&'a [ObjectStatus]>,
} }
/// Type for elements under the &lt;update&gt; tag for domain update /// Type for elements under the &lt;update&gt; tag for domain update
@ -112,20 +112,24 @@ mod tests {
let mut object = DomainUpdate::new("eppdev.com"); let mut object = DomainUpdate::new("eppdev.com");
let statuses = &[ObjectStatus {
status: "clientDeleteProhibited".to_string(),
}];
let add = DomainAddRemove { let add = DomainAddRemove {
ns: None, ns: None,
contacts: None, contacts: None,
statuses: Some(vec![ObjectStatus { statuses: Some(statuses),
status: "clientDeleteProhibited".to_string(),
}]),
}; };
let contacts = &[DomainContact {
contact_type: "billing".to_string(),
id: "eppdev-contact-2".to_string(),
}];
let remove = DomainAddRemove { let remove = DomainAddRemove {
ns: None, ns: None,
contacts: Some(vec![DomainContact { contacts: Some(contacts),
contact_type: "billing".to_string(),
id: "eppdev-contact-2".to_string(),
}]),
statuses: None, statuses: None,
}; };

View File

@ -13,7 +13,7 @@ impl<'a> Command for HostCreate<'a> {
} }
impl<'a> HostCreate<'a> { impl<'a> HostCreate<'a> {
pub fn new(host: &'a str, addresses: Vec<HostAddr>) -> Self { pub fn new(host: &'a str, addresses: &'a [HostAddr]) -> Self {
Self { Self {
host: HostCreateRequestData { host: HostCreateRequestData {
xmlns: XMLNS, xmlns: XMLNS,
@ -37,7 +37,7 @@ pub struct HostCreateRequestData<'a> {
pub name: StringValue<'a>, pub name: StringValue<'a>,
/// The list of IP addresses for the host /// The list of IP addresses for the host
#[serde(rename = "host:addr")] #[serde(rename = "host:addr")]
pub addresses: Option<Vec<HostAddr>>, pub addresses: Option<&'a [HostAddr]>,
} }
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
@ -79,7 +79,7 @@ mod tests {
fn command() { fn command() {
let xml = get_xml("request/host/create.xml").unwrap(); let xml = get_xml("request/host/create.xml").unwrap();
let addresses = vec![ let addresses = &[
HostAddr::new("v4", "29.245.122.14"), HostAddr::new("v4", "29.245.122.14"),
HostAddr::new("v6", "2404:6800:4001:801::200e"), HostAddr::new("v6", "2404:6800:4001:801::200e"),
]; ];

View File

@ -31,12 +31,12 @@ impl<'a> HostUpdate<'a> {
} }
/// Sets the data for the &lt;add&gt; element of the host update /// Sets the data for the &lt;add&gt; element of the host update
pub fn add(&mut self, add: HostAddRemove) { pub fn add(&mut self, add: HostAddRemove<'a>) {
self.host.add = Some(add); self.host.add = Some(add);
} }
/// Sets the data for the &lt;rem&gt; element of the host update /// Sets the data for the &lt;rem&gt; element of the host update
pub fn remove(&mut self, remove: HostAddRemove) { pub fn remove(&mut self, remove: HostAddRemove<'a>) {
self.host.remove = Some(remove); self.host.remove = Some(remove);
} }
} }
@ -51,13 +51,13 @@ pub struct HostChangeInfo<'a> {
/// Type for data under the &lt;add&gt; and &lt;rem&gt; tags /// Type for data under the &lt;add&gt; and &lt;rem&gt; tags
#[derive(Serialize, Debug)] #[derive(Serialize, Debug)]
pub struct HostAddRemove { pub struct HostAddRemove<'a> {
/// The IP addresses to be added to or removed from the host /// The IP addresses to be added to or removed from the host
#[serde(rename = "host:addr")] #[serde(rename = "host:addr")]
pub addresses: Option<Vec<HostAddr>>, pub addresses: Option<&'a [HostAddr]>,
/// The statuses to be added to or removed from the host /// The statuses to be added to or removed from the host
#[serde(rename = "host:status")] #[serde(rename = "host:status")]
pub statuses: Option<Vec<ObjectStatus>>, pub statuses: Option<&'a [ObjectStatus]>,
} }
/// Type for data under the host &lt;update&gt; tag /// Type for data under the host &lt;update&gt; tag
@ -71,10 +71,10 @@ pub struct HostUpdateRequestData<'a> {
name: StringValue<'a>, name: StringValue<'a>,
/// The IP addresses and statuses to be added to the host /// The IP addresses and statuses to be added to the host
#[serde(rename = "host:add")] #[serde(rename = "host:add")]
add: Option<HostAddRemove>, add: Option<HostAddRemove<'a>>,
/// The IP addresses and statuses to be removed from the host /// The IP addresses and statuses to be removed from the host
#[serde(rename = "host:rem")] #[serde(rename = "host:rem")]
remove: Option<HostAddRemove>, remove: Option<HostAddRemove<'a>>,
/// The host details that need to be updated /// The host details that need to be updated
#[serde(rename = "host:chg")] #[serde(rename = "host:chg")]
change_info: Option<HostChangeInfo<'a>>, change_info: Option<HostChangeInfo<'a>>,
@ -99,18 +99,20 @@ mod tests {
fn command() { fn command() {
let xml = get_xml("request/host/update.xml").unwrap(); let xml = get_xml("request/host/update.xml").unwrap();
let addr = vec![HostAddr::new("v6", "2404:6800:4001:801::200e")]; let addr = &[HostAddr::new("v6", "2404:6800:4001:801::200e")];
let add = HostAddRemove { let add = HostAddRemove {
addresses: Some(addr), addresses: Some(addr),
statuses: None, statuses: None,
}; };
let statuses = &[ObjectStatus {
status: "clientDeleteProhibited".to_string(),
}];
let remove = HostAddRemove { let remove = HostAddRemove {
addresses: None, addresses: None,
statuses: Some(vec![ObjectStatus { statuses: Some(statuses),
status: "clientDeleteProhibited".to_string(),
}]),
}; };
let mut object = HostUpdate::new("host1.eppdev-1.com"); let mut object = HostUpdate::new("host1.eppdev-1.com");