Skip to content

Commit

Permalink
impl Display impl for Constant (primitive and composite types);
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Aug 15, 2023
1 parent 0b3d3a8 commit 8784394
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
12 changes: 8 additions & 4 deletions ergotree-interpreter/src/eval/deserialize_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ impl Evaluable for DeserializeContext {
fn eval(&self, env: &mut Env, ctx: &mut EvalContext) -> Result<Value, EvalError> {
match ctx.ctx.extension.values.get(&self.id) {
Some(c) => {
if c.tpe != SType::SColl(SType::SByte.into()) {
Err(EvalError::UnexpectedExpr(format!("DeserializeContext: expected extension value to have type SColl(SByte), got {:?}", c.tpe)))
let expected_tpe = SType::SColl(SType::SByte.into());
if c.tpe != expected_tpe {
Err(EvalError::UnexpectedExpr(format!(
"DeserializeContext: expected extension value to have type {:?} got {:?}",
expected_tpe, c.tpe
)))
} else {
let bytes = c.v.clone().try_extract_into::<Vec<u8>>()?;
let expr = Expr::sigma_parse_bytes(bytes.as_slice())?;
Expand All @@ -26,8 +30,8 @@ impl Evaluable for DeserializeContext {
}
}
None => Err(EvalError::NotFound(format!(
"DeserializeContext: no value with id {} in context extension",
self.id
"DeserializeContext: no value with id {} in context extension map {}",
self.id, ctx.ctx.extension
))),
}
}
Expand Down
2 changes: 1 addition & 1 deletion ergotree-interpreter/src/eval/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Evaluable for Expr {
)),
Expr::Collection(op) => op.eval(env, ctx),
Expr::ValDef(_) => Err(EvalError::UnexpectedExpr(
("ValDef is evaluated in BlockValue").to_string(),
("ValDef should be evaluated in BlockValue").to_string(),
)),
Expr::And(op) => op.eval(env, ctx),
Expr::Or(op) => op.eval(env, ctx),
Expand Down
5 changes: 2 additions & 3 deletions ergotree-interpreter/src/eval/get_var.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// use ergotree_ir::mir::constant::TryExtractInto;
use ergotree_ir::mir::constant::TryExtractFromError;
use ergotree_ir::mir::get_var::GetVar;
use ergotree_ir::mir::value::Value;
Expand All @@ -14,8 +13,8 @@ impl Evaluable for GetVar {
None => Ok(Value::Opt(None.into())),
Some(v) if v.tpe == self.var_tpe => Ok((Some(v.v.clone())).into()),
Some(v) => Err(TryExtractFromError(format!(
"GetVar: expected {:?}, found {:?}",
self.var_tpe, v.tpe
"GetVar: expected extension value id {} to have type {:?}, found {:?} in context extension map {}",
self.var_id, self.var_tpe, v, ctx.ctx.extension
))
.into()),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ergotree_ir::serialization::SigmaSerializable;
use ergotree_ir::serialization::SigmaSerializeResult;
use indexmap::IndexMap;
use std::convert::TryFrom;
use std::fmt;
use thiserror::Error;

/// User-defined variables to be put into context
Expand All @@ -25,6 +26,12 @@ impl ContextExtension {
}
}

impl fmt::Display for ContextExtension {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.values.iter()).finish()
}
}

impl SigmaSerializable for ContextExtension {
fn sigma_serialize<W: SigmaByteWrite>(&self, w: &mut W) -> SigmaSerializeResult {
w.put_u8(self.values.len() as u8)?;
Expand Down
63 changes: 63 additions & 0 deletions ergotree-ir/src/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ impl std::fmt::Debug for Constant {
}
}

impl std::fmt::Display for Constant {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.v.fmt(f)
}
}

impl std::fmt::Debug for Literal {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -112,6 +118,63 @@ impl std::fmt::Debug for Literal {
}
}

