diff --git a/src/bookmarks.rs b/src/bookmarks.rs index 12f95707fc8f7c7bfbcc61d938be68aae9aa7b2e..273f770875d538ebbbfc4f254a4a379d304fd89e 100644 --- a/src/bookmarks.rs +++ b/src/bookmarks.rs @@ -49,6 +49,7 @@ generate_element!( generate_element!( /// Container element for multiple bookmarks. + #[derive(Default)] Storage, "storage", BOOKMARKS, children: [ /// Conferences the user has expressed an interest in. @@ -62,10 +63,7 @@ generate_element!( impl Storage { /// Create an empty bookmarks storage. pub fn new() -> Storage { - Storage { - conferences: Vec::new(), - urls: Vec::new(), - } + Storage::default() } } diff --git a/src/caps.rs b/src/caps.rs index 419e72ccce184ed963e4ca271d10e0e412367e71..a7948d191909d55db79d2692fad3a340f1270665 100644 --- a/src/caps.rs +++ b/src/caps.rs @@ -54,7 +54,7 @@ impl TryFrom for Caps { Ok(Caps { ext: get_attr!(elem, "ext", optional), node: get_attr!(elem, "node", required), - hash: hash, + hash, }) } } @@ -202,7 +202,7 @@ pub fn hash_caps(data: &[u8], algo: Algo) -> Result { } Algo::Unknown(algo) => return Err(format!("Unknown algorithm: {}.", algo)), }, - algo: algo, + algo, }) } diff --git a/src/component.rs b/src/component.rs index 722495b8a02b6aea322c16a990070586e299ea4b..4ab78c57c28bf9ffba5ad50cd4bf9588c69fad8b 100644 --- a/src/component.rs +++ b/src/component.rs @@ -10,6 +10,7 @@ use sha1::Sha1; generate_element!( /// The main authentication mechanism for components. + #[derive(Default)] Handshake, "handshake", COMPONENT, text: ( /// If Some, contains the hex-encoded SHA-1 of the concatenation of the @@ -25,7 +26,7 @@ generate_element!( impl Handshake { /// Creates a successful reply from a server. pub fn new() -> Handshake { - Handshake { data: None } + Handshake::default() } /// Creates an authentication request from the component. diff --git a/src/data_forms.rs b/src/data_forms.rs index 30dbd29aa478ac2c9dd918bb3de0e7c08f4196c7..bce941364d75e7f228bb3170a2c9852af5ccc1bd 100644 --- a/src/data_forms.rs +++ b/src/data_forms.rs @@ -222,7 +222,7 @@ impl TryFrom for DataForm { check_no_unknown_attributes!(elem, "x", ["type"]); let type_ = get_attr!(elem, "type", required); let mut form = DataForm { - type_: type_, + type_, form_type: None, title: None, instructions: None, diff --git a/src/disco.rs b/src/disco.rs index ee673b8f92f0bfa314f4b4abfdf903725c7893f6..9f72cec56677c7d072657a43a093d250f248940d 100644 --- a/src/disco.rs +++ b/src/disco.rs @@ -86,8 +86,8 @@ impl TryFrom for Identity { } Ok(Identity { - category: category, - type_: type_, + category, + type_, lang: get_attr!(elem, "xml:lang", optional), name: get_attr!(elem, "name", optional), }) diff --git a/src/ecaps2.rs b/src/ecaps2.rs index 846fa6da0d75e1720e4bdff06a9a3e33cd3ffad6..ef3a59325d5e6afb59ab8a8d40ad54f5575704d0 100644 --- a/src/ecaps2.rs +++ b/src/ecaps2.rs @@ -136,7 +136,7 @@ pub fn hash_ecaps2(data: &[u8], algo: Algo) -> Result { Algo::Sha_1 => return Err(String::from("Disabled algorithm sha-1: unsafe.")), Algo::Unknown(algo) => return Err(format!("Unknown algorithm: {}.", algo)), }, - algo: algo, + algo, }) } diff --git a/src/iq.rs b/src/iq.rs index ccb503ac18f890ea74ae0758e8dcdda09b7cf46c..dd04e6b8f9d2c385ff25e2460583c59cd5df43cc 100644 --- a/src/iq.rs +++ b/src/iq.rs @@ -96,7 +96,7 @@ impl Iq { from: None, to: None, id, - payload: IqType::Result(payload.map(|payload| payload.into())), + payload: IqType::Result(payload.map(Into::into)), } } @@ -188,9 +188,9 @@ impl TryFrom for Iq { }; Ok(Iq { - from: from, - to: to, - id: id, + from, + to, + id, payload: type_, }) } diff --git a/src/jingle.rs b/src/jingle.rs index 63c78a0f808ec9b678502ca1a19ddcc2e44ba2a8..2596687218cca28b0f0c6a0cc4ba16bbc3dc19a4 100644 --- a/src/jingle.rs +++ b/src/jingle.rs @@ -398,8 +398,8 @@ impl TryFrom for ReasonElement { "Reason doesn’t contain a valid reason.", ))?; Ok(ReasonElement { - reason: reason, - text: text, + reason, + text, }) } } @@ -449,8 +449,8 @@ impl Jingle { /// Create a new Jingle element. pub fn new(action: Action, sid: SessionId) -> Jingle { Jingle { - action: action, - sid: sid, + action, + sid, initiator: None, responder: None, contents: Vec::new(), diff --git a/src/jingle_ft.rs b/src/jingle_ft.rs index fce18cdf47ff7d8eda6b0eb153b88364ab2249fb..2d61569cb6209bccfed5e75d5a0e1a013614dccb 100644 --- a/src/jingle_ft.rs +++ b/src/jingle_ft.rs @@ -47,7 +47,7 @@ generate_id!( ); /// Represents a file to be transferred. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct File { /// The date of last modification of this file. pub date: Option, @@ -74,15 +74,7 @@ pub struct File { impl File { /// Creates a new file descriptor. pub fn new() -> File { - File { - date: None, - media_type: None, - name: None, - descs: BTreeMap::new(), - size: None, - range: None, - hashes: Vec::new(), - } + File::default() } /// Sets the date of last modification on this file. diff --git a/src/jingle_s5b.rs b/src/jingle_s5b.rs index 58c4a27dd36a705bb5a6bd159459a1700655116f..613842f900ca1b8d9179d6f675945a62aa24f600 100644 --- a/src/jingle_s5b.rs +++ b/src/jingle_s5b.rs @@ -232,10 +232,10 @@ impl TryFrom for Transport { } let payload = payload.unwrap_or(TransportPayload::None); Ok(Transport { - sid: sid, - dstaddr: dstaddr, - mode: mode, - payload: payload, + sid, + dstaddr, + mode, + payload, }) } } diff --git a/src/message.rs b/src/message.rs index fe32300e5d353a33aefda76822c6fbd66f436dfe..f2611accd1a2ee40c6412854593ed37c62928b28 100644 --- a/src/message.rs +++ b/src/message.rs @@ -98,7 +98,7 @@ impl Message { pub fn new(to: Option) -> Message { Message { from: None, - to: to, + to, id: None, type_: MessageType::Chat, bodies: BTreeMap::new(), @@ -192,14 +192,14 @@ impl TryFrom for Message { } } Ok(Message { - from: from, - to: to, - id: id, - type_: type_, - bodies: bodies, - subjects: subjects, - thread: thread, - payloads: payloads, + from, + to, + id, + type_, + bodies, + subjects, + thread, + payloads, }) } } diff --git a/src/muc/muc.rs b/src/muc/muc.rs index 9d29cc963c924481d79855c4e7791aebfa4e63b3..a4e60a4dd5d0e5c01a76f197fb0e0f6dead86247 100644 --- a/src/muc/muc.rs +++ b/src/muc/muc.rs @@ -10,7 +10,7 @@ use crate::presence::PresencePayload; generate_element!( /// Represents the query for messages before our join. - #[derive(PartialEq)] + #[derive(PartialEq, Default)] History, "history", MUC, attributes: [ /// How many characters of history to send, in XML characters. @@ -30,12 +30,7 @@ generate_element!( impl History { /// Create a new empty history element. pub fn new() -> Self { - History { - maxchars: None, - maxstanzas: None, - seconds: None, - since: None, - } + History::default() } /// Set how many characters of history to send. @@ -65,7 +60,7 @@ impl History { generate_element!( /// Represents a room join request. - #[derive(PartialEq)] + #[derive(PartialEq, Default)] Muc, "x", MUC, children: [ /// Password to use when the room is protected by a password. password: Option = ("password", MUC) => String, @@ -80,10 +75,7 @@ impl PresencePayload for Muc {} impl Muc { /// Create a new MUC join element. pub fn new() -> Self { - Muc { - password: None, - history: None, - } + Muc::default() } /// Join a room with this password. diff --git a/src/muc/user.rs b/src/muc/user.rs index 46a893337ea7b8525c35c67850dcc31382a45487..8232baf0a875d42208003bb624d6ea0106fb040e 100644 --- a/src/muc/user.rs +++ b/src/muc/user.rs @@ -100,9 +100,9 @@ impl TryFrom for Actor { match (jid, nick) { (Some(_), Some(_)) | (None, None) => { - return Err(Error::ParseError( + Err(Error::ParseError( "Either 'jid' or 'nick' attribute is required.", - )); + )) } (Some(jid), _) => Ok(Actor::Jid(jid)), (_, Some(nick)) => Ok(Actor::Nick(nick)), diff --git a/src/presence.rs b/src/presence.rs index 4a464f9c3ced4dfb767f71f38404ef203479fb09..e7d2a55df7c6b0f5d80d0f38b488c236c9677d6c 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -197,7 +197,7 @@ impl Presence { from: None, to: None, id: None, - type_: type_, + type_, show: Show::None, statuses: BTreeMap::new(), priority: 0i8, diff --git a/src/pubsub/event.rs b/src/pubsub/event.rs index e0f2b598d06b1b3fb840fe99bb434f553516fbaf..0c616ab64225b2a5c894ad42e481ae8c425840c2 100644 --- a/src/pubsub/event.rs +++ b/src/pubsub/event.rs @@ -178,7 +178,7 @@ impl TryFrom for PubSubEvent { } else if child.is("subscription", ns::PUBSUB_EVENT) { check_no_children!(child, "subscription"); payload = Some(PubSubEvent::Subscription { - node: node, + node, expiry: get_attr!(child, "expiry", optional), jid: get_attr!(child, "jid", optional), subid: get_attr!(child, "subid", optional), diff --git a/src/pubsub/mod.rs b/src/pubsub/mod.rs index 81e4e359f14cd54d83f8aa72848a0f474e1d1d8e..cde7e77529d65e99dfd0b4f96bc7f198449cd8ee 100644 --- a/src/pubsub/mod.rs +++ b/src/pubsub/mod.rs @@ -67,7 +67,7 @@ impl Item { Item { id, publisher, - payload: payload.map(|payload| payload.into()), + payload: payload.map(Into::into), } } } diff --git a/src/sasl.rs b/src/sasl.rs index fecdbfa6b6ca7017593af6d3df9bd7839daa1e9d..8f63f092a937ba38e77b2984c04f239a0bd8b745 100644 --- a/src/sasl.rs +++ b/src/sasl.rs @@ -192,8 +192,8 @@ impl TryFrom for Failure { defined_condition.ok_or(Error::ParseError("Failure must have a defined-condition."))?; Ok(Failure { - defined_condition: defined_condition, - texts: texts, + defined_condition, + texts, }) } } diff --git a/src/sm.rs b/src/sm.rs index fc8e7377609a0db349799f2cc9bf033fafd19fe8..1f48615e30da58c541955deade8c912e86bc04de 100644 --- a/src/sm.rs +++ b/src/sm.rs @@ -31,6 +31,7 @@ generate_attribute!( generate_element!( /// Client request for enabling stream management. + #[derive(Default)] Enable, "enable", SM, attributes: [ /// The preferred resumption time in seconds by the client. @@ -45,10 +46,7 @@ generate_element!( impl Enable { /// Generates a new `` element. pub fn new() -> Self { - Enable { - max: None, - resume: ResumeAttr::False, - } + Enable::default() } /// Sets the preferred resumption time in seconds. diff --git a/src/stanza_error.rs b/src/stanza_error.rs index 7a4d421aad5505ce9f02a93c2169a4180ba6bd1b..d254f6b48d9a0e17c9f8fdd8d97f294a557056f6 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -258,11 +258,11 @@ impl TryFrom for StanzaError { defined_condition.ok_or(Error::ParseError("Error must have a defined-condition."))?; Ok(StanzaError { - type_: type_, - by: by, - defined_condition: defined_condition, - texts: texts, - other: other, + type_, + by, + defined_condition, + texts, + other, }) } } diff --git a/src/util/helpers.rs b/src/util/helpers.rs index 2aa37013ea5a7607ca057dd72846721f27e812e6..b3832345c33fd41698c71cce88b7f7dd913aa0e0 100644 --- a/src/util/helpers.rs +++ b/src/util/helpers.rs @@ -19,7 +19,7 @@ impl PlainText { } pub fn encode(string: &Option) -> Option { - string.as_ref().map(|text| text.to_owned()) + string.as_ref().map(ToOwned::to_owned) } } @@ -34,7 +34,7 @@ impl TrimmedPlainText { }) } - pub fn encode(string: &String) -> String { + pub fn encode(string: &str) -> String { string.to_owned() } } @@ -47,7 +47,7 @@ impl Base64 { Ok(base64::decode(s)?) } - pub fn encode(b: &Vec) -> Option { + pub fn encode(b: &[u8]) -> Option { Some(base64::encode(b)) } } @@ -57,11 +57,11 @@ pub struct WhitespaceAwareBase64; impl WhitespaceAwareBase64 { pub fn decode(s: &str) -> Result, Error> { - let s: String = s.chars().into_iter().filter(|ch| *ch != ' ' && *ch != '\n' && *ch != '\t').collect(); + let s: String = s.chars().filter(|ch| *ch != ' ' && *ch != '\n' && *ch != '\t').collect(); Ok(base64::decode(&s)?) } - pub fn encode(b: &Vec) -> Option { + pub fn encode(b: &[u8]) -> Option { Some(base64::encode(b)) } }