render_error.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use std::fmt::{Debug, Display};
  2. use crate::innerlude::*;
  3. /// An error that can occur while rendering a component
  4. #[derive(Clone, PartialEq, Debug)]
  5. pub enum RenderError {
  6. /// The render function returned early
  7. Aborted(CapturedError),
  8. /// The component was suspended
  9. Suspended(SuspendedFuture),
  10. }
  11. impl Default for RenderError {
  12. fn default() -> Self {
  13. struct RenderAbortedEarly;
  14. impl Debug for RenderAbortedEarly {
  15. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  16. f.write_str("Render aborted early")
  17. }
  18. }
  19. impl Display for RenderAbortedEarly {
  20. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  21. f.write_str("Render aborted early")
  22. }
  23. }
  24. impl std::error::Error for RenderAbortedEarly {}
  25. Self::Aborted(RenderAbortedEarly.into())
  26. }
  27. }
  28. impl Display for RenderError {
  29. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  30. match self {
  31. Self::Aborted(e) => write!(f, "Render aborted: {e}"),
  32. Self::Suspended(e) => write!(f, "Component suspended: {e:?}"),
  33. }
  34. }
  35. }
  36. impl<E: std::error::Error + 'static> From<E> for RenderError {
  37. fn from(e: E) -> Self {
  38. Self::Aborted(CapturedError::from(e))
  39. }
  40. }
  41. impl From<CapturedError> for RenderError {
  42. fn from(e: CapturedError) -> Self {
  43. RenderError::Aborted(e)
  44. }
  45. }