#[allow(unused_imports, clippy::wildcard_imports)]
use super::*;
#[cfg_attr(feature = "serde", cfg_eval::cfg_eval)]
#[cfg_attr(feature = "alloc", derive(Default))]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[cfg_attr(
all(feature = "serde", feature = "alloc"),
derive(serde_with::SerializeDisplay, serde_with::DeserializeFromStr)
)]
pub struct Thresholds(pub [u8; 4]);
impl core::fmt::Debug for Thresholds {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let v = &self.0;
write!(f, "Thresholds(")?;
for b in v {
write!(f, "{b:02x}")?;
}
write!(f, ")")?;
Ok(())
}
}
impl core::fmt::Display for Thresholds {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let v = &self.0;
for b in v {
write!(f, "{b:02x}")?;
}
Ok(())
}
}
#[cfg(feature = "alloc")]
impl core::str::FromStr for Thresholds {
type Err = Error;
fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
hex::decode(s).map_err(|_| Error::InvalidHex)?.try_into()
}
}
#[cfg(feature = "schemars")]
impl schemars::JsonSchema for Thresholds {
fn schema_name() -> String {
"Thresholds".to_string()
}
fn is_referenceable() -> bool {
false
}
fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let schema = String::json_schema(gen);
if let schemars::schema::Schema::Object(mut schema) = schema {
schema.extensions.insert(
"contentEncoding".to_owned(),
serde_json::Value::String("hex".to_string()),
);
schema.extensions.insert(
"contentMediaType".to_owned(),
serde_json::Value::String("application/binary".to_string()),
);
let string = *schema.string.unwrap_or_default().clone();
schema.string = Some(Box::new(schemars::schema::StringValidation {
max_length: 4_u32.checked_mul(2).map(Some).unwrap_or_default(),
min_length: 4_u32.checked_mul(2).map(Some).unwrap_or_default(),
..string
}));
schema.into()
} else {
schema
}
}
}
impl From<Thresholds> for [u8; 4] {
#[must_use]
fn from(x: Thresholds) -> Self {
x.0
}
}
impl From<[u8; 4]> for Thresholds {
#[must_use]
fn from(x: [u8; 4]) -> Self {
Thresholds(x)
}
}
impl AsRef<[u8; 4]> for Thresholds {
#[must_use]
fn as_ref(&self) -> &[u8; 4] {
&self.0
}
}
impl ReadXdr for Thresholds {
#[cfg(feature = "std")]
fn read_xdr<R: Read>(r: &mut Limited<R>) -> Result<Self, Error> {
r.with_limited_depth(|r| {
let i = <[u8; 4]>::read_xdr(r)?;
let v = Thresholds(i);
Ok(v)
})
}
}
impl WriteXdr for Thresholds {
#[cfg(feature = "std")]
fn write_xdr<W: Write>(&self, w: &mut Limited<W>) -> Result<(), Error> {
w.with_limited_depth(|w| self.0.write_xdr(w))
}
}
impl Thresholds {
#[must_use]
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}
#[cfg(feature = "alloc")]
impl TryFrom<Vec<u8>> for Thresholds {
type Error = Error;
fn try_from(x: Vec<u8>) -> Result<Self, Error> {
x.as_slice().try_into()
}
}
#[cfg(feature = "alloc")]
impl TryFrom<&Vec<u8>> for Thresholds {
type Error = Error;
fn try_from(x: &Vec<u8>) -> Result<Self, Error> {
x.as_slice().try_into()
}
}
impl TryFrom<&[u8]> for Thresholds {
type Error = Error;
fn try_from(x: &[u8]) -> Result<Self, Error> {
Ok(Thresholds(x.try_into()?))
}
}
impl AsRef<[u8]> for Thresholds {
#[must_use]
fn as_ref(&self) -> &[u8] {
&self.0
}
}