git.delta.rocks / jrsonnet / refs/commits / 87ee0653180e

difftreelog

feat prettier error display for empty strings

Yaroslav Bolyukin2022-06-05parent: #42a0a06.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
35 out35 out
36}36}
37
38const fn format_empty_str(str: &str) -> &str {
39 if str.is_empty() {
40 "\"\" (empty string)"
41 } else {
42 str
43 }
44}
3745
38#[derive(Error, Debug, Clone, Trace)]46#[derive(Error, Debug, Clone, Trace)]
39pub enum Error {47pub enum Error {
60 #[error("string out of bounds: {0} is not within [0,{1})")]68 #[error("string out of bounds: {0} is not within [0,{1})")]
61 StringBoundsError(usize, usize),69 StringBoundsError(usize, usize),
6270
63 #[error("assert failed: {0}")]71 #[error("assert failed: {}", format_empty_str(.0))]
64 AssertionFailed(IStr),72 AssertionFailed(IStr),
6573
66 #[error("variable is not defined: {0}{}", format_found(.1, "variable"))]74 #[error("variable is not defined: {0}{}", format_found(.1, "variable"))]
7078
71 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]79 #[error("type mismatch: expected {}, got {2} {0}", .1.iter().map(|e| format!("{}", e)).collect::<Vec<_>>().join(", "))]
72 TypeMismatch(&'static str, Vec<ValType>, ValType),80 TypeMismatch(&'static str, Vec<ValType>, ValType),
73 #[error("no such field: {0}{}", format_found(.1, "field"))]81 #[error("no such field: {}{}", format_empty_str(.0), format_found(.1, "field"))]
74 NoSuchField(IStr, Vec<IStr>),82 NoSuchField(IStr, Vec<IStr>),
7583
76 #[error("only functions can be called, got {0}")]84 #[error("only functions can be called, got {0}")]
8997
90 #[error("field name should be string, got {0}")]98 #[error("field name should be string, got {0}")]
91 FieldMustBeStringGot(ValType),99 FieldMustBeStringGot(ValType),
92 #[error("duplicate field name: {0}")]100 #[error("duplicate field name: {}", format_empty_str(.0))]
93 DuplicateFieldName(IStr),101 DuplicateFieldName(IStr),
94102
95 #[error("attempted to index array with string {0}")]103 #[error("attempted to index array with string {}", format_empty_str(.0))]
96 AttemptedIndexAnArrayWithString(IStr),104 AttemptedIndexAnArrayWithString(IStr),
97 #[error("{0} index type should be {1}, got {2}")]105 #[error("{0} index type should be {1}, got {2}")]
98 ValueIndexMustBeTypeGot(ValType, ValType, ValType),106 ValueIndexMustBeTypeGot(ValType, ValType, ValType),
129 error: Box<jrsonnet_parser::ParseError>,137 error: Box<jrsonnet_parser::ParseError>,
130 },138 },
131139
132 #[error("runtime error: {0}")]140 #[error("runtime error: {}", format_empty_str(.0))]
133 RuntimeError(IStr),141 RuntimeError(IStr),
134 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]142 #[error("stack overflow, try to reduce recursion, or set --max-stack to bigger value")]
135 StackOverflow,143 StackOverflow,
152 #[error("stream manifest output cannot consist of raw strings")]160 #[error("stream manifest output cannot consist of raw strings")]
153 StreamManifestCannotNestString,161 StreamManifestCannotNestString,
154162
155 #[error("{0}")]163 #[error("{}", format_empty_str(.0))]
156 ImportCallbackError(String),164 ImportCallbackError(String),
157 #[error("invalid unicode codepoint: {0}")]165 #[error("invalid unicode codepoint: {0}")]
158 InvalidUnicodeCodepointGot(u32),166 InvalidUnicodeCodepointGot(u32),