Skip to content

Commit 1a98b9d

Browse files
committed
fix or deal with all clippy lints
1 parent af27bb4 commit 1a98b9d

File tree

13 files changed

+116
-130
lines changed

13 files changed

+116
-130
lines changed

src/attachments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ fn cached_attachment_url(id: &str, dir: &AttachmentsPath) -> eyre::Result<Attach
160160
bail!("directory is empty: {path:?}");
161161
};
162162

163-
Ok(path.join_dir_entry(&entry?)?)
163+
path.join_dir_entry(&entry?)
164164
}
165165

166166
fn cache_imported_attachment(url: &str, path: &AttachmentsPath) -> eyre::Result<AttachmentsPath> {
167167
// if the attachment id directory exists...
168-
if let Ok(mut entries) = read_dir(&path) {
168+
if let Ok(mut entries) = read_dir(path) {
169169
// and the directory contains a file...
170170
if let Some(entry) = entries.next() {
171171
// and we can open the file...

src/command/attach.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ pub async fn main() -> eyre::Result<()> {
2525
create_dir_all(&*ATTACHMENTS_PATH_ROOT)?;
2626

2727
for path in args.paths {
28-
let attachment_path = RealAttachmentsContext.store(&Path::new(&path))?;
28+
let attachment_path = RealAttachmentsContext.store(Path::new(&path))?;
2929
info!(
3030
"created attachment: <{}>",
3131
attachment_path.site_path()?.base_relative_url()

src/command/cohost2autost.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn convert_chost(entry: &DirEntry, context: &dyn AttachmentsContext) -> eyre::Re
115115
}
116116

117117
for (shared_post, output_path) in shared_posts.into_iter().zip(shared_post_filenames.iter()) {
118-
convert_single_chost(shared_post, vec![], &output_path, context)?;
118+
convert_single_chost(shared_post, vec![], output_path, context)?;
119119
}
120120

121121
let output_path = PostsPath::generated_post_path(post_id);
@@ -173,7 +173,7 @@ fn convert_single_chost(
173173
// posts in the cohost api provide an `astMap` that contains the perfect rendering of
174174
// markdown blocks. since our own markdown rendering is far from perfect, we use their
175175
// rendering instead of our own when available.
176-
while spans.front().map_or(false, |(_ast, _start, end)| i >= *end) {
176+
while spans.front().is_some_and(|(_ast, _start, end)| i >= *end) {
177177
spans.pop_front();
178178
}
179179
if let Some((ast, start, end)) = match spans.front() {
@@ -504,7 +504,7 @@ fn process_chost_fragment(
504504
Ok(())
505505
})? {}
506506

507-
Ok(serialize_html_fragment(dom)?)
507+
serialize_html_fragment(dom)
508508
}
509509

510510
#[test]
@@ -530,21 +530,21 @@ fn test_render_markdown_block() -> eyre::Result<()> {
530530
cacheable: &Cacheable,
531531
) -> eyre::Result<CachedFileResult<AttachmentsPath>> {
532532
Ok(CachedFileResult::CachedPath(match cacheable {
533-
Cacheable::Attachment { id, .. } => ATTACHMENTS_PATH_ROOT.join(&format!("{id}"))?,
533+
Cacheable::Attachment { id, .. } => ATTACHMENTS_PATH_ROOT.join(id)?,
534534
Cacheable::Static { filename, .. } => {
535-
ATTACHMENTS_PATH_COHOST_STATIC.join(&format!("{filename}"))?
535+
ATTACHMENTS_PATH_COHOST_STATIC.join(filename)?
536536
}
537537
Cacheable::Avatar { filename, .. } => {
538-
ATTACHMENTS_PATH_COHOST_AVATAR.join(&format!("{filename}"))?
538+
ATTACHMENTS_PATH_COHOST_AVATAR.join(filename)?
539539
}
540540
Cacheable::Header { filename, .. } => {
541-
ATTACHMENTS_PATH_COHOST_HEADER.join(&format!("{filename}"))?
541+
ATTACHMENTS_PATH_COHOST_HEADER.join(filename)?
542542
}
543543
}))
544544
}
545545
fn cache_cohost_thumb(&self, id: &str) -> eyre::Result<CachedFileResult<AttachmentsPath>> {
546546
Ok(CachedFileResult::CachedPath(
547-
ATTACHMENTS_PATH_THUMBS.join(&format!("{id}"))?,
547+
ATTACHMENTS_PATH_THUMBS.join(id)?,
548548
))
549549
}
550550
}

src/command/import.rs

Lines changed: 62 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn fetch_h_entry_post(document: Handle, url: &str) -> eyre::Result<Option<FetchP
134134
};
135135
info!("found h-entry post");
136136

137-
let mut base_href = Url::parse(&url)?;
137+
let mut base_href = Url::parse(url)?;
138138
for node in BreadthTraverse::elements(document) {
139139
let NodeData::Element { name, attrs, .. } = &node.data else {
140140
unreachable!()
@@ -191,10 +191,8 @@ fn fetch_h_entry_post(document: Handle, url: &str) -> eyre::Result<Option<FetchP
191191
while let Some(weak) = node.parent.take() {
192192
let parent = weak.upgrade().expect("dangling weak pointer");
193193
node.parent.set(Some(weak));
194-
if has_class(parent.clone(), "h-entry")? {
195-
if !Rc::ptr_eq(&parent, &h_entry) {
196-
continue 'category;
197-
}
194+
if has_class(parent.clone(), "h-entry")? && !Rc::ptr_eq(&parent, &h_entry) {
195+
continue 'category;
198196
}
199197
node = parent;
200198
}
@@ -239,10 +237,10 @@ async fn fetch_akkoma_post(
239237
let NodeData::Element { name, attrs, .. } = &node.data else {
240238
unreachable!()
241239
};
242-
if name == &QualName::html("script") {
243-
if attrs.borrow().attr_str("id")? == Some("initial-results") {
244-
return Ok(Some(serde_json::from_str(&text_content(node)?)?));
245-
}
240+
if name == &QualName::html("script")
241+
&& attrs.borrow().attr_str("id")? == Some("initial-results")
242+
{
243+
return Ok(Some(serde_json::from_str(&text_content(node)?)?));
246244
}
247245
}
248246
Ok(None)
@@ -262,7 +260,7 @@ async fn fetch_akkoma_post(
262260
let status_id = fetched_page_url
263261
.path_segments()
264262
.ok_or_eyre("bad page url")?
265-
.last()
263+
.next_back()
266264
.ok_or_eyre("page url has no last path segment")?;
267265
trace!(?status_id);
268266
let api_url = instance_url.join(&format!("api/v1/statuses/{status_id}"))?;
@@ -304,7 +302,7 @@ async fn fetch_akkoma_post(
304302

305303
Ok(Some(FetchPostResult {
306304
base_href: url.clone(),
307-
content: content,
305+
content,
308306
url,
309307
meta,
310308
}))
@@ -349,75 +347,66 @@ fn process_content(
349347
let dom = parse_html_fragment(content.as_bytes())?;
350348

351349
for node in BreadthTraverse::nodes(dom.document.clone()) {
352-
match &node.data {
353-
NodeData::Element { name, attrs, .. } => {
354-
let mut attrs = attrs.borrow_mut();
355-
let mut extra_attrs = vec![];
356-
if let Some(attr_names) = html_attributes_with_embedding_urls().get(name) {
357-
for attr in attrs.iter_mut() {
358-
if attr_names.contains(&attr.name) {
359-
// rewrite attachment urls to relative cached paths.
360-
let old_url = attr.value.to_str().to_owned();
361-
let fetch_url = base_href.join(&old_url)?;
362-
trace!(
363-
"found attachment url in <{} {}>: {old_url}",
364-
name.local,
365-
attr.name.local
366-
);
367-
attr.value = context
368-
.cache_imported(&fetch_url.to_string(), post_basename)?
369-
.site_path()?
370-
.base_relative_url()
371-
.into();
372-
extra_attrs.push(Attribute {
373-
name: QualName::attribute(&format!(
374-
"data-import-{}",
375-
attr.name.local
376-
)),
377-
value: old_url.into(),
378-
});
379-
}
350+
if let NodeData::Element { name, attrs, .. } = &node.data {
351+
let mut attrs = attrs.borrow_mut();
352+
let mut extra_attrs = vec![];
353+
if let Some(attr_names) = html_attributes_with_embedding_urls().get(name) {
354+
for attr in attrs.iter_mut() {
355+
if attr_names.contains(&attr.name) {
356+
// rewrite attachment urls to relative cached paths.
357+
let old_url = attr.value.to_str().to_owned();
358+
let fetch_url = base_href.join(&old_url)?;
359+
trace!(
360+
"found attachment url in <{} {}>: {old_url}",
361+
name.local,
362+
attr.name.local
363+
);
364+
attr.value = context
365+
.cache_imported(fetch_url.as_ref(), post_basename)?
366+
.site_path()?
367+
.base_relative_url()
368+
.into();
369+
extra_attrs.push(Attribute {
370+
name: QualName::attribute(&format!("data-import-{}", attr.name.local)),
371+
value: old_url.into(),
372+
});
380373
}
381374
}
382-
if let Some(attr_names) = html_attributes_with_non_embedding_urls().get(name) {
383-
for attr in attrs.iter_mut() {
384-
if attr_names.contains(&attr.name) {
385-
// rewrite urls in links to bake in the `base_href`.
386-
let old_url = attr.value.to_str().to_owned();
387-
let new_url = if old_url.starts_with("#") {
388-
format!("#user-content-{}", &old_url[1..])
389-
} else {
390-
base_href.join(&old_url)?.to_string()
391-
};
392-
trace!(
393-
"rewriting <{} {}>: {old_url:?} -> {new_url:?}",
394-
name.local,
395-
attr.name.local,
396-
);
397-
attr.value = new_url.to_string().into();
398-
extra_attrs.push(Attribute {
399-
name: QualName::attribute(&format!(
400-
"data-import-{}",
401-
attr.name.local
402-
)),
403-
value: old_url.into(),
404-
});
405-
}
375+
}
376+
if let Some(attr_names) = html_attributes_with_non_embedding_urls().get(name) {
377+
for attr in attrs.iter_mut() {
378+
if attr_names.contains(&attr.name) {
379+
// rewrite urls in links to bake in the `base_href`.
380+
let old_url = attr.value.to_str().to_owned();
381+
let new_url = if let Some(fragment) = old_url.strip_prefix("#") {
382+
format!("#user-content-{fragment}")
383+
} else {
384+
base_href.join(&old_url)?.to_string()
385+
};
386+
trace!(
387+
"rewriting <{} {}>: {old_url:?} -> {new_url:?}",
388+
name.local,
389+
attr.name.local,
390+
);
391+
attr.value = new_url.to_string().into();
392+
extra_attrs.push(Attribute {
393+
name: QualName::attribute(&format!("data-import-{}", attr.name.local)),
394+
value: old_url.into(),
395+
});
406396
}
407397
}
408-
if name == &QualName::html("img") {
409-
extra_attrs.push(Attribute {
410-
name: QualName::attribute("loading"),
411-
value: "lazy".into(),
412-
});
413-
}
414-
attrs.extend(extra_attrs);
415398
}
416-
_ => {}
399+
if name == &QualName::html("img") {
400+
extra_attrs.push(Attribute {
401+
name: QualName::attribute("loading"),
402+
value: "lazy".into(),
403+
});
404+
}
405+
attrs.extend(extra_attrs);
417406
}
418407
}
419408

420-
Ok(serialize_html_fragment(dom)?)
409+
serialize_html_fragment(dom)
421410
}
422411

423412
fn mf2_e(node: Handle, class: &str) -> eyre::Result<Option<String>> {
@@ -519,7 +508,7 @@ fn mf2_find_all(node: Handle, class: &str) -> Vec<Handle> {
519508
fn has_class(node: Handle, class: &str) -> eyre::Result<bool> {
520509
if let NodeData::Element { attrs, .. } = &node.data {
521510
if let Some(node_class) = attrs.borrow().attr_str("class")? {
522-
if node_class.split(" ").find(|&c| c == class).is_some() {
511+
if node_class.split(" ").any(|c| c == class) {
523512
return Ok(true);
524513
}
525514
}

src/command/new.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn main(args: New) -> eyre::Result<()> {
1818
info!("creating new site in {path:?}");
1919

2020
create_dir_all(path)?;
21-
for entry in read_dir(path)? {
21+
if let Some(entry) = (read_dir(path)?).next() {
2222
bail!("directory is not empty: {:?}", entry?.path());
2323
}
2424
let mut settings = File::create_new(path.join("autost.toml"))?;

src/command/render.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn render_all() -> eyre::Result<()> {
5454
render(post_paths)
5555
}
5656

57-
pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
57+
pub fn render(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
5858
run_migrations()?;
5959

6060
let now = Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true);
@@ -97,7 +97,7 @@ pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
9797
),
9898
];
9999
for file in static_files.iter() {
100-
copy_static(&*SITE_PATH_ROOT, file)?;
100+
copy_static(&SITE_PATH_ROOT, file)?;
101101
}
102102
#[cfg(unix)]
103103
{
@@ -163,7 +163,7 @@ pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
163163
&format!("{} — {tag}", SETTINGS.site_title),
164164
&now,
165165
)?;
166-
writeln!(File::create(&atom_feed_path)?, "{}", atom_feed,)?;
166+
writeln!(File::create(&atom_feed_path)?, "{atom_feed}",)?;
167167
interesting_output_paths.insert(atom_feed_path);
168168
let threads_content = render_cached_threads_content(&threads_cache, &threads);
169169
let threads_page = ThreadsPageTemplate::render(
@@ -173,7 +173,7 @@ pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
173173
)?;
174174
// TODO: move this logic into path module and check for slashes
175175
let threads_page_path = SITE_PATH_TAGGED.join(&format!("{tag}.html"))?;
176-
writeln!(File::create(&threads_page_path)?, "{}", threads_page)?;
176+
writeln!(File::create(&threads_page_path)?, "{threads_page}")?;
177177
interesting_output_paths.insert(threads_page_path);
178178
}
179179

@@ -294,7 +294,7 @@ fn render_single_post(path: PostsPath) -> eyre::Result<CacheableRenderResult> {
294294
&SETTINGS.page_title(thread.meta.title.as_deref()),
295295
&None,
296296
)?;
297-
writeln!(File::create(rendered_path)?, "{}", threads_page)?;
297+
writeln!(File::create(rendered_path)?, "{threads_page}")?;
298298

299299
let result = CacheableRenderResult {
300300
render_result: result,
@@ -406,7 +406,7 @@ impl Collections {
406406
}
407407

408408
fn keys(&self) -> impl Iterator<Item = &str> {
409-
self.inner.keys().map(|key| *key)
409+
self.inner.keys().copied()
410410
}
411411

412412
fn len(&self, key: &str) -> usize {

src/css.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn serialise_inline_style(tokens: &[InlineStyleToken]) -> String {
2929
.join("")
3030
}
3131

32-
fn parse<'i>(parser: &'i mut Parser) -> Vec<InlineStyleToken> {
32+
fn parse(parser: &mut Parser) -> Vec<InlineStyleToken> {
3333
let mut result = vec![];
3434
loop {
3535
let token = match parser.next_including_whitespace_and_comments() {

src/dom.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,8 @@ pub trait AttrsMutExt: AttrsRefExt {
269269
}
270270
impl AttrsMutExt for Vec<Attribute> {
271271
fn attr_mut(&mut self, name: &str) -> Option<&mut Attribute> {
272-
for attr in self.iter_mut() {
273-
if attr.name == QualName::attribute(name) {
274-
return Some(attr);
275-
}
276-
}
277-
278-
None
272+
self.iter_mut()
273+
.find(|attr| attr.name == QualName::attribute(name))
279274
}
280275
}
281276
impl AttrsRefExt for Vec<Attribute> {

0 commit comments

Comments
 (0)