Skip to content

Commit 71d511c

Browse files
committed
anyhow::ensure!を使うようにした
1 parent 34f3a49 commit 71d511c

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

atcoder-problems-backend/sql-client/src/internal/problem_list_manager.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::PgPool;
2-
use anyhow::{bail, Context, Result};
2+
use anyhow::{ensure, Context, Result};
33
use async_trait::async_trait;
44
use serde::Serialize;
55
use sqlx::Row;
@@ -152,9 +152,8 @@ impl ProblemListManager for PgPool {
152152
let new_list_id = uuid::Uuid::new_v4().to_string();
153153

154154
let list = self.get_list(internal_user_id).await?;
155-
if list.len() >= MAX_LIST_NUM {
156-
bail!("Cannot create a list anymore");
157-
}
155+
156+
ensure!(list.len() < MAX_LIST_NUM, "Cannot create a list anymore");
158157

159158
sqlx::query(
160159
r"
@@ -202,9 +201,11 @@ impl ProblemListManager for PgPool {
202201
.try_map(|row| row.try_get::<String, _>("problem_id"))
203202
.fetch_all(self)
204203
.await?;
205-
if problems.len() >= MAX_ITEM_NUM {
206-
bail!("Cannot create a list item anymore");
207-
}
204+
205+
ensure!(
206+
problems.len() < MAX_ITEM_NUM,
207+
"Cannot create a list item anymore"
208+
);
208209

209210
sqlx::query(
210211
r"

atcoder-problems-backend/sql-client/src/internal/virtual_contest_manager.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::PgPool;
2-
use anyhow::{bail, Context, Result};
2+
use anyhow::{ensure, Context, Result};
33
use async_trait::async_trait;
44
use serde::{Deserialize, Serialize};
55
use sqlx::Row;
@@ -332,9 +332,10 @@ impl VirtualContestManager for PgPool {
332332
problems: &[VirtualContestItem],
333333
user_id: &str,
334334
) -> Result<()> {
335-
if problems.len() > MAX_PROBLEM_NUM_PER_CONTEST {
336-
bail!("The number of problems exceeded.");
337-
}
335+
ensure!(
336+
problems.len() <= MAX_PROBLEM_NUM_PER_CONTEST,
337+
"The number of problems exceeded."
338+
);
338339

339340
// Checks if the target contest exists
340341
sqlx::query(

0 commit comments

Comments
 (0)