Don't to_string() string literals

This commit is contained in:
Jonas Platte 2020-07-14 22:11:05 +02:00
parent 204a90ac43
commit 8683901e14
No known key found for this signature in database
GPG Key ID: 7D261D771D915378
27 changed files with 159 additions and 175 deletions

View File

@ -217,7 +217,7 @@ mod tests {
#[test]
fn test_serialize_third_party_network() {
let json = json!({ "third_party_instance_id": "freenode" });
assert_eq!(to_json_value(RoomNetwork::ThirdParty("freenode".to_string())).unwrap(), json);
assert_eq!(to_json_value(RoomNetwork::ThirdParty("freenode".into())).unwrap(), json);
}
#[test]
@ -225,7 +225,7 @@ mod tests {
let json = json!({ "third_party_instance_id": "freenode" });
assert_eq!(
from_json_value::<RoomNetwork>(json).unwrap(),
RoomNetwork::ThirdParty("freenode".to_string())
RoomNetwork::ThirdParty("freenode".into())
);
}

View File

@ -80,10 +80,10 @@ mod tests {
}))
.unwrap();
let recipient = InvitationRecipient::ThirdPartyId(Invite3pid {
id_server: "example.org".to_string(),
id_access_token: "abcdefghijklmnop".to_string(),
id_server: "example.org".into(),
id_access_token: "abcdefghijklmnop".into(),
medium: Medium::Email,
address: "carl@example.org".to_string(),
address: "carl@example.org".into(),
});
assert_eq!(incoming, recipient);
}

View File

@ -191,7 +191,7 @@ mod tests {
},
login_info: LoginInfo::Token { token: "0xdeadbeef".to_owned() },
device_id: None,
initial_device_display_name: Some("test".to_string()),
initial_device_display_name: Some("test".into()),
}
.try_into()
.unwrap();

View File

