Skip to content

Commit eceac55

Browse files
authored
fix(rust/catalyst-types): add conversion error in problem report (#143)
* fix(catalyst-types): add conversion error in problem report Signed-off-by: bkioshn <[email protected]> * fix(catalyst-types): linter and format Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]>
1 parent 768ecd5 commit eceac55

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

rust/catalyst-types/src/problem_report.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ enum Kind {
5555
/// Additional information about the duplicate field.
5656
description: String,
5757
},
58+
/// Conversion error.
59+
ConversionError {
60+
/// The field that failed to convert
61+
field: String,
62+
/// The value that failed to convert
63+
value: String,
64+
/// The type that the value was expected to convert to
65+
expected_type: String,
66+
},
5867
/// An uncategorized problem was encountered. Use only for rare problems, otherwise
5968
/// make a new problem kind.
6069
Other {
@@ -378,6 +387,42 @@ impl ProblemReport {
378387
);
379388
}
380389

390+
/// Reports a conversion error.
391+
///
392+
/// This method adds an entry to the problem report indicating that a field failed to
393+
/// convert to the expected type, along with the value that failed to convert and
394+
/// the expected type.
395+
///
396+
/// # Arguments
397+
///
398+
/// * `field`: A string slice representing the field that failed to convert.
399+
/// * `value`: A string slice representing the value that failed to convert.
400+
/// * `expected_type`: A string slice representing the type that the value was
401+
/// expected to convert to.
402+
///
403+
/// # Example
404+
///
405+
/// ```rust
406+
/// use catalyst_types::problem_report::ProblemReport;
407+
/// let report = ProblemReport::new("RBAC Registration Decoding");
408+
/// report.conversion_error(
409+
/// "address bytes",
410+
/// "[1, 2, 3, 4]",
411+
/// "Address",
412+
/// "RBAC stake address",
413+
/// );
414+
/// ```
415+
pub fn conversion_error(&self, field: &str, value: &str, expected_type: &str, context: &str) {
416+
self.add_entry(
417+
Kind::ConversionError {
418+
field: field.to_owned(),
419+
value: value.to_owned(),
420+
expected_type: expected_type.to_owned(),
421+
},
422+
context,
423+
);
424+
}
425+
381426
/// Reports an uncategorized problem with the given description and context.
382427
///
383428
/// This method is intended for use in rare situations where a specific type of

0 commit comments

Comments
 (0)