der/tag/
mode.rs

1//! Tag modes.
2
3use crate::{Error, ErrorKind, Result};
4use core::{fmt, str::FromStr};
5
6/// Tagging modes: `EXPLICIT` versus `IMPLICIT`.
7#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
8pub enum TagMode {
9    /// `EXPLICIT` tagging.
10    ///
11    /// Tag is added in addition to the inner tag of the type.
12    Explicit,
13
14    /// `IMPLICIT` tagging.
15    ///
16    /// Tag replaces the existing tag of the inner type.
17    Implicit,
18}
19
20impl Default for TagMode {
21    fn default() -> TagMode {
22        TagMode::Explicit
23    }
24}
25
26impl FromStr for TagMode {
27    type Err = Error;
28
29    fn from_str(s: &str) -> Result<Self> {
30        match s {
31            "EXPLICIT" | "explicit" => Ok(TagMode::Explicit),
32            "IMPLICIT" | "implicit" => Ok(TagMode::Implicit),
33            _ => Err(ErrorKind::TagModeUnknown.into()),
34        }
35    }
36}
37
38impl fmt::Display for TagMode {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            TagMode::Explicit => f.write_str("EXPLICIT"),
42            TagMode::Implicit => f.write_str("IMPLICIT"),
43        }
44    }
45}