impl std::fmt::Display for Literal {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Literal::Coll(CollKind::NativeColl(NativeColl::CollByte(i8_bytes))) => {
write!(f, "Coll[Byte](");
for (i, b) in i8_bytes.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", b)?;
}
write!(f, ")")
}
Literal::Coll(CollKind::WrappedColl { elem_tpe, items }) => {
write!(f, "Coll[{}](", elem_tpe)?;

Check failure on line 135 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / clippy

`types::stype::SType` doesn't implement `std::fmt::Display`

error[E0277]: `types::stype::SType` doesn't implement `std::fmt::Display` --> ergotree-ir/src/mir/constant.rs:135:40 | 135 | write!(f, "Coll[{}](", elem_tpe)?; | ^^^^^^^^ `types::stype::SType` cannot be formatted with the default formatter | = help: the trait `std::fmt::Display` is not implemented for `types::stype::SType` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead = note: this error originates in the macro `$crate::format_args` which comes from the expansion of the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)

Check failure on line 135 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

`stype::SType` doesn't implement `std::fmt::Display`

Check failure on line 135 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Build without default features

`SType` doesn't implement `std::fmt::Display`

Check failure on line 135 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

`stype::SType` doesn't implement `std::fmt::Display`
for (i, item) in items.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
item.fmt(f)?;
}
write!(f, ")")
}
Literal::Opt(boxed_opt) => {
if let Some(v) = &**boxed_opt {
write!(f, "Some(")?;
v.fmt(f)?;
write!(f, ")")
} else {
write!(f, "None")
}
}
Literal::Tup(items) => {
write!(f, "(")?;
for (i, item) in items.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
item.fmt(f)?;
}
write!(f, ")")
}
Literal::Unit => write!(f, "()"),
Literal::Boolean(v) => v.fmt(f),
Literal::Byte(v) => v.fmt(f),
Literal::Short(v) => v.fmt(f),
Literal::Int(v) => v.fmt(f),
Literal::Long(v) => write!(f, "{}L", v),
Literal::BigInt(v) => v.fmt(f),
Literal::SigmaProp(v) => v.fmt(f),

Check failure on line 170 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / clippy

the method `fmt` exists for reference `&std::boxed::Box<sigma_protocol::sigma_boolean::SigmaProp>`, but its trait bounds were not satisfied

error[E0599]: the method `fmt` exists for reference `&std::boxed::Box<sigma_protocol::sigma_boolean::SigmaProp>`, but its trait bounds were not satisfied --> ergotree-ir/src/mir/constant.rs:170:40 | 170 | Literal::SigmaProp(v) => v.fmt(f), | ^^^ method cannot be called on `&std::boxed::Box<sigma_protocol::sigma_boolean::SigmaProp>` due to unsatisfied trait bounds | ::: ergotree-ir/src/sigma_protocol/sigma_boolean.rs:251:1 | 251 | pub struct SigmaProp(SigmaBoolean); | -------------------- doesn't satisfy `_: std::fmt::Display` | = note: the following trait bounds were not satisfied: `sigma_protocol::sigma_boolean::SigmaProp: std::fmt::Display` which is required by `std::boxed::Box<sigma_protocol::sigma_boolean::SigmaProp>: std::fmt::Display` `std::boxed::Box<sigma_protocol::sigma_boolean::SigmaProp>: std::fmt::Display` which is required by `&std::boxed::Box<sigma_protocol::sigma_boolean::SigmaProp>: std::fmt::Display` note: the following trait must be implemented = help: items from traits can only be used if the trait is in scope help: the following traits are implemented but not in scope; perhaps add a `use` for one of them: | 3 | use crate::chain::address::_::_serde::de::Expected; | 3 | use std::fmt::Binary; | 3 | use std::fmt::Debug; | 3 | use std::fmt::Display; | and 6 other candidates

Check failure on line 170 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

the method `fmt` exists for reference `&Box<sigma_boolean::SigmaProp>`, but its trait bounds were not satisfied

Check failure on line 170 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Build without default features

the method `fmt` exists for reference `&Box<SigmaProp>`, but its trait bounds were not satisfied

Check failure on line 170 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

the method `fmt` exists for reference `&Box<sigma_boolean::SigmaProp>`, but its trait bounds were not satisfied
Literal::GroupElement(v) => v.fmt(f),

Check failure on line 171 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / clippy

the method `fmt` exists for reference `&std::boxed::Box<ergo_chain_types::EcPoint>`, but its trait bounds were not satisfied

error[E0599]: the method `fmt` exists for reference `&std::boxed::Box<ergo_chain_types::EcPoint>`, but its trait bounds were not satisfied --> ergotree-ir/src/mir/constant.rs:171:43 | 171 | Literal::GroupElement(v) => v.fmt(f), | ^^^ method cannot be called on `&std::boxed::Box<ergo_chain_types::EcPoint>` due to unsatisfied trait bounds | ::: /home/runner/work/sigma-rust/sigma-rust/ergo-chain-types/src/ec_point.rs:18:1 | 18 | pub struct EcPoint(ProjectivePoint); | ------------------ doesn't satisfy `ergo_chain_types::EcPoint: std::fmt::Display` | = note: the following trait bounds were not satisfied: `ergo_chain_types::EcPoint: std::fmt::Display` which is required by `std::boxed::Box<ergo_chain_types::EcPoint>: std::fmt::Display` `std::boxed::Box<ergo_chain_types::EcPoint>: std::fmt::Display` which is required by `&std::boxed::Box<ergo_chain_types::EcPoint>: std::fmt::Display` = help: items from traits can only be used if the trait is in scope help: the following traits are implemented but not in scope; perhaps add a `use` for one of them: | 3 | use crate::chain::address::_::_serde::de::Expected; | 3 | use std::fmt::Binary; | 3 | use std::fmt::Debug; | 3 | use std::fmt::Display; | and 6 other candidates

