Skip to content

Commit f1bc718

Browse files
authored
feat: add is_* error predicates for all DbError categories (#91) (#103)
* feat: add is_* error predicates for all DbError categories (#91) * feat:Fixing more things from issue #91
1 parent 2c9aa72 commit f1bc718

1 file changed

Lines changed: 123 additions & 2 deletions

File tree

aimdb-core/src/error.rs

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! different deployment targets:
1010
//!
1111
//! - **MCU/Embedded**: Minimal memory footprint with `no_std` compatibility
12-
//! - **Edge/Desktop**: Rich error context with standard library features
12+
//! - **Edge/Desktop**: Rich error context with standard library features
1313
//! - **Cloud**: Full error chains and debugging capabilities with thiserror integration
1414
//!
1515
//! # Error Categories
@@ -384,7 +384,7 @@ impl DbError {
384384
matches!(self, DbError::ConnectionFailed { .. })
385385
}
386386

387-
/// Returns true if this is a capacity-related error
387+
/// Returns true if this is a capacity-related error
388388
pub fn is_capacity_error(&self) -> bool {
389389
matches!(self, DbError::BufferFull { .. })
390390
}
@@ -394,6 +394,65 @@ impl DbError {
394394
matches!(self, DbError::HardwareError { .. })
395395
}
396396

397+
/// Returns true if this is a buffer-related error
398+
pub fn is_buffer_error(&self) -> bool {
399+
matches!(
400+
self,
401+
DbError::BufferFull { .. }
402+
| DbError::BufferEmpty
403+
| DbError::BufferLagged { .. }
404+
| DbError::BufferClosed { .. }
405+
)
406+
}
407+
408+
/// Returns true if this is a database-related error
409+
pub fn is_database_error(&self) -> bool {
410+
matches!(
411+
self,
412+
DbError::RecordNotFound { .. }
413+
| DbError::RecordKeyNotFound { .. }
414+
| DbError::InvalidRecordId { .. }
415+
| DbError::TypeMismatch { .. }
416+
| DbError::AmbiguousType { .. }
417+
| DbError::DuplicateRecordKey { .. }
418+
| DbError::InvalidOperation { .. }
419+
| DbError::PermissionDenied { .. }
420+
)
421+
}
422+
423+
/// Returns true if this is a configuration-related error
424+
pub fn is_configuration_error(&self) -> bool {
425+
matches!(self, DbError::MissingConfiguration { .. })
426+
}
427+
428+
/// Returns true if this is a runtime error
429+
pub fn is_runtime_error(&self) -> bool {
430+
matches!(
431+
self,
432+
DbError::RuntimeError { .. } | DbError::ResourceUnavailable { .. }
433+
)
434+
}
435+
436+
/// Returns true if this is a transform error
437+
pub fn is_transform_error(&self) -> bool {
438+
matches!(
439+
self,
440+
DbError::CyclicDependency { .. } | DbError::TransformInputNotFound { .. }
441+
)
442+
}
443+
444+
/// Returns true if this is an IO error
445+
#[cfg(feature = "std")]
446+
pub fn is_io_error(&self) -> bool {
447+
matches!(self, DbError::Io { .. } | DbError::IoWithContext { .. })
448+
}
449+
450+
/// Returns true if this is a JSON error
451+
#[cfg(feature = "std")]
452+
pub fn is_json_error(&self) -> bool {
453+
matches!(self, DbError::Json { .. } | DbError::JsonWithContext { .. })
454+
}
455+
397456
/// Returns a numeric error code for embedded environments
398457
pub const fn error_code(&self) -> u32 {
399458
match self {
@@ -807,4 +866,66 @@ mod tests {
807866
let db_error: DbError = json_error.into();
808867
assert!(matches!(db_error, DbError::Json { .. }));
809868
}
869+
870+
#[test]
871+
fn test_buffer_empty() {
872+
let buffer = DbError::BufferEmpty;
873+
assert!(buffer.is_buffer_error());
874+
assert!(!buffer.is_network_error());
875+
}
876+
877+
#[cfg(feature = "std")]
878+
#[test]
879+
fn test_configuration_error() {
880+
let configuration = DbError::MissingConfiguration {
881+
parameter: "my_parameter".to_string(),
882+
};
883+
assert!(configuration.is_configuration_error());
884+
assert!(!configuration.is_buffer_error());
885+
}
886+
887+
#[cfg(feature = "std")]
888+
#[test]
889+
fn test_runtime_error() {
890+
let runtime = DbError::RuntimeError {
891+
message: "Hello, World".to_string(),
892+
};
893+
assert!(runtime.is_runtime_error());
894+
assert!(!runtime.is_buffer_error());
895+
}
896+
897+
#[test]
898+
fn test_database_error() {
899+
let database = DbError::InvalidRecordId { id: 5 };
900+
assert!(database.is_database_error());
901+
assert!(!database.is_buffer_error());
902+
}
903+
904+
#[cfg(feature = "std")]
905+
#[test]
906+
fn test_transform_error() {
907+
let transform = DbError::CyclicDependency {
908+
records: vec!["Hello, World!".to_string()],
909+
};
910+
assert!(transform.is_transform_error());
911+
assert!(!transform.is_buffer_error());
912+
}
913+
914+
#[cfg(feature = "std")]
915+
#[test]
916+
fn test_io_error() {
917+
let io_error = std::io::Error::other("test");
918+
let db_error: DbError = io_error.into();
919+
assert!(db_error.is_io_error());
920+
assert!(!db_error.is_buffer_error());
921+
}
922+
923+
#[cfg(feature = "std")]
924+
#[test]
925+
fn test_json_error() {
926+
let json_error = serde_json::from_str::<serde_json::Value>("invalid").unwrap_err();
927+
let db_error: DbError = json_error.into();
928+
assert!(db_error.is_json_error());
929+
assert!(!db_error.is_buffer_error());
930+
}
810931
}

0 commit comments

Comments
 (0)