system_shutdown/
lib.rs

1//! `system_shutdown` provides a cross platform way to shut down, reboot or log out operations.
2//!
3//! Supported platforms: Linux, Windows and macOS.
4//!
5//! # Example
6//!
7//! The example below shows how to shut down the machine:
8//!
9//! ```rust
10//! use system_shutdown::shutdown;
11//!
12//! fn main() {
13//!     match shutdown() {
14//!         Ok(_) => println!("Shutting down, bye!"),
15//!         Err(error) => eprintln!("Failed to shut down: {}", error),
16//!     }
17//! }
18//! ```
19//!
20//! In most of the systems it does not requires the user to be root/admin.
21
22#[cfg(target_os = "linux")]
23#[path = "linux.rs"]
24mod os;
25
26#[cfg(target_os = "macos")]
27#[path = "macos.rs"]
28mod os;
29#[cfg(target_os = "macos")]
30pub use os::request_permission_dialog;
31
32#[cfg(target_os = "windows")]
33#[path = "windows.rs"]
34mod os;
35#[cfg(target_os = "windows")]
36pub use os::{reboot_with_message, shutdown_with_message};
37
38use std::io;
39
40#[doc(hidden)]
41#[macro_export]
42macro_rules! not_implemented {
43    () => {
44        Err(std::io::Error::new(
45            std::io::ErrorKind::Other,
46            "feature not implemented yet",
47        ))
48    };
49}
50
51/// A specialized `Result` type for shut down, reboot and log out operations.
52pub type ShutdownResult = io::Result<()>;
53
54/// Calls the OS-specific function to shut down the machine.
55pub fn shutdown() -> ShutdownResult {
56    os::shutdown()
57}
58
59/// Calls the OS-specific function to force to shut down the machine.
60pub fn force_shutdown() -> ShutdownResult {
61    os::force_shutdown()
62}
63
64/// Calls the OS-specific function to reboot the machine.
65pub fn reboot() -> ShutdownResult {
66    os::reboot()
67}
68
69/// Calls the OS-specific function to force to reboot the machine.
70pub fn force_reboot() -> ShutdownResult {
71    os::force_reboot()
72}
73
74/// Calls the OS-specific function to log out the user.
75pub fn logout() -> ShutdownResult {
76    os::logout()
77}
78
79/// Calls the OS-specific function to force to log out the user.
80pub fn force_logout() -> ShutdownResult {
81    os::force_logout()
82}
83
84/// Calls the OS-specific function to put the machine to sleep.
85pub fn sleep() -> ShutdownResult {
86    os::sleep()
87}
88
89/// Calls the OS-specific function to hibernate the machine.
90pub fn hibernate() -> ShutdownResult {
91    os::hibernate()
92}