From c18deecdc8d077338d5f2e53a8bef8a082c1e896 Mon Sep 17 00:00:00 2001 From: KtorZ Date: Fri, 7 Apr 2023 16:21:33 +0200 Subject: [PATCH] Show slightly better schema mismatch errors Display terms as CBOR diagnostic when they are Plutus data. --- Cargo.lock | 1 + crates/aiken-project/Cargo.toml | 1 + crates/aiken-project/src/blueprint/error.rs | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ebcce565..462a5c33 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,7 @@ dependencies = [ "indexmap", "itertools", "miette", + "minicbor", "owo-colors", "pallas", "pallas-traverse", diff --git a/crates/aiken-project/Cargo.toml b/crates/aiken-project/Cargo.toml index fbc72bda..f972900a 100644 --- a/crates/aiken-project/Cargo.toml +++ b/crates/aiken-project/Cargo.toml @@ -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" diff --git a/crates/aiken-project/src/blueprint/error.rs b/crates/aiken-project/src/blueprint/error.rs index cb53916b..894ec7ed 100644 --- a/crates/aiken-project/src/blueprint/error.rs +++ b/crates/aiken-project/src/blueprint/error.rs @@ -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 },