use std::fmt;
use crate::format::Format;
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Endian {
#[default]
Big,
Little,
}
impl Endian {
pub fn is_little(self) -> bool {
self == Endian::Little
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Endpoint {
pub format: Format,
pub endian: Endian,
}
impl Endpoint {
pub const fn big(format: Format) -> Endpoint {
Endpoint { format, endian: Endian::Big }
}
pub const fn little(format: Format) -> Endpoint {
Endpoint { format, endian: Endian::Little }
}
pub fn parse(s: &str) -> Option<Endpoint> {
let lower = s.to_ascii_lowercase();
if let Some(f) = Format::parse(&lower) {
return Some(Endpoint::big(f));
}
for (prefix, endian) in [("le", Endian::Little), ("be", Endian::Big)] {
if let Some(rest) = lower.strip_prefix(prefix).filter(|r| !r.is_empty()) {
if let Some(format) = Format::parse(rest) {
return Some(Endpoint { format, endian });
}
}
}
None
}
pub fn command_name(self) -> String {
match self.endian {
Endian::Big => self.format.to_string(),
Endian::Little => format!("le{}", self.format),
}
}
}
impl fmt::Display for Endpoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.endian {
Endian::Big => write!(f, "{}", self.format),
Endian::Little => write!(f, "little-endian {}", self.format),
}
}
}
pub fn swap_words(bytes: &mut [u8], word: Option<usize>) -> Result<(), usize> {
match word.filter(|w| *w > 0) {
None => {
bytes.reverse();
Ok(())
}
Some(w) => {
let tail = bytes.len() % w;
for chunk in bytes.chunks_mut(w) {
chunk.reverse();
}
if tail == 0 { Ok(()) } else { Err(tail) }
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_a_prefixed_format() {
assert_eq!(Endpoint::parse("lehex"), Some(Endpoint::little(Format::HEX)));
assert_eq!(Endpoint::parse("behex"), Some(Endpoint::big(Format::HEX)));
assert_eq!(Endpoint::parse("hex"), Some(Endpoint::big(Format::HEX)));
assert_eq!(Endpoint::parse("LEHEX"), Some(Endpoint::little(Format::HEX)));
assert_eq!(Endpoint::parse("leb64"), Some(Endpoint::little(Format::B64)));
assert_eq!(Endpoint::parse("leb"), Some(Endpoint::little(Format::BIN)));
assert_eq!(Endpoint::parse("le16"), Some(Endpoint::little(Format::HEX)));
}
#[test]
fn a_bare_prefix_is_not_an_endpoint() {
for bare in ["", "le", "be", "l", "e", "b3", "leb3", "nonsense"] {
assert_eq!(Endpoint::parse(bare), None, "`{bare}` must not be an endpoint");
}
}
#[test]
fn displays_as_prose_but_names_as_a_command() {
assert_eq!(Endpoint::little(Format::HEX).to_string(), "little-endian hex");
assert_eq!(Endpoint::big(Format::HEX).to_string(), "hex");
assert_eq!(Endpoint::little(Format::HEX).command_name(), "lehex");
assert_eq!(Endpoint::big(Format::HEX).command_name(), "hex");
}
#[test]
fn swap_words_reverses_within_each_chunk() {
let mut b = [1, 2, 3, 4];
assert_eq!(swap_words(&mut b, Some(2)), Ok(()));
assert_eq!(b, [2, 1, 4, 3]);
let mut b = [1, 2, 3, 4];
assert_eq!(swap_words(&mut b, None), Ok(()));
assert_eq!(b, [4, 3, 2, 1]);
let mut b = [1, 2, 3];
assert_eq!(swap_words(&mut b, Some(2)), Err(1));
assert_eq!(b, [2, 1, 3]);
let mut b = [1, 2, 3];
assert_eq!(swap_words(&mut b, Some(0)), Ok(()));
assert_eq!(b, [3, 2, 1]);
}
}