cni_plugin/
command.rs

1use std::str::FromStr;
2
3use crate::error::InvalidCommandError;
4
5/// Identifies the command given to a plugin.
6///
7/// For more information about the command semantics, see the spec or the
8/// [`Cni`][crate::Cni] enum documentation.
9#[derive(Clone, Copy, Debug)]
10pub enum Command {
11	/// The ADD command.
12	Add,
13
14	/// The DEL command.
15	Del,
16
17	/// The CHECK command.
18	///
19	/// Introduced in spec version 1.0.0.
20	Check,
21
22	/// The VERSION command.
23	Version,
24}
25
26impl FromStr for Command {
27	type Err = InvalidCommandError;
28
29	/// Parses the Command from exactly ADD, DEL, CHECK, or VERSION only.
30	fn from_str(s: &str) -> Result<Self, Self::Err> {
31		match s {
32			"ADD" => Ok(Self::Add),
33			"DEL" => Ok(Self::Del),
34			"CHECK" => Ok(Self::Check),
35			"VERSION" => Ok(Self::Version),
36			_ => Err(InvalidCommandError),
37		}
38	}
39}
40
41impl AsRef<str> for Command {
42	/// Returns one of ADD, DEL, CHECK, or VERSION.
43	fn as_ref(&self) -> &'static str {
44		match self {
45			Command::Add => "ADD",
46			Command::Del => "DEL",
47			Command::Check => "CHECK",
48			Command::Version => "VERSION",
49		}
50	}
51}