use super::EditorError;
use ass_core::utils::errors::CoreError;
use core::fmt;
#[cfg(not(feature = "std"))]
use alloc::string::ToString;
impl EditorError {
pub fn command_failed<T: fmt::Display>(message: T) -> Self {
Self::CommandFailed {
message: message.to_string(),
}
}
pub fn validation<T: fmt::Display>(message: T) -> Self {
Self::ValidationError {
message: message.to_string(),
}
}
pub fn io<T: fmt::Display>(message: T) -> Self {
Self::IoError(message.to_string())
}
pub fn builder_validation<T: fmt::Display>(message: T) -> Self {
Self::BuilderValidationError {
message: message.to_string(),
}
}
pub fn serialization<T: fmt::Display>(message: T) -> Self {
Self::SerializationError {
message: message.to_string(),
}
}
pub fn format_line<T: fmt::Display>(message: T) -> Self {
Self::FormatLineError {
message: message.to_string(),
}
}
#[must_use]
pub const fn is_recoverable(&self) -> bool {
match self {
Self::Core(core_err) => core_err.is_recoverable(),
Self::DocumentNotFound { .. }
| Self::InvalidPosition { .. }
| Self::PositionOutOfBounds { .. }
| Self::InvalidRange { .. }
| Self::CommandFailed { .. }
| Self::HistoryError { .. }
| Self::NothingToUndo
| Self::NothingToRedo
| Self::SearchIndexError { .. }
| Self::ExtensionError { .. }
| Self::FeatureNotEnabled { .. }
| Self::RopeOperationFailed { .. }
| Self::EventChannelError { .. }
| Self::ValidationError { .. }
| Self::IoError(..)
| Self::InvalidFormat(..)
| Self::UnsupportedFormat(..)
| Self::BuilderValidationError { .. }
| Self::SerializationError { .. }
| Self::FormatLineError { .. }
| Self::SectionNotFound { .. } => true,
Self::SessionLimitExceeded { .. }
| Self::ArenaAllocationFailed { .. }
| Self::ThreadSafetyError { .. } => false,
}
}
#[must_use]
pub const fn is_position_error(&self) -> bool {
matches!(
self,
Self::InvalidPosition { .. }
| Self::PositionOutOfBounds { .. }
| Self::InvalidRange { .. }
)
}
#[must_use]
pub const fn is_history_error(&self) -> bool {
matches!(
self,
Self::HistoryError { .. } | Self::NothingToUndo | Self::NothingToRedo
)
}
#[must_use]
pub const fn as_core_error(&self) -> Option<&CoreError> {
match self {
Self::Core(core_err) => Some(core_err),
_ => None,
}
}
}