Lines
76 %
Functions
63.64 %
Branches
100 %
//! Folding the failures of a lock-protocol command into an [`Attempt`].
use super::CmdError;
use crate::attempt;
use crate::tag::lock::{KeyChanged, is_retryable};
/// An attempt of a command.
pub(super) type Attempt = attempt::Attempt<CmdError>;
impl From<sqlx::Error> for Attempt {
fn from(err: sqlx::Error) -> Self {
match is_retryable(&err) {
true => Self::Retry,
false => Self::Failed(err.into()),
}
impl From<CmdError> for Attempt {
fn from(err: CmdError) -> Self {
Self::Failed(err)
/// A tag-lock outcome as an attempt; a missing owner row is `gone`.
pub(super) fn settled(
outcome: sqlx::Result<Result<(), KeyChanged>>,
gone: fn() -> CmdError,
) -> Result<(), Attempt> {
match outcome {
Ok(Ok(())) => Ok(()),
Ok(Err(KeyChanged)) => Err(Attempt::Retry),
Err(err) => Err(missing(err, gone)),
/// A missing row is `gone`; any other failure as in [`Attempt`].
pub(super) fn missing(err: sqlx::Error, gone: fn() -> CmdError) -> Attempt {
match err {
sqlx::Error::RowNotFound => gone().into(),
other => other.into(),
pub(super) fn found<T>(row: Option<T>, gone: fn() -> CmdError) -> Result<T, Attempt> {
row.ok_or_else(|| gone().into())