Check failure on line 171 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

the method `fmt` exists for reference `&Box<ergo_chain_types::EcPoint>`, but its trait bounds were not satisfied

Check failure on line 171 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Build without default features

the method `fmt` exists for reference `&Box<EcPoint>`, but its trait bounds were not satisfied

Check failure on line 171 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

the method `fmt` exists for reference `&Box<ergo_chain_types::EcPoint>`, but its trait bounds were not satisfied
Literal::AvlTree(v) => v.fmt(f),

Check failure on line 172 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / clippy

the method `fmt` exists for reference `&std::boxed::Box<mir::avl_tree_data::AvlTreeData>`, but its trait bounds were not satisfied

error[E0599]: the method `fmt` exists for reference `&std::boxed::Box<mir::avl_tree_data::AvlTreeData>`, but its trait bounds were not satisfied --> ergotree-ir/src/mir/constant.rs:172:38 | 172 | Literal::AvlTree(v) => v.fmt(f), | ^^^ method cannot be called on `&std::boxed::Box<mir::avl_tree_data::AvlTreeData>` due to unsatisfied trait bounds | ::: ergotree-ir/src/mir/avl_tree_data.rs:59:1 | 59 | pub struct AvlTreeData { | ---------------------- doesn't satisfy `mir::avl_tree_data::AvlTreeData: std::fmt::Display` | = note: the following trait bounds were not satisfied: `mir::avl_tree_data::AvlTreeData: std::fmt::Display` which is required by `std::boxed::Box<mir::avl_tree_data::AvlTreeData>: std::fmt::Display` `std::boxed::Box<mir::avl_tree_data::AvlTreeData>: std::fmt::Display` which is required by `&std::boxed::Box<mir::avl_tree_data::AvlTreeData>: std::fmt::Display` note: the following trait must be implemented = help: items from traits can only be used if the trait is in scope help: the following traits are implemented but not in scope; perhaps add a `use` for one of them: | 3 | use crate::chain::address::_::_serde::de::Expected; | 3 | use std::fmt::Binary; | 3 | use std::fmt::Debug; | 3 | use std::fmt::Display; | and 6 other candidates

Check failure on line 172 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

the method `fmt` exists for reference `&Box<avl_tree_data::AvlTreeData>`, but its trait bounds were not satisfied

Check failure on line 172 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Build without default features

the method `fmt` exists for reference `&Box<AvlTreeData>`, but its trait bounds were not satisfied

Check failure on line 172 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

the method `fmt` exists for reference `&Box<avl_tree_data::AvlTreeData>`, but its trait bounds were not satisfied
Literal::CBox(v) => v.fmt(f),

Check failure on line 173 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / clippy

the method `fmt` exists for reference `&std::sync::Arc<chain::ergo_box::ErgoBox>`, but its trait bounds were not satisfied

error[E0599]: the method `fmt` exists for reference `&std::sync::Arc<chain::ergo_box::ErgoBox>`, but its trait bounds were not satisfied --> ergotree-ir/src/mir/constant.rs:173:35 | 173 | Literal::CBox(v) => v.fmt(f), | ^^^ method cannot be called on `&std::sync::Arc<chain::ergo_box::ErgoBox>` due to unsatisfied trait bounds | ::: ergotree-ir/src/chain/ergo_box.rs:60:1 | 60 | pub struct ErgoBox { | ------------------ doesn't satisfy `chain::ergo_box::ErgoBox: std::fmt::Display` | = note: the following trait bounds were not satisfied: `chain::ergo_box::ErgoBox: std::fmt::Display` which is required by `std::sync::Arc<chain::ergo_box::ErgoBox>: std::fmt::Display` `std::sync::Arc<chain::ergo_box::ErgoBox>: std::fmt::Display` which is required by `&std::sync::Arc<chain::ergo_box::ErgoBox>: std::fmt::Display` note: the following trait must be implemented = help: items from traits can only be used if the trait is in scope help: the following traits are implemented but not in scope; perhaps add a `use` for one of them: | 3 | use crate::chain::address::_::_serde::de::Expected; | 3 | use std::fmt::Binary; | 3 | use std::fmt::Debug; | 3 | use std::fmt::Display; | and 6 other candidates

Check failure on line 173 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

the method `fmt` exists for reference `&Arc<chain::ergo_box::ErgoBox>`, but its trait bounds were not satisfied

Check failure on line 173 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Build without default features

the method `fmt` exists for reference `&Arc<ErgoBox>`, but its trait bounds were not satisfied

Check failure on line 173 in ergotree-ir/src/mir/constant.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

the method `fmt` exists for reference `&Arc<chain::ergo_box::ErgoBox>`, but its trait bounds were not satisfied
}
}
}

impl From<()> for Literal {
fn from(_: ()) -> Literal {
Literal::Unit
Expand Down

0 comments on commit 8784394

Please sign in to comment.