Address lints

- Remove unneeded box, add as_ref when passing client Box.
- Remove unnecessary borrows.
This commit is contained in:
Thomas Gideon 2024-10-06 10:43:32 -04:00
parent 927a0c3257
commit 4e1b227042
5 changed files with 494 additions and 629 deletions

1073
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
[package]
name = "kensho"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
default-run = "kensho"

View file

@ -12,7 +12,7 @@ use url::Url;
use url_open::UrlOpen;
pub(super) async fn format_status(
client: &Box<dyn Megalodon + Send + Sync>,
client: &(dyn Megalodon + Send + Sync),
depth: usize,
account: &Account,
status: &Status,

View file

@ -77,7 +77,7 @@ async fn main() -> Result<()> {
// for regular chronological ordering
let mut reversed = Vec::new();
loop {
let statuses = fetch_page(&client, &last_id_on_page).await?;
let statuses = fetch_page(client.as_ref(), &last_id_on_page).await?;
if statuses.is_empty() {
debug!("No more posts in range.");
break;
@ -86,8 +86,15 @@ async fn main() -> Result<()> {
trace!("Page bounds {:?}", page);
let (last_id, next_iter, mut formatted) =
process_page(&client, &account, &statuses, &last_id_on_page, &day, 1).await?;
let (last_id, next_iter, mut formatted) = process_page(
client.as_ref(),
&account,
&statuses,
&last_id_on_page,
&day,
1,
)
.await?;
reversed.append(&mut formatted);
if let Some(NextIter::Stop) = next_iter {
break;
@ -101,7 +108,7 @@ async fn main() -> Result<()> {
}
last_id_on_page = None;
loop {
let statuses = fetch_dm_page(&client, &account, &last_id_on_page).await?;
let statuses = fetch_dm_page(client.as_ref(), &account, &last_id_on_page).await?;
if statuses.is_empty() {
debug!("No more DMs in range.");
break;
@ -110,8 +117,15 @@ async fn main() -> Result<()> {
trace!("Page bounds {:?}", page);
let (last_id, next_iter, mut formatted) =
process_page(&client, &account, &statuses, &last_id_on_page, &day, 0).await?;
let (last_id, next_iter, mut formatted) = process_page(
client.as_ref(),
&account,
&statuses,
&last_id_on_page,
&day,
0,
)
.await?;
reversed.append(&mut formatted);
if let Some(NextIter::Stop) = next_iter {
break;
@ -235,14 +249,14 @@ enum NextIter {
}
async fn process_page(
client: &Box<dyn Megalodon + Send + Sync + 'static>,
client: &(dyn Megalodon + Send + Sync + 'static),
account: &Account,
statuses: &Vec<Status>,
statuses: &[Status],
last_id_on_page: &Option<String>,
day: &Range,
depth: usize,
) -> Result<(Option<String>, Option<NextIter>, Vec<String>)> {
let page = bounds_from(&statuses);
let page = bounds_from(statuses);
trace!("Page bounds {:?}", page);
@ -254,7 +268,7 @@ async fn process_page(
// fetching returns 20 at a time, in reverse chronological order so may require skipping
// pages after the requested date
if let Some(oldest_id) = page_newer_than(&page, &day) {
if let Some(oldest_id) = page_newer_than(&page, day) {
return Ok((Some(oldest_id), Some(NextIter::Skip), Vec::new()));
}
@ -262,13 +276,13 @@ async fn process_page(
// resolved values; a for in loop works with await but also runs into thorny ownership
// issues; a stream resolves both because the stream takes ownership of the statuses and
// can be iterated in a simple way that allows the use of await in the body
let mut stream = iter(filter_statuses(account, &day, &statuses));
let mut stream = iter(filter_statuses(account, day, statuses));
let mut formatted = Vec::new();
while let Some(status) = stream.next().await {
formatted.push(format_status(client, depth, &account, status).await?);
formatted.push(format_status(client, depth, account, status).await?);
}
if page_end_older_than(&page, &day) {
if page_end_older_than(&page, day) {
debug!("No more posts in range.");
return Ok((None, Some(NextIter::Stop), formatted));
}
@ -297,7 +311,7 @@ fn create_client(url: String, token: String) -> Result<Box<dyn Megalodon + Send
}
async fn fetch_page(
client: &Box<dyn Megalodon + Send + Sync>,
client: &(dyn Megalodon + Send + Sync),
last_id_on_page: &Option<String>,
) -> Result<Vec<Status>> {
trace!("Fetching page of local timeline");
@ -317,7 +331,7 @@ async fn fetch_page(
}
async fn fetch_dm_page(
client: &Box<dyn Megalodon + Send + Sync>,
client: &(dyn Megalodon + Send + Sync),
account: &Account,
last_id_on_page: &Option<String>,
) -> Result<Vec<Status>> {

View file

@ -8,7 +8,7 @@ pub(super) struct Page<'a> {
pub newest: Option<&'a DateTime<Utc>>,
}
pub(super) fn bounds_from<'a>(statuses: &'a Vec<Status>) -> Page<'a> {
pub(super) fn bounds_from(statuses: &[Status]) -> Page<'_> {
Page {
newest: statuses.first().map(|s| &s.created_at),
oldest_id: statuses.last().map(|s| s.id.clone()),