Show slightly better schema mismatch errors

Display terms as CBOR diagnostic when they are Plutus data.
This commit is contained in:
KtorZ 2023-04-07 16:21:33 +02:00
parent 176cb45524
commit c18deecdc8
No known key found for this signature in database
GPG Key ID: 33173CB6F77F4277
3 changed files with 15 additions and 3 deletions

1
Cargo.lock generated vendored
View File

@ -129,6 +129,7 @@ dependencies = [
"indexmap",
"itertools",
"miette",
"minicbor",
"owo-colors",
"pallas",
"pallas-traverse",

View File

@ -20,6 +20,7 @@ ignore = "0.4.20"
indexmap = "1.9.2"
itertools = "0.10.5"
miette = { version = "5.5.0", features = ["fancy"] }
minicbor = "0.19.1"
owo-colors = { version = "3.5.0", features = ["supports-colors"] }
pallas = "0.18.0"
pallas-traverse = "0.18.0"

View File

@ -4,6 +4,7 @@ use super::{
};
use aiken_lang::ast::Span;
use miette::{Diagnostic, NamedSource};
use minicbor as cbor;
use owo_colors::{OwoColorize, Stream::Stdout};
use std::fmt::Debug;
use uplc::ast::Constant;
@ -66,9 +67,18 @@ pub enum Error {
#[error("I caught a parameter application that seems off.")]
#[diagnostic(code("aiken::blueprint::apply::mismatch"))]
#[diagnostic(help(
"When applying parameters to a validator, I control that the shape of the parameter you give me matches what is specified in the blueprint. Unfortunately, schemas didn't match in this case.\n\nI am expecting something of the shape:\n\n{}Which I couldn't match against the following term:\n\n{}\n\nNote that this may only represent part of a bigger whole.",
serde_json::to_string_pretty(&schema).unwrap().if_supports_color(Stdout, |s| s.green()),
term.to_pretty().if_supports_color(Stdout, |s| s.red()),
"When applying parameters to a validator, I control that the shape of the parameter you give me matches what is specified in the blueprint. Unfortunately, schemas didn't match in this case.\n\nI am expecting something of the shape:\n\n{expected}Which I couldn't match against the following term:\n\n{term}\n\nNote that this may only represent part of a bigger whole.",
expected = serde_json::to_string_pretty(&schema).unwrap().if_supports_color(Stdout, |s| s.green()),
term = {
let mut buf = vec![];
match term {
Constant::Data(data) => {
cbor::encode(data, &mut buf).unwrap();
cbor::display(&buf).to_string()
},
_ => term.to_pretty()
}
}.if_supports_color(Stdout, |s| s.red()),
))]
SchemaMismatch { schema: Schema, term: Constant },