Skip to content

Commit

Permalink
objectionary#29: implement FromStr trait for Hex
Browse files Browse the repository at this point in the history
  • Loading branch information
UARTman committed Nov 23, 2022
1 parent d40b2e0 commit aa2da93
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
62 changes: 35 additions & 27 deletions src/hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use crate::{Deserialize, Serialize};
use anyhow::{Context, Result};
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;

/// It is an object-oriented representation of binary data
/// in hexadecimal format, which can be put into vertices of the graph.
Expand Down Expand Up @@ -143,34 +144,20 @@ impl Hex {
}
}

/// From `String` as HEX, for example `DE-AD-BE-EF-20-22`.
///
/// ```
/// use sodg::Hex;
/// let hex = "DE-AD-BE-EF-20-22";
/// let d = Hex::parse(hex.to_string());
/// assert_eq!("DE-AD-BE-EF-20-22", d.print());
/// ```
pub fn parse(hex: String) -> Self {
let s = hex.replace('-', "");
Self::from_vec(hex::decode(s).unwrap())
}

/// Make `Hex` from `String`.
///
/// ```
/// use sodg::Hex;
/// let d = Hex::from_string("Ура!".to_string());
/// let d = Hex::from_string_bytes("Ура!".to_string());
/// assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());
/// ```
pub fn from_string(d: String) -> Self {
pub fn from_string_bytes(d: String) -> Self {
Self::from_slice(d.as_bytes())
}

/// From `&str`.
#[allow(clippy::should_implement_trait)]
pub fn from_str(d: &str) -> Self {
Self::from_slice(d.to_string().as_bytes())
/// Make hex from the bytes composing `&str`.
pub fn from_str_bytes(d: &str) -> Self {
Self::from_slice(d.as_bytes())
}

/// It's empty and no data?
Expand Down Expand Up @@ -272,7 +259,7 @@ impl Hex {
///
/// ```
/// use sodg::Hex;
/// let d = Hex::from_str("你好");
/// let d = Hex::from_str_bytes("你好");
/// assert_eq!("E4-BD-A0-E5-A5-BD", d.print());
/// assert_eq!(0xA0, d.byte_at(2));
/// ```
Expand All @@ -285,7 +272,7 @@ impl Hex {
///
/// ```
/// use sodg::Hex;
/// let d = Hex::from_str("Hello, world!");
/// let d = Hex::from_str_bytes("Hello, world!");
/// assert_eq!("world!", d.tail(7).to_utf8().unwrap());
/// ```
pub fn tail(&self, skip: usize) -> Self {
Expand Down Expand Up @@ -333,6 +320,27 @@ impl From<bool> for Hex {
}
}

impl FromStr for Hex {
type Err = anyhow::Error;

/// Creeate a `Hex` from a `&str` containing a hexadecimal representation of some data,
/// for example, `DE-AD-BE-EF-20-22`.
///
/// ```
/// use sodg::Hex;
/// use std::str::FromStr;
/// let hex = "DE-AD-BE-EF-20-22";
/// let d: Hex = hex.parse().unwrap();
/// let d2 = Hex::from_str(hex).unwrap();
/// assert_eq!("DE-AD-BE-EF-20-22", d.print());
/// assert_eq!("DE-AD-BE-EF-20-22", d2.print());
/// ```
fn from_str(hex: &str) -> std::result::Result<Self, Self::Err> {
let s = hex.replace('-', "");
Ok(Self::from_vec(hex::decode(s)?))
}
}

#[test]
fn simple_int() -> Result<()> {
let i = 42;
Expand Down Expand Up @@ -372,16 +380,16 @@ fn compares_with_data() -> Result<()> {
#[test]
fn prints_bytes() -> Result<()> {
let txt = "привет";
let d = Hex::from_str(txt);
let d = Hex::from_str_bytes(txt);
assert_eq!("D0-BF-D1-80-D0-B8-D0-B2-D0-B5-D1-82", d.print());
assert_eq!(txt, Hex::parse(d.print()).to_utf8()?);
assert_eq!(txt, Hex::from_str(&d.print())?.to_utf8()?);
Ok(())
}

#[test]
fn prints_empty_bytes() -> Result<()> {
let txt = "";
let d = Hex::from_str(txt);
let d = Hex::from_str_bytes(txt);
assert_eq!("--", d.print());
Ok(())
}
Expand Down Expand Up @@ -453,22 +461,22 @@ fn non_utf8_string() -> Result<()> {

#[test]
fn takes_tail() -> Result<()> {
let d = Hex::from_str("Hello, world!");
let d = Hex::from_str_bytes("Hello, world!");
assert_eq!("world!", d.tail(7).to_utf8()?);
Ok(())
}

#[test]
fn takes_one_byte() -> Result<()> {
let d = Hex::from_str("Ура!");
let d = Hex::from_str_bytes("Ура!");
assert_eq!("D0-A3-D1-80-D0-B0-21", d.print());
assert_eq!(0xD1, d.byte_at(2));
Ok(())
}

#[test]
fn measures_length() -> Result<()> {
let d = Hex::from_str("Ура!");
let d = Hex::from_str_bytes("Ура!");
assert_eq!(7, d.len());
Ok(())
}
2 changes: 1 addition & 1 deletion src/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ use crate::Hex;
fn inspects_simple_object() -> Result<()> {
let mut g = Sodg::empty();
g.add(0)?;
g.put(0, Hex::from_str("hello"))?;
g.put(0, Hex::from_str_bytes("hello"))?;
g.add(1)?;
g.bind(0, 1, "foo")?;
let txt = g.inspect("")?;
Expand Down
6 changes: 3 additions & 3 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Sodg {
/// use sodg::Sodg;
/// let mut g = Sodg::empty();
/// g.add(42).unwrap();
/// g.put(42, Hex::from_str("hello, world!")).unwrap();
/// g.put(42, Hex::from_str_bytes("hello, world!")).unwrap();
/// ```
///
/// If vertex `v1` is absent, an `Err` will be returned.
Expand All @@ -112,7 +112,7 @@ impl Sodg {
/// use sodg::Sodg;
/// let mut g = Sodg::empty();
/// g.add(42).unwrap();
/// let data = Hex::from_str("hello, world!");
/// let data = Hex::from_str_bytes("hello, world!");
/// g.put(42, data.clone()).unwrap();
/// assert_eq!(data, g.data(42).unwrap());
/// ```
Expand Down Expand Up @@ -477,7 +477,7 @@ fn binds_to_root() -> Result<()> {
#[test]
fn sets_simple_data() -> Result<()> {
let mut g = Sodg::empty();
let data = Hex::from_str("hello");
let data = Hex::from_str_bytes("hello");
g.add(0)?;
g.put(0, data.clone())?;
assert_eq!(data, g.data(0)?);
Expand Down
4 changes: 2 additions & 2 deletions src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ use crate::Hex;
fn saves_and_loads() -> Result<()> {
let mut g = Sodg::empty();
g.add(0)?;
g.put(0, Hex::from_str("hello"))?;
g.put(0, Hex::from_str_bytes("hello"))?;
g.add(1)?;
g.bind(0, 1, "foo")?;
g.put(1, Hex::from_str("foo"))?;
g.put(1, Hex::from_str_bytes("foo"))?;
let tmp = TempDir::new()?;
let file = tmp.path().join("foo.sodg");
g.save(file.as_path())?;
Expand Down
4 changes: 2 additions & 2 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl Sodg {
/// use sodg::Sodg;
/// let mut g = Sodg::empty();
/// g.add(0).unwrap();
/// g.put(0, Hex::from_str("hello")).unwrap();
/// g.put(0, Hex::from_str_bytes("hello")).unwrap();
/// g.add(1).unwrap();
/// g.bind(0, 1, "foo").unwrap();
/// g.bind(0, 1, "bar").unwrap();
Expand Down Expand Up @@ -97,7 +97,7 @@ use crate::Hex;
fn prints_simple_graph() -> Result<()> {
let mut g = Sodg::empty();
g.add(0)?;
g.put(0, Hex::from_str("hello"))?;
g.put(0, Hex::from_str_bytes("hello"))?;
g.add(1)?;
g.bind(0, 1, "foo")?;
let xml = g.to_xml()?;
Expand Down

0 comments on commit aa2da93

Please sign in to comment.