enable string_slice lint

Also swaps out vendored HTML-escaping code for a dependency that I
imagine has decent testing considering all of its reverse depedencies.
This commit is contained in:
Charles Hall 2024-05-12 19:08:12 -07:00
parent 9abe4799db
commit 844b32f097
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
7 changed files with 35 additions and 73 deletions

View file

@ -6,7 +6,7 @@ use rand::prelude::*;
use ring::digest;
use ruma::{canonical_json::try_from_json_map, CanonicalJsonError, CanonicalJsonObject};
use std::{
cmp, fmt,
cmp,
str::FromStr,
time::{SystemTime, UNIX_EPOCH},
};
@ -149,40 +149,3 @@ pub(crate) fn deserialize_from_str<
}
deserializer.deserialize_str(Visitor(std::marker::PhantomData))
}
// Copied from librustdoc:
// https://github.com/rust-lang/rust/blob/cbaeec14f90b59a91a6b0f17fc046c66fa811892/src/librustdoc/html/escape.rs
/// Wrapper struct which will emit the HTML-escaped version of the contained
/// string when passed to a format string.
pub(crate) struct HtmlEscape<'a>(pub(crate) &'a str);
impl fmt::Display for HtmlEscape<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
// Because the internet is always right, turns out there's not that many
// characters to escape: http://stackoverflow.com/questions/7381974
let HtmlEscape(s) = *self;
let pile_o_bits = s;
let mut last = 0;
for (i, ch) in s.char_indices() {
let s = match ch {
'>' => "&gt;",
'<' => "&lt;",
'&' => "&amp;",
'\'' => "&#39;",
'"' => "&quot;",
_ => continue,
};
fmt.write_str(&pile_o_bits[last..i])?;
fmt.write_str(s)?;
// NOTE: we only expect single byte characters here - which is fine as long as we
// only match single byte characters
last = i + 1;
}
if last < s.len() {
fmt.write_str(&pile_o_bits[last..])?;
}
Ok(())
}
}