@ -43,8 +43,8 @@ mod tests {
#[test]
fn test_serializing_get_tags_response() {
let mut tags = Tags::new();
tags.insert("m.favourite".to_string(), TagInfo { order: Some(0.25) });
tags.insert("u.user_tag".to_string(), TagInfo { order: Some(0.11) });
tags.insert("m.favourite".into(), TagInfo { order: Some(0.25) });
tags.insert("u.user_tag".into(), TagInfo { order: Some(0.11) });
let response = Response { tags };
let http_response = http::Response::<Vec<u8>>::try_from(response).unwrap();

View File

@ -149,8 +149,8 @@ mod tests {
#[test]
fn test_serialize_authentication_data_direct_request() {
let authentication_data = AuthData::DirectRequest {
kind: "example.type.foo".to_string(),
session: Some("ZXY000".to_string()),
kind: "example.type.foo".into(),
session: Some("ZXY000".into()),
auth_parameters: btreemap! {
"example_credential".to_owned() => json!("verypoorsharedsecret")
},
@ -187,8 +187,7 @@ mod tests {
#[test]
fn test_serialize_authentication_data_fallback() {
let authentication_data =
AuthData::FallbackAcknowledgement { session: "ZXY000".to_string() };
let authentication_data = AuthData::FallbackAcknowledgement { session: "ZXY000".into() };
assert_eq!(json!({ "session": "ZXY000" }), to_json_value(authentication_data).unwrap());
}
@ -208,9 +207,9 @@ mod tests {
fn test_serialize_uiaa_info() {
let uiaa_info = UiaaInfo {
flows: vec![AuthFlow {
stages: vec!["m.login.password".to_string(), "m.login.dummy".to_string()],
stages: vec!["m.login.password".into(), "m.login.dummy".into()],
}],
completed: vec!["m.login.password".to_string()],
completed: vec!["m.login.password".into()],
params: to_raw_json_value(&json!({
"example.type.baz": {
"example_key": "foobar"
@ -267,18 +266,18 @@ mod tests {
params,
session: Some(session),
} if error_message == "Invalid password"
&& completed == vec!["example.type.foo".to_string()]
&& completed == vec!["example.type.foo".to_owned()]
&& flows == vec![
AuthFlow {
stages: vec![
"example.type.foo".to_string(),
"example.type.bar".to_string(),
"example.type.foo".into(),
"example.type.bar".into(),
],
},
AuthFlow {
stages: vec![
"example.type.foo".to_string(),
"example.type.baz".to_string(),
"example.type.foo".into(),
"example.type.baz".into(),
],
},
]
@ -295,9 +294,9 @@ mod tests {
fn test_try_uiaa_response_into_http_response() {
let uiaa_info = UiaaInfo {
flows: vec![AuthFlow {
stages: vec!["m.login.password".to_string(), "m.login.dummy".to_string()],
stages: vec!["m.login.password".into(), "m.login.dummy".into()],
}],
completed: vec!["m.login.password".to_string()],
completed: vec!["m.login.password".into()],
params: to_raw_json_value(&json!({
"example.type.baz": {
"example_key": "foobar"
@ -318,9 +317,9 @@ mod tests {
session: None,
auth_error: None,
} if flows == vec![AuthFlow {
stages: vec!["m.login.password".to_string(), "m.login.dummy".to_string()],
stages: vec!["m.login.password".into(), "m.login.dummy".into()],
}]
&& completed == vec!["m.login.password".to_string()]
&& completed == vec!["m.login.password".to_owned()]
&& from_json_str::<JsonValue>(params.get()).unwrap() == json!({
"example.type.baz": {
"example_key": "foobar"
@ -375,18 +374,18 @@ mod tests {
params,
session: Some(session),
} if error_message == "Invalid password"
&& completed == vec!["example.type.foo".to_string()]
&& completed == vec!["example.type.foo".to_owned()]
&& flows == vec![
AuthFlow {
stages: vec![
"example.type.foo".to_string(),
"example.type.bar".to_string(),
"example.type.foo".into(),
"example.type.bar".into(),
],
},
AuthFlow {
stages: vec![
"example.type.foo".to_string(),
"example.type.baz".to_string(),
"example.type.foo".into(),
"example.type.baz".into(),
],
},
]

View File

@ -327,8 +327,8 @@ mod tests {
});
assert_eq!(
to_json_value(&PushCondition::EventMatch {
key: "content.msgtype".to_string(),
pattern: "m.notice".to_string(),
key: "content.msgtype".into(),
pattern: "m.notice".into(),
})
.unwrap(),
json_data
@ -366,7 +366,7 @@ mod tests {
});
assert_eq!(
json_data,
to_json_value(&PushCondition::SenderNotificationPermission { key: "room".to_string() })
to_json_value(&PushCondition::SenderNotificationPermission { key: "room".into() })
.unwrap()
);
}

View File

@ -59,6 +59,6 @@ mod tests {
fn serialize_and_deserialize_from_display_form() {
serde_json_eq(Algorithm::MegolmV1AesSha2, json!("m.megolm.v1.aes-sha2"));
serde_json_eq(Algorithm::OlmV1Curve25519AesSha2, json!("m.olm.v1.curve25519-aes-sha2"));
serde_json_eq(Algorithm::Custom("io.ruma.test".to_string()), json!("io.ruma.test"));
serde_json_eq(Algorithm::Custom("io.ruma.test".into()), json!("io.ruma.test"));
}
}

View File

@ -306,6 +306,6 @@ mod tests {
serde_json_eq(EventType::Sticker, json!("m.sticker"));
serde_json_eq(EventType::Tag, json!("m.tag"));
serde_json_eq(EventType::Typing, json!("m.typing"));
serde_json_eq(EventType::Custom("io.ruma.test".to_string()), json!("io.ruma.test"));
serde_json_eq(EventType::Custom("io.ruma.test".into()), json!("io.ruma.test"));
}
}

View File

@ -133,7 +133,7 @@ mod tests {
#[test]
fn custom_cancel_codes_serialize_to_display_form() {
assert_eq!(
to_json_value(&CancelCode::Custom("io.ruma.test".to_string())).unwrap(),
to_json_value(&CancelCode::Custom("io.ruma.test".into())).unwrap(),
json!("io.ruma.test")
);
}
@ -147,7 +147,7 @@ mod tests {
fn custom_cancel_codes_deserialize_from_display_form() {
assert_eq!(
from_json_value::<CancelCode>(json!("io.ruma.test")).unwrap(),
CancelCode::Custom("io.ruma.test".to_string())
CancelCode::Custom("io.ruma.test".into())
)
}
}

View File

@ -107,12 +107,12 @@ impl MSasV1Content {
/// * `short_authentication_string` does not include `ShortAuthenticationString::Decimal`.
pub fn new(options: MSasV1ContentOptions) -> Result<Self, InvalidInput> {
if !options.key_agreement_protocols.contains(&KeyAgreementProtocol::Curve25519) {
return Err(InvalidInput("`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`".to_string()));
return Err(InvalidInput("`key_agreement_protocols` must contain at least `KeyAgreementProtocol::Curve25519`".into()));
}
if !options.hashes.contains(&HashAlgorithm::Sha256) {
return Err(InvalidInput(
"`hashes` must contain at least `HashAlgorithm::Sha256`".to_string(),
"`hashes` must contain at least `HashAlgorithm::Sha256`".into(),
));
}
@ -120,11 +120,11 @@ impl MSasV1Content {
.message_authentication_codes
.contains(&MessageAuthenticationCode::HkdfHmacSha256)
{
return Err(InvalidInput("`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`".to_string()));
return Err(InvalidInput("`message_authentication_codes` must contain at least `MessageAuthenticationCode::HkdfHmacSha256`".into()));
}
if !options.short_authentication_string.contains(&ShortAuthenticationString::Decimal) {
return Err(InvalidInput("`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`".to_string()));
return Err(InvalidInput("`short_authentication_string` must contain at least `ShortAuthenticationString::Decimal`".into()));
}
Ok(Self {
@ -152,8 +152,8 @@ mod tests {
#[test]
fn invalid_m_sas_v1_content_missing_required_key_agreement_protocols() {
let error = MSasV1Content::new(MSasV1ContentOptions {
from_device: "123".to_string(),
transaction_id: "456".to_string(),
from_device: "123".into(),
transaction_id: "456".into(),
hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
@ -168,8 +168,8 @@ mod tests {
#[test]
fn invalid_m_sas_v1_content_missing_required_hashes() {
let error = MSasV1Content::new(MSasV1ContentOptions {
from_device: "123".to_string(),
transaction_id: "456".to_string(),
from_device: "123".into(),
transaction_id: "456".into(),
hashes: vec![],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
@ -184,8 +184,8 @@ mod tests {
#[test]
fn invalid_m_sas_v1_content_missing_required_message_authentication_codes() {
let error = MSasV1Content::new(MSasV1ContentOptions {
from_device: "123".to_string(),
transaction_id: "456".to_string(),
from_device: "123".into(),
transaction_id: "456".into(),
hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![],
@ -200,8 +200,8 @@ mod tests {
#[test]
fn invalid_m_sas_v1_content_missing_required_short_authentication_string() {
let error = MSasV1Content::new(MSasV1ContentOptions {
from_device: "123".to_string(),
transaction_id: "456".to_string(),
from_device: "123".into(),
transaction_id: "456".into(),
hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],
@ -217,8 +217,8 @@ mod tests {
fn serialization() {
let key_verification_start_content = StartEventContent::MSasV1(
MSasV1Content::new(MSasV1ContentOptions {
from_device: "123".to_string(),
transaction_id: "456".to_string(),
from_device: "123".into(),
transaction_id: "456".into(),
hashes: vec![HashAlgorithm::Sha256],
key_agreement_protocols: vec![KeyAgreementProtocol::Curve25519],
message_authentication_codes: vec![MessageAuthenticationCode::HkdfHmacSha256],

View File

@ -66,12 +66,12 @@ mod tests {
fn serialization() {
let event = PresenceEvent {
content: PresenceEventContent {
avatar_url: Some("mxc://localhost:wefuiwegh8742w".to_string()),
avatar_url: Some("mxc://localhost:wefuiwegh8742w".into()),
currently_active: Some(false),
displayname: None,
last_active_ago: Some(uint!(2_478_593)),
presence: PresenceState::Online,
status_msg: Some("Making cupcakes".to_string()),
status_msg: Some("Making cupcakes".into()),
},
sender: UserId::try_from("@example:localhost").unwrap(),
};

View File

@ -54,7 +54,7 @@ mod tests {
prev_content: None,
room_id: RoomId::try_from("!dummy:example.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData::default(),
};

View File

@ -78,10 +78,10 @@ mod tests {
fn serialization() {
let key_verification_start_content =
EncryptedEventContent::MegolmV1AesSha2(MegolmV1AesSha2Content {
ciphertext: "ciphertext".to_string(),
sender_key: "sender_key".to_string(),
device_id: "device_id".to_string(),
session_id: "session_id".to_string(),
ciphertext: "ciphertext".into(),
sender_key: "sender_key".into(),
device_id: "device_id".into(),
session_id: "session_id".into(),
});
let json_data = json!({

View File

@ -412,9 +412,9 @@ mod tests {
fn serialization() {
let ev = MessageEvent {
content: MessageEventContent::Audio(AudioMessageEventContent {
body: "test".to_string(),
body: "test".into(),
info: None,
url: Some("http://example.com/audio.mp3".to_string()),
url: Some("http://example.com/audio.mp3".into()),
file: None,
}),
event_id: EventId::try_from("$143273582443PhrSn:example.org").unwrap(),
@ -444,9 +444,9 @@ mod tests {
#[test]
fn content_serialization() {
let message_event_content = MessageEventContent::Audio(AudioMessageEventContent {
body: "test".to_string(),
body: "test".into(),
info: None,
url: Some("http://example.com/audio.mp3".to_string()),
url: Some("http://example.com/audio.mp3".into()),
file: None,
});

View File

@ -27,7 +27,7 @@ impl NameEventContent {
match name.len() {
0 => Ok(Self { name: None }),
1..=255 => Ok(Self { name: Some(name) }),
_ => Err(InvalidInput("a room name cannot be more than 255 bytes".to_string())),
_ => Err(InvalidInput("a room name cannot be more than 255 bytes".into())),
}
}
@ -74,13 +74,13 @@ mod tests {
#[test]
fn serialization_with_optional_fields_as_none() {
let name_event = StateEvent {
content: NameEventContent { name: Some("The room name".to_string()) },
content: NameEventContent { name: Some("The room name".into()) },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: None,
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData::default(),
};
@ -103,13 +103,13 @@ mod tests {
#[test]
fn serialization_with_all_fields() {
let name_event = StateEvent {
content: NameEventContent { name: Some("The room name".to_string()) },
content: NameEventContent { name: Some("The room name".into()) },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
prev_content: Some(NameEventContent { name: Some("The old name".to_string()) }),
prev_content: Some(NameEventContent { name: Some("The old name".into()) }),
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData { age: Some(Int::from(100)), ..UnsignedData::default() },
};
@ -233,7 +233,7 @@ mod tests {
#[test]
fn nonempty_field_as_some() {
let name = Some("The room name".to_string());
let name = Some("The room name".into());
let json_data = json!({
"content": {
"name": "The room name"

View File

@ -45,7 +45,7 @@ mod tests {
prev_content: None,
room_id: RoomId::new(server_name),
sender: UserId::new(server_name),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData::default(),
};

View File

@ -154,7 +154,7 @@ mod tests {
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(),
unsigned: UnsignedData::default(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
};
let actual = to_json_value(&power_levels_event).unwrap();
@ -213,7 +213,7 @@ mod tests {
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(),
unsigned: UnsignedData { age: Some(Int::from(100)), ..UnsignedData::default() },
sender: user,
state_key: "".to_string(),
state_key: "".into(),
};
let actual = to_json_value(&power_levels_event).unwrap();

View File

@ -51,7 +51,7 @@ fn serialize_custom_message_event() {
},
"msgtype": "m.text"
}),
event_type: "m.room.message".to_string(),
event_type: "m.room.message".into(),
},
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(10),
@ -91,14 +91,14 @@ fn serialize_custom_state_event() {
json: json!({
"custom": 10
}),
event_type: "m.made.up".to_string(),
event_type: "m.made.up".into(),
},
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(10),
prev_content: None,
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData::default(),
});

View File

@ -214,7 +214,7 @@ fn deserialize_message_sticker() {
size: thumb_size,
} if *thumb_width == UInt::new(800)
&& *thumb_height == UInt::new(334)
&& *thumb_mimetype == Some("image/png".to_string())
&& *thumb_mimetype == Some("image/png".into())
&& *thumb_size == UInt::new(82595)
)
&& url == "http://www.matrix.org"

View File

@ -16,34 +16,32 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
fn serialize_stub_as_v1() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
"ed25519:key_version".to_string(),
"86BytesOfSignatureOfTheRedactedEvent".to_string(),
);
signatures.insert("example.com".to_string(), inner_signature);
inner_signature
.insert("ed25519:key_version".into(), "86BytesOfSignatureOfTheRedactedEvent".into());
signatures.insert("example.com".into(), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".to_string(), json!({"a": 456}));
unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_stub = RoomV1PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(),
origin: "matrix.org".to_string(),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".to_string()),
state_key: Some("state".into()),
prev_events: vec![(
EventId::try_from("$previousevent:matrix.org").unwrap(),
EventHash { sha256: "123567".to_string() },
EventHash { sha256: "123567".into() },
)],
depth: 2_u32.into(),
auth_events: vec![(
EventId::try_from("$someauthevent:matrix.org").unwrap(),
EventHash { sha256: "21389CFEDABC".to_string() },
EventHash { sha256: "21389CFEDABC".into() },
)],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()),
unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".to_string() },
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures,
};
let pdu_stub = PduStub::RoomV1PduStub(v1_stub);
@ -79,28 +77,26 @@ fn serialize_stub_as_v1() {
fn serialize_stub_as_v3() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
"ed25519:key_version".to_string(),
"86BytesOfSignatureOfTheRedactedEvent".to_string(),
);
signatures.insert("example.com".to_string(), inner_signature);
inner_signature
.insert("ed25519:key_version".into(), "86BytesOfSignatureOfTheRedactedEvent".into());
signatures.insert("example.com".into(), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".to_string(), json!({"a": 456}));
unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_stub = RoomV3PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(),
origin: "matrix.org".to_string(),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".to_string()),
state_key: Some("state".into()),
prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()],
depth: 2_u32.into(),
auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()),
unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".to_string() },
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures,
};
let pdu_stub = PduStub::RoomV3PduStub(v3_stub);
@ -238,36 +234,34 @@ fn deserialize_stub_as_v3() {
fn serialize_pdu_as_v1() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
"ed25519:key_version".to_string(),
"86BytesOfSignatureOfTheRedactedEvent".to_string(),
);
signatures.insert("example.com".to_string(), inner_signature);
inner_signature
.insert("ed25519:key_version".into(), "86BytesOfSignatureOfTheRedactedEvent".into());
signatures.insert("example.com".into(), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".to_string(), json!({"a": 456}));
unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_pdu = RoomV1Pdu {
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(),
event_id: EventId::try_from("$somejoinevent:matrix.org").unwrap(),
sender: UserId::try_from("@sender:example.com").unwrap(),
origin: "matrix.org".to_string(),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".to_string()),
state_key: Some("state".into()),
prev_events: vec![(
EventId::try_from("$previousevent:matrix.org").unwrap(),
EventHash { sha256: "123567".to_string() },
EventHash { sha256: "123567".into() },
)],
depth: 2_u32.into(),
auth_events: vec![(
EventId::try_from("$someauthevent:matrix.org").unwrap(),
EventHash { sha256: "21389CFEDABC".to_string() },
EventHash { sha256: "21389CFEDABC".into() },
)],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()),
unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".to_string() },
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures,
};
let pdu = Pdu::RoomV1Pdu(v1_pdu);
@ -305,29 +299,27 @@ fn serialize_pdu_as_v1() {
fn serialize_pdu_as_v3() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
"ed25519:key_version".to_string(),
"86BytesOfSignatureOfTheRedactedEvent".to_string(),
);
signatures.insert("example.com".to_string(), inner_signature);
inner_signature
.insert("ed25519:key_version".into(), "86BytesOfSignatureOfTheRedactedEvent".into());
signatures.insert("example.com".into(), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".to_string(), json!({"a": 456}));
unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_pdu = RoomV3Pdu {
room_id: RoomId::try_from("!n8f893n9:example.com").unwrap(),
sender: UserId::try_from("@sender:example.com").unwrap(),
origin: "matrix.org".to_string(),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".to_string()),
state_key: Some("state".into()),
prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()],
depth: 2_u32.into(),
auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()),
unsigned,
hashes: EventHash { sha256: "1233543bABACDEF".to_string() },
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures,
};
let pdu_stub = Pdu::RoomV3Pdu(v3_pdu);
@ -469,34 +461,32 @@ fn deserialize_pdu_as_v3() {
fn convert_v1_stub_to_pdu() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
"ed25519:key_version".to_string(),
"86BytesOfSignatureOfTheRedactedEvent".to_string(),
);
signatures.insert("example.com".to_string(), inner_signature);
inner_signature
.insert("ed25519:key_version".into(), "86BytesOfSignatureOfTheRedactedEvent".into());
signatures.insert("example.com".into(), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".to_string(), json!({"a": 456}));
unsigned.insert("somekey".into(), json!({"a": 456}));
let v1_stub = RoomV1PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(),
origin: "matrix.org".to_string(),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".to_string()),
state_key: Some("state".into()),
prev_events: vec![(
EventId::try_from("$previousevent:matrix.org").unwrap(),
EventHash { sha256: "123567".to_string() },
EventHash { sha256: "123567".into() },
)],
depth: 2_u32.into(),
auth_events: vec![(
EventId::try_from("$someauthevent:matrix.org").unwrap(),
EventHash { sha256: "21389CFEDABC".to_string() },
EventHash { sha256: "21389CFEDABC".into() },
)],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()),
unsigned: (&unsigned).clone(),
hashes: EventHash { sha256: "1233543bABACDEF".to_string() },
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures: (&signatures).clone(),
};
@ -528,7 +518,7 @@ fn convert_v1_stub_to_pdu() {
&& origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658)
&& kind == EventType::RoomPowerLevels
&& content == json!({"testing": 123})
&& state_key == Some("state".to_string())
&& state_key == Some("state".into())
&& prev_events[0].0 == EventId::try_from("$previousevent:matrix.org").unwrap()
&& prev_events[0].1.sha256 == "123567"
&& depth == 2_u32.into()
@ -545,28 +535,26 @@ fn convert_v1_stub_to_pdu() {
fn convert_v3_stub_to_pdu() {
let mut signatures = BTreeMap::new();
let mut inner_signature = BTreeMap::new();
inner_signature.insert(
"ed25519:key_version".to_string(),
"86BytesOfSignatureOfTheRedactedEvent".to_string(),
);
signatures.insert("example.com".to_string(), inner_signature);
inner_signature
.insert("ed25519:key_version".into(), "86BytesOfSignatureOfTheRedactedEvent".into());
signatures.insert("example.com".into(), inner_signature);
let mut unsigned = BTreeMap::new();
unsigned.insert("somekey".to_string(), json!({"a": 456}));
unsigned.insert("somekey".into(), json!({"a": 456}));
let v3_stub = RoomV3PduStub {
sender: UserId::try_from("@sender:example.com").unwrap(),
origin: "matrix.org".to_string(),
origin: "matrix.org".into(),
origin_server_ts: SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658),
kind: EventType::RoomPowerLevels,
content: json!({"testing": 123}),
state_key: Some("state".to_string()),
state_key: Some("state".into()),
prev_events: vec![EventId::try_from("$previousevent:matrix.org").unwrap()],
depth: 2_u32.into(),
auth_events: vec![EventId::try_from("$someauthevent:matrix.org").unwrap()],
redacts: Some(EventId::try_from("$9654:matrix.org").unwrap()),
unsigned: (&unsigned).clone(),
hashes: EventHash { sha256: "1233543bABACDEF".to_string() },
hashes: EventHash { sha256: "1233543bABACDEF".into() },
signatures: (&signatures).clone(),
};
@ -593,7 +581,7 @@ fn convert_v3_stub_to_pdu() {
&& origin_server_ts == SystemTime::UNIX_EPOCH + Duration::from_millis(1_592_050_773_658)
&& kind == EventType::RoomPowerLevels
&& content == json!({"testing": 123})
&& state_key == Some("state".to_string())
&& state_key == Some("state".into())
&& prev_events == vec![EventId::try_from("$previousevent:matrix.org").unwrap()]
&& depth == 2_u32.into()
&& auth_events == vec![EventId::try_from("$someauthevent:matrix.org").unwrap()]

View File

@ -49,7 +49,7 @@ fn redacted_aliases_event_serialize() {
let redacted = RedactedSyncStateEvent {
content: RedactedAliasesEventContent { aliases: None },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
sender: UserId::try_from("@carl:example.com").unwrap(),
unsigned: UnsignedData::default(),
@ -246,10 +246,10 @@ fn redacted_custom_event_deserialize() {
}));
let redacted = RedactedSyncStateEvent {
content: RedactedCustomEventContent { event_type: "m.made.up".to_string() },
content: RedactedCustomEventContent { event_type: "m.made.up".into() },
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "hello there".to_string(),
state_key: "hello there".into(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
unsigned: unsigned.clone(),
};

View File

@ -30,7 +30,7 @@ fn redaction() -> JsonValue {
#[test]
fn serialize_redaction() {
let aliases_event = RedactionEvent {
content: RedactionEventContent { reason: Some("being a turd".to_string()) },
content: RedactionEventContent { reason: Some("being a turd".into()) },
redacts: EventId::try_from("$nomore:example.com").unwrap(),
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),

View File

@ -45,7 +45,7 @@ fn serialize_aliases_with_prev_content() {
})),
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData::default(),
};
@ -66,7 +66,7 @@ fn serialize_aliases_without_prev_content() {
prev_content: None,
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
sender: UserId::try_from("@carl:example.com").unwrap(),
state_key: "".to_string(),
state_key: "".into(),
unsigned: UnsignedData::default(),
};
@ -228,7 +228,7 @@ fn deserialize_avatar_without_prev_content() {
size: thumb_size,
} if *thumb_width == UInt::new(800)
&& *thumb_height == UInt::new(334)
&& *thumb_mimetype == Some("image/png".to_string())
&& *thumb_mimetype == Some("image/png".into())
&& *thumb_size == UInt::new(82595)
&& *thumbnail_url == "mxc://matrix.org"
)
@ -279,6 +279,6 @@ fn deserialize_member_event_with_top_level_membership_field() {
)) if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
&& sender == UserId::try_from("@example:localhost").unwrap()
&& content.displayname == Some("example".to_string())
&& content.displayname == Some("example".into())
);
}

View File

@ -12,9 +12,9 @@ use serde_json::{from_value as from_json_value, json, to_value as to_json_value}
fn serialize_stripped_state_event_any_content() {
let event = StrippedStateEvent {
content: AnyStateEventContent::RoomTopic(TopicEventContent {
topic: "Testing room".to_string(),
topic: "Testing room".into(),
}),
state_key: "".to_string(),
state_key: "".into(),
sender: UserId::try_from("@example:localhost").unwrap(),
};
@ -33,8 +33,8 @@ fn serialize_stripped_state_event_any_content() {
#[test]
fn serialize_stripped_state_event_any_event() {
let event = AnyStrippedStateEvent::RoomTopic(StrippedStateEvent {
content: TopicEventContent { topic: "Testing room".to_string() },
state_key: "".to_string(),
content: TopicEventContent { topic: "Testing room".into() },
state_key: "".into(),
sender: UserId::try_from("@example:localhost").unwrap(),
});

View File

@ -88,11 +88,8 @@ mod tests {
#[test]
fn test_serialize_response() {
let room_state = RoomState {
origin: "matrix.org".to_string(),
auth_chain: Vec::new(),
state: Vec::new(),
};
let room_state =
RoomState { origin: "matrix.org".into(), auth_chain: Vec::new(), state: Vec::new() };
let serialized = serialize(&room_state, serde_json::value::Serializer).unwrap();
let expected = to_json_value(&json!(

View File

@ -85,7 +85,7 @@ static REFERENCE_HASH_FIELDS_TO_REMOVE: &[&str] = &["age_ts", "signatures", "uns
/// // Create an Ed25519 key pair.
/// let key_pair = ruma_signatures::Ed25519KeyPair::new(
/// &document,
/// "1".to_string(), // The "version" of the key.
/// "1".into(), // The "version" of the key.
/// ).unwrap();
///
/// // Deserialize some JSON.
@ -148,10 +148,10 @@ where
let map = value.as_object_mut().unwrap();
// Put `signatures` and `unsigned` back in.
map.insert("signatures".to_string(), to_value(signature_map)?);
map.insert("signatures".into(), to_value(signature_map)?);
if let Some(unsigned) = maybe_unsigned {
map.insert("unsigned".to_string(), to_value(unsigned)?);
map.insert("unsigned".into(), to_value(unsigned)?);
}
Ok(())
@ -221,9 +221,9 @@ pub fn canonical_json(value: &Value) -> Result<String, Error> {
///
/// // Create the `PublicKeyMap` that will inform `verify_json` which signatures to verify.
/// let mut public_key_set = HashMap::new();
/// public_key_set.insert("ed25519:1".to_string(), PUBLIC_KEY.to_string());
/// public_key_set.insert("ed25519:1".into(), PUBLIC_KEY.to_string());
/// let mut public_key_map = HashMap::new();
/// public_key_map.insert("domain".to_string(), public_key_set);
/// public_key_map.insert("domain".into(), public_key_set);
///
/// // Verify at least one signature for each entity in `public_key_map`.
/// assert!(ruma_signatures::verify_json(&public_key_map, &value).is_ok());
@ -400,7 +400,7 @@ pub fn reference_hash(value: &Value) -> Result<String, Error> {
/// // Create an Ed25519 key pair.
/// let key_pair = ruma_signatures::Ed25519KeyPair::new(
/// &document,
/// "1".to_string(), // The "version" of the key.
/// "1".into(), // The "version" of the key.
/// ).unwrap();
///
/// // Deserialize an event from JSON.
@ -472,7 +472,7 @@ where
map.entry("hashes").or_insert_with(|| Value::Object(Map::with_capacity(1)));
match hashes_value.as_object_mut() {
Some(hashes) => hashes.insert("sha256".to_string(), Value::String(hash)),
Some(hashes) => hashes.insert("sha256".into(), Value::String(hash)),
None => return Err(Error::new("field `hashes` must be a JSON object")),
};
}
@ -484,7 +484,7 @@ where
// Safe to unwrap because we did this exact check at the beginning of the function.
let map = value.as_object_mut().unwrap();
map.insert("signatures".to_string(), redacted["signatures"].take());
map.insert("signatures".into(), redacted["signatures"].take());
Ok(())
}
@ -543,9 +543,9 @@ where
///
/// // Create the `PublicKeyMap` that will inform `verify_json` which signatures to verify.
/// let mut public_key_set = HashMap::new();
/// public_key_set.insert("ed25519:1".to_string(), PUBLIC_KEY.to_string());
/// public_key_set.insert("ed25519:1".into(), PUBLIC_KEY.to_string());
/// let mut public_key_map = HashMap::new();
/// public_key_map.insert("domain".to_string(), public_key_set);
/// public_key_map.insert("domain".into(), public_key_set);
///
/// // Verify at least one signature for each entity in `public_key_map`.
/// assert!(ruma_signatures::verify_event(&public_key_map, &value).is_ok());

View File

@ -296,7 +296,7 @@ mod test {
fn sign_empty_json() {
let key_pair = Ed25519KeyPair::new(
decode_config(&PKCS8, STANDARD_NO_PAD).unwrap().as_slice(),
"1".to_string(),
"1".into(),
)
.unwrap();
@ -315,10 +315,10 @@ mod test {
let value = from_str(r#"{"signatures":{"domain":{"ed25519:1":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}}"#).unwrap();
let mut signature_set = HashMap::new();
signature_set.insert("ed25519:1".to_string(), public_key_string());
signature_set.insert("ed25519:1".into(), public_key_string());
let mut public_key_map = HashMap::new();
public_key_map.insert("domain".to_string(), signature_set);
public_key_map.insert("domain".into(), signature_set);
assert!(verify_json(&public_key_map, &value).is_ok());
}
@ -327,7 +327,7 @@ mod test {
fn sign_minimal_json() {
let key_pair = Ed25519KeyPair::new(
decode_config(&PKCS8, STANDARD_NO_PAD).unwrap().as_slice(),
"1".to_string(),
"1".into(),
)
.unwrap();
@ -366,10 +366,10 @@ mod test {
).unwrap();
let mut signature_set = HashMap::new();
signature_set.insert("ed25519:1".to_string(), public_key_string());
signature_set.insert("ed25519:1".into(), public_key_string());
let mut public_key_map = HashMap::new();
public_key_map.insert("domain".to_string(), signature_set);
public_key_map.insert("domain".into(), signature_set);
assert!(verify_json(&public_key_map, &value).is_ok());
@ -385,10 +385,10 @@ mod test {
let value = from_str(r#"{"not":"empty","signatures":{"domain":"lXjsnvhVlz8t3etR+6AEJ0IT70WujeHC1CFjDDsVx0xSig1Bx7lvoi1x3j/2/GPNjQM4a2gD34UqsXFluaQEBA"}}"#).unwrap();
let mut signature_set = HashMap::new();
signature_set.insert("ed25519:1".to_string(), public_key_string());
signature_set.insert("ed25519:1".into(), public_key_string());
let mut public_key_map = HashMap::new();
public_key_map.insert("domain".to_string(), signature_set);
public_key_map.insert("domain".into(), signature_set);
assert!(verify_json(&public_key_map, &value).is_err());
}
@ -397,7 +397,7 @@ mod test {
fn sign_minimal_event() {
let key_pair = Ed25519KeyPair::new(
decode_config(&PKCS8, STANDARD_NO_PAD).unwrap().as_slice(),
"1".to_string(),
"1".into(),
)
.unwrap();
@ -431,7 +431,7 @@ mod test {
fn sign_redacted_event() {
let key_pair = Ed25519KeyPair::new(
decode_config(&PKCS8, STANDARD_NO_PAD).unwrap().as_slice(),
"1".to_string(),
"1".into(),
)
.unwrap();
@ -463,10 +463,10 @@ mod test {
#[test]
fn verify_minimal_event() {
let mut signature_set = HashMap::new();
signature_set.insert("ed25519:1".to_string(), public_key_string());
signature_set.insert("ed25519:1".into(), public_key_string());
let mut public_key_map = HashMap::new();
public_key_map.insert("domain".to_string(), signature_set);
public_key_map.insert("domain".into(), signature_set);
let value = from_str(
r#"{