Skip to content

Commit

Permalink
Shorten the name of the Minor variants
Browse files Browse the repository at this point in the history
Signed-off-by: Nathaniel McCallum <[email protected]>
  • Loading branch information
npmccallum committed Dec 10, 2020
1 parent 967c27f commit 7c40978
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
84 changes: 42 additions & 42 deletions src/basic/min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use core::convert::TryFrom;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Minor {
Immediate(u8),
Subsequent1([u8; 1]),
Subsequent2([u8; 2]),
Subsequent4([u8; 4]),
Subsequent8([u8; 8]),
Indeterminate,
This(u8),
Next1([u8; 1]),
Next2([u8; 2]),
Next4([u8; 4]),
Next8([u8; 8]),
More,
}

impl TryFrom<u8> for Minor {
Expand All @@ -20,12 +20,12 @@ impl TryFrom<u8> for Minor {
#[inline]
fn try_from(value: u8) -> Result<Self, Self::Error> {
Ok(match value & 0b00011111 {
x @ 0..=23 => Minor::Immediate(x),
24 => Minor::Subsequent1([0u8; 1]),
25 => Minor::Subsequent2([0u8; 2]),
26 => Minor::Subsequent4([0u8; 4]),
27 => Minor::Subsequent8([0u8; 8]),
31 => Minor::Indeterminate,
x @ 0..=23 => Minor::This(x),
24 => Minor::Next1([0u8; 1]),
25 => Minor::Next2([0u8; 2]),
26 => Minor::Next4([0u8; 4]),
27 => Minor::Next8([0u8; 8]),
31 => Minor::More,
_ => return Err(InvalidError(())),
})
}
Expand All @@ -35,12 +35,12 @@ impl From<Minor> for u8 {
#[inline]
fn from(value: Minor) -> Self {
match value {
Minor::Immediate(x) => x,
Minor::Subsequent1(..) => 24,
Minor::Subsequent2(..) => 25,
Minor::Subsequent4(..) => 26,
Minor::Subsequent8(..) => 27,
Minor::Indeterminate => 31,
Minor::This(x) => x,
Minor::Next1(..) => 24,
Minor::Next2(..) => 25,
Minor::Next4(..) => 26,
Minor::Next8(..) => 27,
Minor::More => 31,
}
}
}
Expand All @@ -49,12 +49,12 @@ impl AsRef<[u8]> for Minor {
#[inline]
fn as_ref(&self) -> &[u8] {
match self {
Self::Indeterminate => &[],
Self::Immediate(_) => &[],
Self::Subsequent1(x) => x.as_ref(),
Self::Subsequent2(x) => x.as_ref(),
Self::Subsequent4(x) => x.as_ref(),
Self::Subsequent8(x) => x.as_ref(),
Self::More => &[],
Self::This(..) => &[],
Self::Next1(x) => x.as_ref(),
Self::Next2(x) => x.as_ref(),
Self::Next4(x) => x.as_ref(),
Self::Next8(x) => x.as_ref(),
}
}
}
Expand All @@ -63,12 +63,12 @@ impl AsMut<[u8]> for Minor {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
match self {
Self::Indeterminate => &mut [],
Self::Immediate(_) => &mut [],
Self::Subsequent1(x) => x.as_mut(),
Self::Subsequent2(x) => x.as_mut(),
Self::Subsequent4(x) => x.as_mut(),
Self::Subsequent8(x) => x.as_mut(),
Self::More => &mut [],
Self::This(..) => &mut [],
Self::Next1(x) => x.as_mut(),
Self::Next2(x) => x.as_mut(),
Self::Next4(x) => x.as_mut(),
Self::Next8(x) => x.as_mut(),
}
}
}
Expand All @@ -77,12 +77,12 @@ impl From<Minor> for Option<u64> {
#[inline]
fn from(value: Minor) -> Self {
Some(match value {
Minor::Immediate(x) => x.into(),
Minor::Subsequent1(x) => u8::from_be_bytes(x).into(),
Minor::Subsequent2(x) => u16::from_be_bytes(x).into(),
Minor::Subsequent4(x) => u32::from_be_bytes(x).into(),
Minor::Subsequent8(x) => u64::from_be_bytes(x),
Minor::Indeterminate => return None,
Minor::This(x) => x.into(),
Minor::Next1(x) => u8::from_be_bytes(x).into(),
Minor::Next2(x) => u16::from_be_bytes(x).into(),
Minor::Next4(x) => u32::from_be_bytes(x).into(),
Minor::Next8(x) => u64::from_be_bytes(x),
Minor::More => return None,
})
}
}
Expand All @@ -103,15 +103,15 @@ impl From<u64> for Minor {
#[inline]
fn from(value: u64) -> Self {
if value < 24 {
Self::Immediate(value as u8)
Self::This(value as u8)
} else if let Ok(value) = u8::try_from(value) {
Self::Subsequent1(value.to_be_bytes())
Self::Next1(value.to_be_bytes())
} else if let Ok(value) = u16::try_from(value) {
Self::Subsequent2(value.to_be_bytes())
Self::Next2(value.to_be_bytes())
} else if let Ok(value) = u32::try_from(value) {
Self::Subsequent4(value.to_be_bytes())
Self::Next4(value.to_be_bytes())
} else {
Self::Subsequent8(value.to_be_bytes())
Self::Next8(value.to_be_bytes())
}
}
}
Expand All @@ -129,7 +129,7 @@ impl From<Option<usize>> for Minor {
fn from(value: Option<usize>) -> Self {
match value {
Some(x) => x.into(),
None => Self::Indeterminate,
None => Self::More,
}
}
}
14 changes: 7 additions & 7 deletions src/basic/tit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use core::convert::TryFrom;
pub struct Title(pub Major, pub Minor);

impl Title {
pub const BREAK: Self = Self(Major::Other, Minor::Indeterminate);
pub const BREAK: Self = Self(Major::Other, Minor::More);

pub const FALSE: Self = Self(Major::Other, Minor::Immediate(20));
pub const TRUE: Self = Self(Major::Other, Minor::Immediate(21));
pub const NULL: Self = Self(Major::Other, Minor::Immediate(22));
pub const UNDEFINED: Self = Self(Major::Other, Minor::Immediate(23));
pub const FALSE: Self = Self(Major::Other, Minor::This(20));
pub const TRUE: Self = Self(Major::Other, Minor::This(21));
pub const NULL: Self = Self(Major::Other, Minor::This(22));
pub const UNDEFINED: Self = Self(Major::Other, Minor::This(23));

pub const TAG_BIGPOS: Self = Self(Major::Tag, Minor::Immediate(2));
pub const TAG_BIGNEG: Self = Self(Major::Tag, Minor::Immediate(3));
pub const TAG_BIGPOS: Self = Self(Major::Tag, Minor::This(2));
pub const TAG_BIGNEG: Self = Self(Major::Tag, Minor::This(3));
}

impl TryFrom<u8> for Title {
Expand Down
14 changes: 7 additions & 7 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ where
let (title, offset) = self.0.pull(true)?;

let float = match (title.0, title.1) {
(Major::Other, Minor::Subsequent2(x)) => half::f16::from_be_bytes(x).into(),
(Major::Other, Minor::Subsequent4(x)) => f32::from_be_bytes(x).into(),
(Major::Other, Minor::Subsequent8(x)) => f64::from_be_bytes(x).into(),
(Major::Other, Minor::Next2(x)) => half::f16::from_be_bytes(x).into(),
(Major::Other, Minor::Next4(x)) => f32::from_be_bytes(x).into(),
(Major::Other, Minor::Next8(x)) => f64::from_be_bytes(x).into(),
_ => return Err(Error::semantic(offset, msg)),
};

Expand Down Expand Up @@ -233,9 +233,9 @@ where
Title(Major::Array, _) => self.deserialize_seq(v),
Title(Major::Map, _) => self.deserialize_map(v),

Title(Major::Other, Minor::Subsequent2(_)) => self.deserialize_f64(v),
Title(Major::Other, Minor::Subsequent4(_)) => self.deserialize_f64(v),
Title(Major::Other, Minor::Subsequent8(_)) => self.deserialize_f64(v),
Title(Major::Other, Minor::Next2(_)) => self.deserialize_f64(v),
Title(Major::Other, Minor::Next4(_)) => self.deserialize_f64(v),
Title(Major::Other, Minor::Next8(_)) => self.deserialize_f64(v),
Title::FALSE => self.deserialize_bool(v),
Title::TRUE => self.deserialize_bool(v),
Title::UNDEFINED => self.deserialize_option(v),
Expand Down Expand Up @@ -473,7 +473,7 @@ where
let (title, offset) = self.0.pull(true)?;

match title {
Title(Major::Map, Minor::Immediate(1)) => visitor.visit_enum(self),
Title(Major::Map, Minor::This(1)) => visitor.visit_enum(self),
Title(Major::Text, ..) => {
self.0.push((title, offset));
visitor.visit_enum(self)
Expand Down
6 changes: 3 additions & 3 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ where
let v = Float::from(v);

let minor = if let Ok(x) = half::f16::try_from(v) {
Minor::Subsequent2(x.to_be_bytes())
Minor::Next2(x.to_be_bytes())
} else if let Ok(x) = f32::try_from(v) {
Minor::Subsequent4(x.to_be_bytes())
Minor::Next4(x.to_be_bytes())
} else {
Minor::Subsequent8(f64::from(v).to_be_bytes())
Minor::Next8(f64::from(v).to_be_bytes())
};

self.save(Title(Major::Other, minor))
Expand Down

0 comments on commit 7c40978

Please sign in to comment.