Skip to content

Commit 4149bca

Browse files
committed
Add VecDisplay wrapper to display Vec values
1 parent 5637121 commit 4149bca

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

lib/src/common/collections.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Display;
2+
use itertools::Itertools;
23

34
pub trait Equality<T> {
45
fn eq(a: &T, b: &T) -> bool;
@@ -288,6 +289,14 @@ impl<'a, T> Into<&'a [T]> for &'a CowArray<T> {
288289
}
289290
}
290291

292+
pub struct VecDisplay<'a, T: 'a + Display>(pub &'a Vec<T>);
293+
294+
impl<'a, T: 'a + Display> Display for VecDisplay<'a, T> {
295+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
296+
write!(f, "[{}]", self.0.iter().format(", "))
297+
}
298+
}
299+
291300

292301
#[cfg(test)]
293302
mod test {

lib/src/metta/runner/stdlib/debug.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@ use crate::*;
22
use crate::metta::*;
33
use crate::metta::text::Tokenizer;
44
use crate::space::*;
5-
use crate::common::collections::{Equality, DefaultEquality};
5+
use crate::common::collections::{VecDisplay, Equality, DefaultEquality};
66
use crate::common::assert::compare_vec_no_order;
77
use crate::atom::matcher::atoms_are_equivalent;
88
use crate::metta::runner::stdlib::{grounded_op, atom_to_string, regex, interpret_no_error, unit_result};
99
use crate::metta::runner::bool::*;
1010

1111
use std::convert::TryInto;
12-
use itertools::Itertools;
13-
1412

1513
fn assert_results_equal(actual: &Vec<Atom>, expected: &Vec<Atom>) -> Result<Vec<Atom>, ExecError> {
16-
let report = format!("\nExpected: [{}]\nGot: [{}]", expected.iter().format(", "), actual.iter().format(", "));
14+
let report = format!("\nExpected: {}\nGot: {}", VecDisplay(expected), VecDisplay(actual));
1715
match compare_vec_no_order(actual.iter(), expected.iter(), DefaultEquality{}).as_display() {
1816
None => unit_result(),
1917
Some(diff) => Err(ExecError::Runtime(format!("{}\n{}", report, diff)))
@@ -115,7 +113,7 @@ impl Equality<&Atom> for AlphaEquality {
115113
}
116114

117115
fn assert_alpha_equal(actual: &Vec<Atom>, expected: &Vec<Atom>) -> Result<Vec<Atom>, ExecError> {
118-
let report = format!("\nExpected: [{}]\nGot: [{}]", expected.iter().format(", "), actual.iter().format(", "));
116+
let report = format!("\nExpected: {}\nGot: {}", VecDisplay(expected), VecDisplay(actual));
119117
let res = compare_vec_no_order(actual.iter(), expected.iter(), AlphaEquality{});
120118
match res.as_display() {
121119
None => unit_result(),

repl/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ signal-hook = "0.3.17"
1414
pyo3 = { version = "0.19.2", features = ["auto-initialize"], optional = true }
1515
pep440_rs = { version = "0.3.11", optional = true }
1616
hyperon = { workspace = true, optional = true } #TODO: We can only link Hyperon directly or through Python, but not both at the same time. The right fix is to allow HyperonPy to be built within Hyperon, See https://github.com/trueagi-io/hyperon-experimental/issues/283
17-
itertools = "0.13.0"
1817

1918
[[bin]]
2019
name = "metta-repl"

repl/src/metta_shim.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ pub mod metta_interface_mod {
3737
use pep440_rs::{parse_version_specifiers, Version};
3838
use pyo3::prelude::*;
3939
use pyo3::types::{PyTuple, PyString, PyBool, PyList, PyDict};
40+
use hyperon::common::collections::VecDisplay;
4041
use super::{strip_quotes, exec_state_prepare, exec_state_should_break};
41-
use itertools::Itertools;
4242

4343
/// Load the hyperon module, and get the "__version__" attribute
4444
pub fn get_hyperonpy_version() -> Result<String, String> {
@@ -167,7 +167,7 @@ pub mod metta_interface_mod {
167167
Python::with_gil(|py| -> PyResult<()> {
168168
for result_vec in self.result.iter() {
169169
let result_vec: Vec<&PyAny> = result_vec.iter().map(|atom| atom.as_ref(py)).collect();
170-
println!("[{}]", result_vec.iter().format(", "));
170+
println!("{}", VecDisplay(&result_vec));
171171
}
172172
Ok(())
173173
}).unwrap()
@@ -319,8 +319,8 @@ pub mod metta_interface_mod {
319319
use hyperon::ExpressionAtom;
320320
use hyperon::Atom;
321321
use hyperon::metta::runner::{Metta, RunnerState, Environment, EnvBuilder};
322+
use hyperon::common::collections::VecDisplay;
322323
use super::{strip_quotes, exec_state_prepare, exec_state_should_break};
323-
use itertools::Itertools;
324324

325325
pub use hyperon::metta::text::SyntaxNodeType as SyntaxNodeType;
326326

@@ -420,7 +420,7 @@ pub mod metta_interface_mod {
420420

421421
pub fn print_result(&self) {
422422
for result in self.result.iter() {
423-
println!("[{}]", result.iter().format(", "));
423+
println!("{}", VecDisplay(result));
424424
}
425425
}
426426

0 commit comments

Comments
 (0)