Skip to content

Commit

Permalink
#49: better data printing
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 25, 2022
1 parent 8e05653 commit bd8cc72
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 18 deletions.
67 changes: 64 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ impl Data {
self.bytes.len() == 0
}

/// Turn it into `bool`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x01].to_vec());
/// assert_eq!(true, d.as_bool().unwrap());
/// ```
pub fn as_bool(&self) -> Result<bool> {
Ok(self.bytes[0] == 0x01)
}

/// Turn it into `i64`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2A].to_vec());
/// assert_eq!(42, d.as_int().unwrap());
/// ```
pub fn as_int(&self) -> Result<i64> {
let a: &[u8; 8] = &self
.bytes
Expand All @@ -100,6 +118,13 @@ impl Data {
Ok(i64::from_be_bytes(*a))
}

/// Turn it into `f64`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x40, 0x09, 0x21, 0xfb, 0x54, 0x44, 0x2d, 0x18].to_vec());
/// assert_eq!(std::f64::consts::PI, d.as_float().unwrap());
/// ```
pub fn as_float(&self) -> Result<f64> {
let a: &[u8; 8] = &self
.bytes
Expand All @@ -109,28 +134,64 @@ impl Data {
Ok(f64::from_be_bytes(*a))
}

/// Turn it into `string`.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0x41, 0x42].to_vec());
/// assert_eq!("AB", d.as_string().unwrap());
/// ```
pub fn as_string(&self) -> Result<String> {
Ok(String::from_utf8(self.bytes.clone())?)
}

/// Turn it into a hexadecimal string.
///
/// ```
/// use reo::data::Data;
/// let d = Data::from_bytes([0xCA, 0xFE].to_vec());
/// assert_eq!("CA-FE", d.as_hex());
/// ```
pub fn as_hex(&self) -> String {
if self.bytes.is_empty() {
"--".to_string()
} else {
self.bytes
.iter()
.map(|b| format!("{:02x}", b).to_string())
.map(|b| format!("{:02X}", b).to_string())
.collect::<Vec<String>>()
.join("-")
}
}

/// Turn it into a vector of bytes.
pub fn as_bytes(&self) -> Vec<u8> {
self.bytes.clone()
}
}

#[test]
fn simple_int() {
let i = 42;
let d = Data::from_int(42);
let d = Data::from_int(i);
assert_eq!(i, d.as_int().unwrap());
assert_eq!("00-00-00-00-00-00-00-2A", d.as_hex());
}

#[test]
fn simple_bool() {
let b = true;
let d = Data::from_bool(b);
assert_eq!(b, d.as_bool().unwrap());
assert_eq!("01", d.as_hex());
}

#[test]
fn simple_float() {
let f = std::f64::consts::PI;
let d = Data::from_float(f);
assert_eq!(f, d.as_float().unwrap());
assert_eq!("40-09-21-FB-54-44-2D-18", d.as_hex());
}

#[test]
Expand All @@ -145,7 +206,7 @@ fn compares_with_data() {
fn prints_bytes() {
let txt = "привет";
let d = Data::from_str(txt);
assert_eq!("d0-bf-d1-80-d0-b8-d0-b2-d0-b5-d1-82", d.as_hex());
assert_eq!("D0-BF-D1-80-D0-B8-D0-B2-D0-B5-D1-82", d.as_hex());
assert_eq!(txt, Data::from_hex(d.as_hex()).as_string().unwrap());
}

Expand Down
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#[macro_export]
macro_rules! da {
($uni:expr, $loc:expr) => {
$uni.dataize(format!("{}", $loc).as_str())
$uni.dataize(format!("{}", $loc).as_str())
.expect(format!("Can't dataize {}", $loc).as_str())
};
}
17 changes: 15 additions & 2 deletions src/universe/dataize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl Universe {
.data
.clone()
.context(format!("There is no data in ν{}", id))?;
trace!(
"#dataize: data found in ν{} ({} bytes), all good",
id,
data.as_bytes().len()
);
Ok(data)
}

Expand All @@ -56,6 +61,7 @@ impl Universe {
let mut sectors = VecDeque::new();
loc.split('.').for_each(|k| sectors.push_back(k));
loop {
trace!("#find: at ν{}", v);
self.reconnect(v)?;
if let Some(k) = sectors.pop_front() {
if k.starts_with("ν") {
Expand Down Expand Up @@ -91,7 +97,13 @@ impl Universe {
{
Some(m) => {
let to = m(self, v)?;
trace!("#dataize({}, '{}'): atom returned {}", v, loc, to);
trace!(
"#find({}, '{}'): atom at ν{} returned {}",
v1,
loc,
v,
to
);
to
}
None => {
Expand All @@ -114,6 +126,7 @@ impl Universe {
break;
}
}
trace!("#find: found ν{} by '{}'", v1, loc);
Ok(v)
}

Expand Down Expand Up @@ -168,7 +181,7 @@ fn finds_root() -> Result<()> {
uni.data(0, Data::from_int(0))?;
uni.add(1)?;
uni.atom(1, "S/Q")?;
assert_eq!(uni.find(1, "Δ")?, 0);
assert_eq!(0, uni.find(1, "Δ")?);
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions tests/basic_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn dataizes_simple_gmi() -> Result<()> {
.arg("foo")
.assert()
.success()
.stdout("ff-ff\n");
.stdout("FF-FF\n");
Ok(())
}

Expand All @@ -71,7 +71,7 @@ fn dataizes_in_eoc_mode() -> Result<()> {
"
ADD('$ν1');
BIND('$ε1', 'ν0', '$ν1', 'foo');
DATA('$ν1', 'ff ff');
DATA('$ν1', 'ca fe');
"
.as_bytes(),
)?;
Expand All @@ -92,7 +92,7 @@ fn dataizes_in_eoc_mode() -> Result<()> {
.arg("foo")
.assert()
.success()
.stdout("ff-ff\n");
.stdout("CA-FE\n");
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#![deny(warnings)]

use simple_logger::SimpleLogger;
use log::LevelFilter;
use simple_logger::SimpleLogger;

#[ctor::ctor]
fn init() {
Expand Down
9 changes: 1 addition & 8 deletions tests/snippets_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ mod common;

use anyhow::{Context, Result};
use glob::glob;
use log::LevelFilter;
use reo::da;
use reo::universe::Universe;
use simple_logger::SimpleLogger;
use std::path::Path;

fn all_apps() -> Result<Vec<String>> {
Expand Down Expand Up @@ -54,11 +52,6 @@ fn all_apps() -> Result<Vec<String>> {
#[test]
#[ignore]
fn deploys_and_runs_all_apps() -> Result<()> {
SimpleLogger::new()
.with_level(LevelFilter::Trace)
.init()
.unwrap();

let relf = Path::new("target/snippets.relf");
assert_cmd::Command::cargo_bin("reo")?
.arg("compile")
Expand All @@ -67,7 +60,7 @@ fn deploys_and_runs_all_apps() -> Result<()> {
.assert()
.success();
let mut uni = Universe::load(relf)?;
println!("{}", uni.inspect("Q")?);
println!("{}", uni.inspect("Q.org.eolang.reo")?);
for app in all_apps()? {
let expected = da!(uni, format!("Φ.{}.expected", app));
let actual = da!(uni, format!("Φ.{}", app));
Expand Down

0 comments on commit bd8cc72

Please sign in to comment.