Skip to content

Commit c5cd297

Browse files
authored
Merge pull request #1771 from WaffleLapkin/usizeless
Don't use `usize` for ids and such
2 parents b0bbe70 + 60e608e commit c5cd297

File tree

7 files changed

+64
-78
lines changed

7 files changed

+64
-78
lines changed

Cargo.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/db/notifications.rs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use tokio_postgres::Client as DbClient;
44
use tracing as log;
55

66
pub struct Notification {
7-
pub user_id: i64,
7+
pub user_id: u64,
88
pub origin_url: String,
99
pub origin_html: String,
1010
pub short_description: Option<String>,
@@ -15,10 +15,10 @@ pub struct Notification {
1515
pub team_name: Option<String>,
1616
}
1717

18-
pub async fn record_username(db: &DbClient, user_id: i64, username: String) -> anyhow::Result<()> {
18+
pub async fn record_username(db: &DbClient, user_id: u64, username: String) -> anyhow::Result<()> {
1919
db.execute(
2020
"INSERT INTO users (user_id, username) VALUES ($1, $2) ON CONFLICT DO NOTHING",
21-
&[&user_id, &username],
21+
&[&(user_id as i64), &username],
2222
)
2323
.await
2424
.context("inserting user id / username")?;
@@ -31,7 +31,7 @@ pub async fn record_ping(db: &DbClient, notification: &Notification) -> anyhow::
3131
$1, $2, $3, $4, $5, $6,
3232
(SELECT max(notifications.idx) + 1 from notifications where notifications.user_id = $1)
3333
)",
34-
&[&notification.user_id, &notification.origin_url, &notification.origin_html, &notification.time, &notification.short_description, &notification.team_name],
34+
&[&(notification.user_id as i64), &notification.origin_url, &notification.origin_html, &notification.time, &notification.short_description, &notification.team_name],
3535
).await.context("inserting notification")?;
3636

3737
Ok(())
@@ -40,14 +40,14 @@ pub async fn record_ping(db: &DbClient, notification: &Notification) -> anyhow::
4040
#[derive(Copy, Clone)]
4141
pub enum Identifier<'a> {
4242
Url(&'a str),
43-
Index(std::num::NonZeroUsize),
43+
Index(std::num::NonZeroU32),
4444
/// Glob identifier (`all` or `*`).
4545
All,
4646
}
4747

4848
pub async fn delete_ping(
4949
db: &mut DbClient,
50-
user_id: i64,
50+
user_id: u64,
5151
identifier: Identifier<'_>,
5252
) -> anyhow::Result<Vec<NotificationData>> {
5353
match identifier {
@@ -56,7 +56,7 @@ pub async fn delete_ping(
5656
.query(
5757
"DELETE FROM notifications WHERE user_id = $1 and origin_url = $2
5858
RETURNING origin_html, time, short_description, metadata",
59-
&[&user_id, &origin_url],
59+
&[&(user_id as i64), &origin_url],
6060
)
6161
.await
6262
.context("delete notification query")?;
@@ -91,13 +91,13 @@ pub async fn delete_ping(
9191
from notifications
9292
where user_id = $1
9393
order by idx asc nulls last;",
94-
&[&user_id],
94+
&[&(user_id as i64)],
9595
)
9696
.await
9797
.context("failed to get ordering")?;
9898

9999
let notification_id: i64 = notifications
100-
.get(idx.get() - 1)
100+
.get((idx.get() - 1) as usize)
101101
.ok_or_else(|| anyhow::anyhow!("No such notification with index {}", idx.get()))?
102102
.get(0);
103103

@@ -144,7 +144,7 @@ pub async fn delete_ping(
144144
.query(
145145
"DELETE FROM notifications WHERE user_id = $1
146146
RETURNING origin_url, origin_html, time, short_description, metadata",
147-
&[&user_id],
147+
&[&(user_id as i64)],
148148
)
149149
.await
150150
.context("delete all notifications query")?;
@@ -180,10 +180,12 @@ pub struct NotificationData {
180180

181181
pub async fn move_indices(
182182
db: &mut DbClient,
183-
user_id: i64,
184-
from: usize,
185-
to: usize,
183+
user_id: u64,
184+
from: u32,
185+
to: u32,
186186
) -> anyhow::Result<()> {
187+
let from = usize::try_from(from)?;
188+
let to = usize::try_from(to)?;
187189
loop {
188190
let t = db
189191
.build_transaction()
@@ -198,7 +200,7 @@ pub async fn move_indices(
198200
from notifications
199201
where user_id = $1
200202
order by idx asc nulls last;",
201-
&[&user_id],
203+
&[&(user_id as i64)],
202204
)
203205
.await
204206
.context("failed to get initial ordering")?;
@@ -257,10 +259,11 @@ pub async fn move_indices(
257259

258260
pub async fn add_metadata(
259261
db: &mut DbClient,
260-
user_id: i64,
261-
idx: usize,
262+
user_id: u64,
263+
idx: u32,
262264
metadata: Option<&str>,
263265
) -> anyhow::Result<()> {
266+
let idx = usize::try_from(idx)?;
264267
loop {
265268
let t = db
266269
.build_transaction()
@@ -275,7 +278,7 @@ pub async fn add_metadata(
275278
from notifications
276279
where user_id = $1
277280
order by idx asc nulls last;",
278-
&[&user_id],
281+
&[&(user_id as i64)],
279282
)
280283
.await
281284
.context("failed to get initial ordering")?;

src/github.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ use tracing as log;
1919
#[derive(Debug, PartialEq, Eq, serde::Deserialize)]
2020
pub struct User {
2121
pub login: String,
22-
pub id: Option<i64>,
22+
pub id: Option<u64>,
2323
}
2424

2525
impl GithubClient {
2626
async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<(Bytes, String)> {
27-
const MAX_ATTEMPTS: usize = 2;
27+
const MAX_ATTEMPTS: u32 = 2;
2828
log::debug!("send_req with {:?}", req);
2929
let req_dbg = format!("{:?}", req);
3030
let req = req
@@ -80,7 +80,7 @@ impl GithubClient {
8080
&self,
8181
req: Request,
8282
sleep: Duration,
83-
remaining_attempts: usize,
83+
remaining_attempts: u32,
8484
) -> BoxFuture<Result<Response, reqwest::Error>> {
8585
#[derive(Debug, serde::Deserialize)]
8686
struct RateLimit {
@@ -203,7 +203,7 @@ impl User {
203203
}
204204

205205
// Returns the ID of the given user, if the user is in the `all` team.
206-
pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
206+
pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<u64>> {
207207
let permission = crate::team_data::teams(client).await?;
208208
let map = permission.teams;
209209
Ok(map["all"]
@@ -504,7 +504,7 @@ impl Issue {
504504
self.state == IssueState::Open
505505
}
506506

507-
pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
507+
pub async fn get_comment(&self, client: &GithubClient, id: u64) -> anyhow::Result<Comment> {
508508
let comment_url = format!("{}/issues/comments/{}", self.repository().url(client), id);
509509
let comment = client.json(client.get(&comment_url)).await?;
510510
Ok(comment)
@@ -540,7 +540,7 @@ impl Issue {
540540
pub async fn edit_comment(
541541
&self,
542542
client: &GithubClient,
543-
id: usize,
543+
id: u64,
544544
new_body: &str,
545545
) -> anyhow::Result<()> {
546546
let comment_url = format!("{}/issues/comments/{}", self.repository().url(client), id);
@@ -1030,7 +1030,7 @@ pub fn parse_diff(diff: &str) -> Vec<FileDiff> {
10301030

10311031
#[derive(Debug, serde::Deserialize)]
10321032
pub struct IssueSearchResult {
1033-
pub total_count: usize,
1033+
pub total_count: u64,
10341034
pub incomplete_results: bool,
10351035
pub items: Vec<Issue>,
10361036
}
@@ -1049,7 +1049,7 @@ struct Ordering<'a> {
10491049
pub sort: &'a str,
10501050
pub direction: &'a str,
10511051
pub per_page: &'a str,
1052-
pub page: usize,
1052+
pub page: u64,
10531053
}
10541054

10551055
impl Repository {
@@ -1136,7 +1136,7 @@ impl Repository {
11361136
.await
11371137
.with_context(|| format!("failed to list issues from {}", url))?;
11381138
issues.extend(result.items);
1139-
if issues.len() < result.total_count {
1139+
if (issues.len() as u64) < result.total_count {
11401140
ordering.page += 1;
11411141
continue;
11421142
}

src/handlers/assign/tests/tests_candidates.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ fn convert_simplified(
5353
"members": serde_json::Value::Array(members),
5454
"alumni": [],
5555
"discord": [],
56+
"roles": [],
5657
});
5758
}
5859
let teams = serde_json::value::from_value(teams_config).unwrap();

src/handlers/notification.rs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
};
1212
use anyhow::Context as _;
1313
use std::collections::HashSet;
14-
use std::convert::{TryFrom, TryInto};
14+
use std::convert::TryInto;
1515
use tracing as log;
1616

1717
pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
@@ -58,7 +58,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
5858
let mut users_notified = HashSet::new();
5959
if let Some(from) = event.comment_from() {
6060
for login in parser::get_mentions(from).into_iter() {
61-
if let Some((Ok(users), _)) = id_from_user(ctx, login).await? {
61+
if let Some((users, _)) = id_from_user(ctx, login).await? {
6262
users_notified.extend(users.into_iter().map(|user| user.id.unwrap()));
6363
}
6464
}
@@ -85,14 +85,6 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
8585
None => continue,
8686
};
8787

88-
let users = match users {
89-
Ok(users) => users,
90-
Err(err) => {
91-
log::error!("getting users failed: {:?}", err);
92-
continue;
93-
}
94-
};
95-
9688
let client = ctx.db.get().await;
9789
for user in users {
9890
if !users_notified.insert(user.id.unwrap()) {
@@ -132,7 +124,7 @@ pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
132124
async fn id_from_user(
133125
ctx: &Context,
134126
login: &str,
135-
) -> anyhow::Result<Option<(anyhow::Result<Vec<github::User>>, Option<String>)>> {
127+
) -> anyhow::Result<Option<(Vec<github::User>, Option<String>)>> {
136128
if login.contains('/') {
137129
// This is a team ping. For now, just add it to everyone's agenda on
138130
// that team, but also mark it as such (i.e., a team ping) for
@@ -174,15 +166,11 @@ async fn id_from_user(
174166
Ok(Some((
175167
team.members
176168
.into_iter()
177-
.map(|member| {
178-
let id = i64::try_from(member.github_id)
179-
.with_context(|| format!("user id {} out of bounds", member.github_id))?;
180-
Ok(github::User {
181-
id: Some(id),
182-
login: member.github,
183-
})
169+
.map(|member| github::User {
170+
id: Some(member.github_id),
171+
login: member.github,
184172
})
185-
.collect::<anyhow::Result<Vec<github::User>>>(),
173+
.collect::<Vec<github::User>>(),
186174
Some(team.name),
187175
)))
188176
} else {
@@ -194,22 +182,16 @@ async fn id_from_user(
194182
.get_id(&ctx.github)
195183
.await
196184
.with_context(|| format!("failed to get user {} ID", user.login))?;
197-
let id = match id {
198-
Some(id) => id,
185+
let Some(id) = id else {
199186
// If the user was not in the team(s) then just don't record it.
200-
None => {
201-
log::trace!("Skipping {} because no id found", user.login);
202-
return Ok(None);
203-
}
187+
log::trace!("Skipping {} because no id found", user.login);
188+
return Ok(None);
204189
};
205-
let id = i64::try_from(id).with_context(|| format!("user id {} out of bounds", id));
206190
Ok(Some((
207-
id.map(|id| {
208-
vec![github::User {
209-
login: user.login.clone(),
210-
id: Some(id),
211-
}]
212-
}),
191+
vec![github::User {
192+
login: user.login.clone(),
193+
id: Some(id),
194+
}],
213195
None,
214196
)))
215197
}

src/handlers/rustc_commits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::VecDeque;
1010
use std::convert::TryInto;
1111
use tracing as log;
1212

13-
const BORS_GH_ID: i64 = 3372342;
13+
const BORS_GH_ID: u64 = 3372342;
1414

1515
pub async fn handle(ctx: &Context, event: &Event) -> anyhow::Result<()> {
1616
let body = match event.comment_body() {

0 commit comments

Comments
 (0)