RowNotFound -> DialogueNotFound

This commit is contained in:
Temirkhan Myrzamadi 2021-05-08 19:00:38 +06:00
parent d14e0a49e9
commit 27eda5759c
6 changed files with 24 additions and 20 deletions

View file

@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Storage::get_dialogue` to obtain a dialogue indexed by a chat ID. - `Storage::get_dialogue` to obtain a dialogue indexed by a chat ID.
- `RedisStorageError::RowNotFound` to be returned from `RedisStorage::remove_dialogue`. - `RedisStorageError::RowNotFound` to be returned from `RedisStorage::remove_dialogue`.
- `InMemStorageError` with a single variant `RowNotFound` to be returned from `InMemStorage::remove_dialogue`. - `InMemStorageError` with a single variant `DialogueNotFound` to be returned from `InMemStorage::remove_dialogue`.
- `RedisStorageError::DialogueNotFound` and `SqliteStorageError::DialogueNotFound` to be returned from `Storage::remove_dialogue`.
### Changed ### Changed

View file

@ -9,7 +9,7 @@ use tokio::sync::Mutex;
pub enum InMemStorageError { pub enum InMemStorageError {
/// Returned from [`InMemStorage::remove_dialogue`]. /// Returned from [`InMemStorage::remove_dialogue`].
#[error("row not found")] #[error("row not found")]
RowNotFound, DialogueNotFound,
} }
/// A dialogue storage based on [`std::collections::HashMap`]. /// A dialogue storage based on [`std::collections::HashMap`].
@ -46,7 +46,7 @@ where
.lock() .lock()
.await .await
.remove(&chat_id) .remove(&chat_id)
.map_or_else(|| Err(InMemStorageError::RowNotFound), |_| Ok(())) .map_or_else(|| Err(InMemStorageError::DialogueNotFound), |_| Ok(()))
}) })
} }

View file

@ -25,7 +25,7 @@ where
/// Returned from [`RedisStorage::remove_dialogue`]. /// Returned from [`RedisStorage::remove_dialogue`].
#[error("row not found")] #[error("row not found")]
RowNotFound, DialogueNotFound,
} }
/// A dialogue storage based on [Redis](https://redis.io/). /// A dialogue storage based on [Redis](https://redis.io/).
@ -68,8 +68,8 @@ where
if let redis::Value::Bulk(values) = deleted_rows_count { if let redis::Value::Bulk(values) = deleted_rows_count {
if let redis::Value::Int(deleted_rows_count) = values[0] { if let redis::Value::Int(deleted_rows_count) = values[0] {
match deleted_rows_count { match deleted_rows_count {
0 => return Err(RedisStorageError::RowNotFound), 0 => return Err(RedisStorageError::DialogueNotFound),
_ => return Ok(()) _ => return Ok(()),
} }
} }
} }

View file

@ -27,6 +27,10 @@ where
#[error("sqlite error: {0}")] #[error("sqlite error: {0}")]
SqliteError(#[from] sqlx::Error), SqliteError(#[from] sqlx::Error),
/// Returned from [`SqliteStorage::remove_dialogue`].
#[error("row not found")]
DialogueNotFound,
} }
impl<S> SqliteStorage<S> { impl<S> SqliteStorage<S> {
@ -73,7 +77,7 @@ where
.rows_affected(); .rows_affected();
if deleted_rows_count == 0 { if deleted_rows_count == 0 {
return Err(SqliteStorageError::SqliteError(sqlx::Error::RowNotFound)); return Err(SqliteStorageError::DialogueNotFound);
} }
Ok(()) Ok(())

View file

@ -74,6 +74,6 @@ where
// Check that a try to remove a non-existing dialogue results in an error. // Check that a try to remove a non-existing dialogue results in an error.
assert!(matches!( assert!(matches!(
Arc::clone(&storage).remove_dialogue(1).await.unwrap_err(), Arc::clone(&storage).remove_dialogue(1).await.unwrap_err(),
RedisStorageError::RowNotFound RedisStorageError::DialogueNotFound
)); ));
} }

View file

@ -68,9 +68,8 @@ where
test_dialogues!(storage, None, None, None); test_dialogues!(storage, None, None, None);
// Check that a try to remove a non-existing dialogue results in an error. // Check that a try to remove a non-existing dialogue results in an error.
let err = Arc::clone(&storage).remove_dialogue(1).await.unwrap_err(); assert!(matches!(
match err { Arc::clone(&storage).remove_dialogue(1).await.unwrap_err(),
SqliteStorageError::SqliteError(err) => assert!(matches!(err, sqlx::Error::RowNotFound)), SqliteStorageError::DialogueNotFound
_ => panic!("Must be sqlx::Error::RowNotFound"), ));
}
} }