use std::str::FromStr;
#[derive(Clone, PartialEq, Eq, Hash, Debug, Display, Error, From)]
#[display(doc_comments)]
pub struct FormatParseError;
#[derive(
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
Display,
StrictEncode,
StrictDecode
)]
#[cfg_attr(feature = "clap", derive(ArgEnum))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename = "lowercase")
)]
#[non_exhaustive]
#[repr(u8)]
pub enum BinaryFormat {
#[display("bin")]
Bin = 1,
#[display("hex")]
Hex = 2,
#[display("bech32")]
Bech32 = 3,
#[display("base58")]
Base58 = 4,
#[display("base64")]
Base64 = 5,
}
impl FromStr for BinaryFormat {
type Err = FormatParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match &s.to_lowercase() {
s if s.starts_with("bin") => Self::Bin,
s if s.starts_with("hex") => Self::Hex,
s if s.starts_with("bech32") => Self::Bech32,
s if s.starts_with("base64") => Self::Base64,
_ => Err(FormatParseError)?,
})
}
}
#[derive(
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
Display,
StrictEncode,
StrictDecode
)]
#[cfg_attr(feature = "clap", derive(ArgEnum))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename = "lowercase")
)]
#[non_exhaustive]
#[repr(u8)]
pub enum StructuredFormat {
#[display("bin")]
Bin = 1,
#[display("hex")]
Hex = 2,
#[display("bech32")]
Bech32 = 3,
#[display("base58")]
Base58 = 4,
#[display("base64")]
Base64 = 5,
#[display("json")]
Json = 10,
#[display("yaml")]
Yaml = 11,
#[display("toml")]
Toml = 12,
}
impl FromStr for StructuredFormat {
type Err = FormatParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match &s.to_lowercase() {
s if s.starts_with("yaml") || s.starts_with("yml") => Self::Yaml,
s if s.starts_with("json") => Self::Json,
s if s.starts_with("toml") => Self::Toml,
s if s.starts_with("bin") => Self::Bin,
s if s.starts_with("hex") => Self::Hex,
s if s.starts_with("bech32") => Self::Bech32,
s if s.starts_with("base64") => Self::Base64,
_ => Err(FormatParseError)?,
})
}
}
#[derive(
Copy,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
Debug,
Display,
StrictEncode,
StrictDecode
)]
#[cfg_attr(feature = "clap", derive(ArgEnum))]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename = "lowercase")
)]
#[non_exhaustive]
#[repr(u8)]
pub enum FileFormat {
#[display("json")]
Json = 10,
#[display("yaml")]
Yaml = 11,
#[display("toml")]
Toml = 12,
#[display("strict-encode")]
StrictEncode = 0,
}
impl FileFormat {
pub fn extension(&self) -> &'static str {
match self {
FileFormat::Yaml => "yaml",
FileFormat::Json => "json",
FileFormat::Toml => "toml",
FileFormat::StrictEncode => "se",
}
}
}
impl FromStr for FileFormat {
type Err = FormatParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match &s.to_lowercase() {
s if s.starts_with("yaml") || s.starts_with("yml") => Self::Yaml,
s if s.starts_with("json") => Self::Json,
s if s.starts_with("toml") => Self::Toml,
s if s.starts_with("se")
|| s.starts_with("dat")
|| s.starts_with("strictencode")
|| s.starts_with("strict-encode")
|| s.starts_with("strict_encode") =>
{
Self::StrictEncode
}
_ => Err(FormatParseError)?,
})
}
}