Turn RankedArticleRow into a struct instead of a 7-field tuple
All checks were successful
CI / check (pull_request) Successful in 1m34s
CI / test (pull_request) Successful in 3m19s
CI / audit (pull_request) Successful in 12s

Tuples this large stop being readable at the call site (positional
indices like rows[0].6 give no hint what they mean); a named struct
documents each field and lets rustc catch reordering mistakes.
This commit is contained in:
Austin Schaefer 2026-09-03 21:20:41 +02:00
parent 33d2dfe011
commit 73c80491be
2 changed files with 30 additions and 35 deletions

View file

@ -7,17 +7,16 @@ use diesel_async::RunQueryDsl;
use feedsignal_core::Article;
use uuid::Uuid;
/// `(id, feed_id, title, url, summary, topics, final_score)`, as returned
/// by [`Db::list_ranked_articles`].
type RankedArticleRow = (
String,
String,
String,
String,
String,
Vec<String>,
Option<f32>,
);
/// One row of [`Db::list_ranked_articles`].
pub struct RankedArticleRow {
pub id: String,
pub feed_id: String,
pub title: String,
pub url: String,
pub summary: String,
pub topics: Vec<String>,
pub final_score: Option<f32>,
}
impl Db {
pub async fn insert_article(&self, article: &Article) -> Result<()> {
@ -137,16 +136,14 @@ impl Db {
Ok(rows
.into_iter()
.map(
|(id, feed_id, title, url, summary, topics_json, final_score)| {
(
|(id, feed_id, title, url, summary, topics_json, final_score)| RankedArticleRow {
id,
feed_id,
title,
url,
summary,
serde_json::from_str(&topics_json).unwrap_or_default(),
topics: serde_json::from_str(&topics_json).unwrap_or_default(),
final_score,
)
},
)
.collect())
@ -246,7 +243,7 @@ mod tests {
let rows = db.list_ranked_articles(Some(feed_a), 10).await.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].1, feed_a.to_string());
assert_eq!(rows[0].feed_id, feed_a.to_string());
}
/// Results stay ordered by `final_score` descending regardless of
@ -263,7 +260,7 @@ mod tests {
let rows = db.list_ranked_articles(None, 10).await.unwrap();
assert_eq!(rows[0].6, Some(0.8));
assert_eq!(rows[1].6, Some(0.2));
assert_eq!(rows[0].final_score, Some(0.8));
assert_eq!(rows[1].final_score, Some(0.2));
}
}

View file

@ -12,16 +12,14 @@ pub async fn list_ranked(db: Db, feed_id: Option<String>) -> Result<Vec<ArticleV
.list_ranked_articles(feed_id, 100)
.await?
.into_iter()
.map(
|(id, feed_id, title, url, summary, topics, final_score)| ArticleView {
id,
feed_id,
title,
url,
summary,
topics,
final_score,
},
)
.map(|row| ArticleView {
id: row.id,
feed_id: row.feed_id,
title: row.title,
url: row.url,
summary: row.summary,
topics: row.topics,
final_score: row.final_score,
})
.collect())
}