rocket/shutdown/
sig.rs

1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5/// A Unix signal for triggering graceful shutdown.
6///
7/// Each variant corresponds to a Unix process signal which can be used to
8/// trigger a graceful shutdown. See [`Shutdown`](crate::Shutdown) for details.
9///
10/// ## (De)serialization
11///
12/// A `Sig` variant serializes and deserializes as a lowercase string equal to
13/// the name of the variant: `"alrm"` for [`Sig::Alrm`], `"chld"` for
14/// [`Sig::Chld`], and so on.
15#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
16#[serde(rename_all = "lowercase")]
17#[cfg_attr(nightly, doc(cfg(unix)))]
18pub enum Sig {
19    /// The `SIGALRM` Unix signal.
20    Alrm,
21    /// The `SIGCHLD` Unix signal.
22    Chld,
23    /// The `SIGHUP` Unix signal.
24    Hup,
25    /// The `SIGINT` Unix signal.
26    Int,
27    /// The `SIGIO` Unix signal.
28    Io,
29    /// The `SIGPIPE` Unix signal.
30    Pipe,
31    /// The `SIGQUIT` Unix signal.
32    Quit,
33    /// The `SIGTERM` Unix signal.
34    Term,
35    /// The `SIGUSR1` Unix signal.
36    Usr1,
37    /// The `SIGUSR2` Unix signal.
38    Usr2
39}
40
41impl Sig {
42    pub fn as_str(&self) -> &'static str {
43        match self {
44            Sig::Alrm => "SIGALRM",
45            Sig::Chld => "SIGCHLD",
46            Sig::Hup => "SIGHUP",
47            Sig::Int => "SIGINT",
48            Sig::Io => "SIGIO",
49            Sig::Pipe => "SIGPIPE",
50            Sig::Quit => "SIGQUIT",
51            Sig::Term => "SIGTERM",
52            Sig::Usr1 => "SIGUSR1",
53            Sig::Usr2 => "SIGUSR2",
54        }
55    }
56}
57
58impl fmt::Display for Sig {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.write_str(self.as_str())
61    }
62}