Skip to main content

focus_tracker_core/
error.rs

1use std::sync::PoisonError;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum FocusTrackerError {
6    #[error("unsupported platform or environment")]
7    Unsupported,
8
9    #[error("permission denied: {context}")]
10    PermissionDenied { context: String },
11
12    #[error("no display available")]
13    NoDisplay,
14
15    #[error("not running in interactive session")]
16    NotInteractiveSession,
17
18    #[error("invalid config: {reason}")]
19    InvalidConfig { reason: String },
20
21    #[error("{context}")]
22    Platform {
23        context: String,
24        #[source]
25        source: Option<Box<dyn std::error::Error + Send + Sync>>,
26    },
27}
28
29impl FocusTrackerError {
30    pub fn platform(context: impl Into<String>) -> Self {
31        Self::Platform {
32            context: context.into(),
33            source: None,
34        }
35    }
36
37    pub fn platform_with_source(
38        context: impl Into<String>,
39        source: impl std::error::Error + Send + Sync + 'static,
40    ) -> Self {
41        Self::Platform {
42            context: context.into(),
43            source: Some(Box::new(source)),
44        }
45    }
46}
47
48pub type FocusTrackerResult<T> = Result<T, FocusTrackerError>;
49
50impl<T> From<PoisonError<T>> for FocusTrackerError {
51    fn from(value: PoisonError<T>) -> Self {
52        FocusTrackerError::platform(format!("mutex poisoned: {value}"))
53    }
54}