more_wallpapers/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#[cfg(target_os = "linux")]
use std::env;
use std::io;
#[cfg(target_os = "linux")]
use std::{ffi::OsString, process::Command};
use thiserror::Error;

#[cfg(target_os = "linux")]
use serde_json;
#[cfg(target_os = "linux")]
use xrandr;

#[cfg(target_os = "linux")]
#[derive(Debug, Error)]
pub enum CommandError {
	#[cfg(target_os = "linux")]
	#[error("failed to execute program {0:?}: {1}")]
	CommandIO(OsString, std::io::Error),

	#[cfg(target_os = "linux")]
	#[error("{command:?} exit with code {exit_code:?}:\n{}", String::from_utf8_lossy(.stderr))]
	CommandStatus {
		command: Command,
		exit_code: Option<i32>,
		stderr: Vec<u8>,
	},
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum WallpaperError {
	#[cfg(target_os = "linux")]
	#[error("unsupported Desktop Environment {0:?}")]
	Unsuported(String),

	#[cfg(target_os = "linux")]
	#[error("can not read Environment Variable {0:?}: {1}")]
	EnvVar(&'static str, env::VarError),

	#[cfg(target_os = "linux")]
	#[error("Dbus error: {0}")]
	Dbus(#[from] rustbus::connection::Error),

	#[cfg(target_os = "linux")]
	#[error("failed to serialize json output: {0}")]
	SerdeJson(#[from] serde_json::Error),

	#[cfg(target_os = "linux")]
	#[error("xrandr  erro): {0}")]
	Xrandr(#[from] xrandr::XrandrError),

	#[cfg(target_os = "linux")]
	#[error("{0}")]
	Command(#[from] CommandError),

	#[cfg(feature = "fallback")]
	#[error("{0}")]
	WallpaperCrate(#[from] fallback::Error),

	#[error("{0:?} {1}")]
	IOError(String, io::Error),

	#[cfg(target_os = "linux")]
	#[error("Unknow XFCE wallpaper mode {0:?}")]
	UnknownMode(String),
}

pub(crate) trait Context<V> {
	fn context<C>(self, context: C) -> Result<V, WallpaperError>
	where
		C: std::fmt::Display;
}

/// add path as additional info to erro and convert it to a [WallpaperError].
impl<V> Context<V> for Result<V, io::Error> {
	fn context<C>(self, context: C) -> Result<V, WallpaperError>
	where
		C: std::fmt::Display,
	{
		self.map_err(|error| WallpaperError::IOError(context.to_string(), error))
	}
}

#[cfg(target_os = "linux")]
pub(crate) fn load_env_var(key: &'static str) -> Result<String, WallpaperError> {
	std::env::var(key).map_err(|err| WallpaperError::EnvVar(key, err))
}