Apply cargo clippy suggestions to reduce not needed allocations (#750)

* Apply cargo clippy suggestions to reduce not needed allocations

* Add changelog entry
This commit is contained in:
Marcel 2023-05-17 15:53:42 +02:00 committed by GitHub
parent 7223a86b2b
commit 409a8b83a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 47 deletions

1
changelog.d/750.misc Normal file
View File

@ -0,0 +1 @@
Apply non-style suggestions by `cargo clippy` to reduce allocations in the rust code.

View File

@ -76,7 +76,7 @@ impl BridgePermissions {
#[napi]
pub fn get_interested_rooms(&self) -> Vec<String> {
self.room_membership.keys().map(|k| k.clone()).collect()
self.room_membership.keys().cloned().collect()
}
#[napi]

View File

@ -34,7 +34,7 @@ fn parse_channel_to_js_result(channel: &Channel) -> JsRssChannel {
.iter()
.map(|item: &rss::Item| FeedItem {
title: item.title().map(String::from),
link: item.link().and_then(|v| Some(v.to_string())).or_else(|| {
link: item.link().map(ToString::to_string).or_else(|| {
item.guid()
.and_then(|i| i.permalink.then(|| i.value.to_string()))
}),
@ -46,7 +46,7 @@ fn parse_channel_to_js_result(channel: &Channel) -> JsRssChannel {
hash_id: item
.guid
.clone()
.and_then(|f| Some(f.value))
.map(|f| f.value)
.or(item.link.clone())
.or(item.title.clone())
.and_then(|f| hash_id(f).ok()),
@ -57,7 +57,7 @@ fn parse_channel_to_js_result(channel: &Channel) -> JsRssChannel {
fn parse_feed_to_js_result(feed: &Feed) -> JsRssChannel {
fn authors_to_string(persons: &[Person]) -> Option<String> {
if persons.len() == 0 {
if persons.is_empty() {
return None;
}
let mut outs = Vec::<String>::new();
@ -65,11 +65,11 @@ fn parse_feed_to_js_result(feed: &Feed) -> JsRssChannel {
let email = person
.email
.clone()
.map_or_else(|| String::new(), |v| format!("<{}>", v));
.map_or_else(String::new, |v| format!("<{}>", v));
let uri = person
.uri
.clone()
.map_or_else(|| String::new(), |v| format!("<{}>", v));
.map_or_else(String::new, |v| format!("<{}>", v));
outs.push(format!("{}{}{}", person.name, email, uri))
}
Some(outs.join(", "))
@ -106,47 +106,42 @@ pub fn js_parse_feed(xml: String) -> Result<JsRssChannel, JsError> {
{
match Feed::from_str(&xml) {
Ok(feed) => Ok(parse_feed_to_js_result(&feed)),
Err(AtomError::Eof) => Err(JsError::new(
Status::Unknown,
format!("Unexpected end of input.").to_string(),
)),
Err(AtomError::Eof) => {
Err(JsError::new(Status::Unknown, "Unexpected end of input."))
}
Err(AtomError::InvalidStartTag) => Err(JsError::new(
Status::Unknown,
format!("An error while converting bytes to UTF8.").to_string(),
"An error while converting bytes to UTF8.",
)),
Err(AtomError::WrongAttribute { attribute, value }) => Err(JsError::new(
Status::Unknown,
format!(
"The attribute '{}' had the wrong value '{}'",
attribute, value
)
.to_string(),
),
)),
Err(AtomError::WrongDatetime(value)) => Err(JsError::new(
Status::Unknown,
format!("The format of the datetime ('{}') was wrong.", value).to_string(),
format!("The format of the datetime ('{}') was wrong.", value),
)),
Err(AtomError::Xml(err)) => Err(JsError::new(
Status::Unknown,
format!("XML parsing error . {}'", err).to_string(),
format!("XML parsing error . {}'", err),
)),
Err(err) => Err(JsError::new(
Status::Unknown,
format!("Unknown error trying to parse feed parse feed '{}'", err).to_string(),
format!("Unknown error trying to parse feed parse feed '{}'", err),
)),
}
}
Err(RssError::Utf8(err)) => Err(JsError::new(
Status::Unknown,
format!("An error while converting bytes to UTF8. {}'", err).to_string(),
format!("An error while converting bytes to UTF8. {}'", err),
)),
Err(RssError::Xml(err)) => Err(JsError::new(
Status::Unknown,
format!("XML parsing error. {}", err).to_string(),
)),
Err(RssError::Eof) => Err(JsError::new(
Status::Unknown,
format!("Unexpected end of input").to_string(),
format!("XML parsing error. {}", err),
)),
Err(RssError::Eof) => Err(JsError::new(Status::Unknown, "Unexpected end of input")),
}
}

View File

@ -42,7 +42,7 @@ fn parse_rgb(input_color: String) -> Result<rgb::RGB8> {
_ => {
return Err(Error::new(
Status::InvalidArg,
format!("color '{}' is invalid", color).to_string(),
format!("color '{}' is invalid", color),
));
}
}
@ -53,15 +53,14 @@ fn parse_rgb(input_color: String) -> Result<rgb::RGB8> {
.map_err(|e| {
Error::new(
Status::InvalidArg,
format!("UTF8Error '{}' when converting rgb component", e).to_string(),
format!("UTF8Error '{}' when converting rgb component", e),
)
})
.and_then(|v| {
u8::from_str_radix(v, 16).map_err(|e| {
Error::new(
Status::InvalidArg,
format!("Integer parse error '{}' when converting rgb component", e)
.to_string(),
format!("Integer parse error '{}' when converting rgb component", e),
)
})
})?;
@ -80,8 +79,7 @@ fn parse_rgb(input_color: String) -> Result<rgb::RGB8> {
pub fn format_labels(array: Vec<IssueLabelDetail>) -> Result<MatrixMessageFormatResult> {
let mut plain = String::new();
let mut html = String::new();
let mut i = 0;
for label in array {
for (i, label) in array.into_iter().enumerate() {
if i != 0 {
plain.push_str(", ");
html.push_str(" ");
@ -90,31 +88,24 @@ pub fn format_labels(array: Vec<IssueLabelDetail>) -> Result<MatrixMessageFormat
// HTML
html.push_str("<span");
match label.color {
Some(color) => {
write!(html, " data-mx-bg-color=\"#{}\"", color).unwrap();
// Determine the constrast
let color_rgb = parse_rgb(color)?;
let contrast_color;
if contrast::contrast::<u8, f32>(color_rgb, RGB::new(0, 0, 0)) > 4.5 {
contrast_color = "#000000";
} else {
contrast_color = "#FFFFFF";
}
write!(html, " data-mx-color=\"{}\"", contrast_color).unwrap();
if let Some(color) = label.color {
write!(html, " data-mx-bg-color=\"#{}\"", color).unwrap();
// Determine the constrast
let color_rgb = parse_rgb(color)?;
let contrast_color;
if contrast::contrast::<u8, f32>(color_rgb, RGB::new(0, 0, 0)) > 4.5 {
contrast_color = "#000000";
} else {
contrast_color = "#FFFFFF";
}
None => {}
write!(html, " data-mx-color=\"{}\"", contrast_color).unwrap();
}
match label.description {
Some(description) => {
write!(html, " title=\"{}\"", description).unwrap();
}
None => {}
if let Some(description) = label.description {
write!(html, " title=\"{}\"", description).unwrap();
}
html.push_str(">");
html.push_str(&label.name);
html.push_str("</span>");
i += 1;
}
Ok(MatrixMessageFormatResult {