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 36471da6a9
4 changed files with 492 additions and 627 deletions

1071
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

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

View file

@ -77,7 +77,7 @@ async fn main() -> Result<()> {
// for regular chronological ordering // for regular chronological ordering
let mut reversed = Vec::new(); let mut reversed = Vec::new();
loop { 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() { if statuses.is_empty() {
debug!("No more posts in range."); debug!("No more posts in range.");
break; break;
@ -86,8 +86,15 @@ async fn main() -> Result<()> {
trace!("Page bounds {:?}", page); trace!("Page bounds {:?}", page);
let (last_id, next_iter, mut formatted) = let (last_id, next_iter, mut formatted) = process_page(
process_page(&client, &account, &statuses, &last_id_on_page, &day, 1).await?; client.as_ref(),
&account,
&statuses,
&last_id_on_page,
&day,
1,
)
.await?;
reversed.append(&mut formatted); reversed.append(&mut formatted);
if let Some(NextIter::Stop) = next_iter { if let Some(NextIter::Stop) = next_iter {
break; break;
@ -101,7 +108,7 @@ async fn main() -> Result<()> {
} }
last_id_on_page = None; last_id_on_page = None;
loop { 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() { if statuses.is_empty() {
debug!("No more DMs in range."); debug!("No more DMs in range.");
break; break;
@ -110,8 +117,15 @@ async fn main() -> Result<()> {
trace!("Page bounds {:?}", page); trace!("Page bounds {:?}", page);
let (last_id, next_iter, mut formatted) = let (last_id, next_iter, mut formatted) = process_page(
process_page(&client, &account, &statuses, &last_id_on_page, &day, 0).await?; client.as_ref(),
&account,
&statuses,
&last_id_on_page,
&day,
0,
)
.await?;
reversed.append(&mut formatted); reversed.append(&mut formatted);
if let Some(NextIter::Stop) = next_iter { if let Some(NextIter::Stop) = next_iter {
break; break;
@ -235,14 +249,14 @@ enum NextIter {
} }
async fn process_page( async fn process_page(
client: &Box<dyn Megalodon + Send + Sync + 'static>, client: &(dyn Megalodon + Send + Sync + 'static),
account: &Account, account: &Account,
statuses: &Vec<Status>, statuses: &[Status],
last_id_on_page: &Option<String>, last_id_on_page: &Option<String>,
day: &Range, day: &Range,
depth: usize, depth: usize,
) -> Result<(Option<String>, Option<NextIter>, Vec<String>)> { ) -> Result<(Option<String>, Option<NextIter>, Vec<String>)> {
let page = bounds_from(&statuses); let page = bounds_from(statuses);
trace!("Page bounds {:?}", page); 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 // fetching returns 20 at a time, in reverse chronological order so may require skipping
// pages after the requested date // 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())); 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 // 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 // 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 // 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(); let mut formatted = Vec::new();
while let Some(status) = stream.next().await { 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."); debug!("No more posts in range.");
return Ok((None, Some(NextIter::Stop), formatted)); 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( async fn fetch_page(
client: &Box<dyn Megalodon + Send + Sync>, client: &(dyn Megalodon + Send + Sync),
last_id_on_page: &Option<String>, last_id_on_page: &Option<String>,
) -> Result<Vec<Status>> { ) -> Result<Vec<Status>> {
trace!("Fetching page of local timeline"); trace!("Fetching page of local timeline");
@ -317,7 +331,7 @@ async fn fetch_page(
} }
async fn fetch_dm_page( async fn fetch_dm_page(
client: &Box<dyn Megalodon + Send + Sync>, client: &(dyn Megalodon + Send + Sync),
account: &Account, account: &Account,
last_id_on_page: &Option<String>, last_id_on_page: &Option<String>,
) -> Result<Vec<Status>> { ) -> Result<Vec<Status>> {

View file

@ -8,7 +8,7 @@ pub(super) struct Page<'a> {
pub newest: Option<&'a DateTime<Utc>>, 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 { Page {
newest: statuses.first().map(|s| &s.created_at), newest: statuses.first().map(|s| &s.created_at),
oldest_id: statuses.last().map(|s| s.id.clone()), oldest_id: statuses.last().map(|s| s.id.clone()),