use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum DocumentError {
#[error("{0}")]
NotFound(String),
#[error("{0}")]
InvalidCursorContext(String),
#[error("{0}")]
OutOfRange(String),
#[error("{0}")]
InvalidArgument(String),
#[error("{0}")]
Unsupported(String),
#[error("{0}")]
TransientEdits(TransientRefusal),
#[error(transparent)]
Internal(anyhow::Error),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum TransientRefusal {
#[error(
"a transient edit must change one run of text: this selection crosses a table's \
cells, or holds part of a table and text outside it"
)]
CrossesTableCells,
#[error("a load in place is writing the document: no transient edit is made until it ends")]
LoadInPlace,
#[error("a write to the document is in progress: no transient edit is made until it ends")]
WriteInProgress,
#[error(
"the transient edits changed the document in more than one place: committing them \
would replace the recorded text between them"
)]
CommitInSeveralPlaces,
#[error("the range held only transient text, which was reverted: nothing to replace")]
RangeOfTransientTextOnly,
#[error(
"transient edits are outstanding: the document's store refuses a recorded write over them"
)]
LayerOutstanding,
}
impl From<anyhow::Error> for DocumentError {
fn from(error: anyhow::Error) -> Self {
use common::database::rope_store::{TransientEditsOutstanding, TransientWriteRefused};
for cause in error.chain() {
if cause.downcast_ref::<TransientEditsOutstanding>().is_some() {
return DocumentError::TransientEdits(TransientRefusal::LayerOutstanding);
}
if let Some(refused) = cause.downcast_ref::<TransientWriteRefused>() {
return DocumentError::TransientEdits((*refused).into());
}
}
DocumentError::Internal(error)
}
}
impl From<common::database::rope_store::TransientWriteRefused> for TransientRefusal {
fn from(_: common::database::rope_store::TransientWriteRefused) -> Self {
TransientRefusal::WriteInProgress
}
}
pub type Result<T> = std::result::Result<T, DocumentError>;