error.rs 922 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. use std::{error::Error, fmt::Display};
  2. #[derive(Debug)]
  3. pub enum BackendErrorKind {
  4. FileNotFound,
  5. KernelLoadError,
  6. }
  7. #[derive(Debug)]
  8. pub struct BackendError {
  9. kind: BackendErrorKind,
  10. message: Option<String>,
  11. }
  12. impl BackendError {
  13. pub fn new(kind: BackendErrorKind, message: Option<String>) -> Self {
  14. Self { kind, message }
  15. }
  16. }
  17. impl Error for BackendError {}
  18. impl Display for BackendError {
  19. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  20. match &self.kind {
  21. BackendErrorKind::FileNotFound => {
  22. write!(f, "File not found: {:?}", self.message.as_ref().unwrap())
  23. }
  24. BackendErrorKind::KernelLoadError => {
  25. write!(
  26. f,
  27. "Failed to load kernel: {:?}",
  28. self.message.as_ref().unwrap()
  29. )
  30. }
  31. }
  32. }
  33. }