Skip to content

Commit 8c0be15

Browse files
authored
feat(rust/catalyst-types): Add getters to ProblemReport and its Entrys (#549)
* feat: initial * chore: lintfix * chore: iterator * chore: lintfix
1 parent 9b04d25 commit 8c0be15

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

rust/catalyst-types/src/problem_report.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@
66
77
use std::sync::Arc;
88

9-
use orx_concurrent_vec::ConcurrentVec;
10-
use serde::Serialize;
9+
use orx_concurrent_vec::{ConcurrentElement, ConcurrentVec};
1110

1211
/// The kind of problem being reported
13-
#[derive(Debug, Serialize, Clone)]
14-
#[serde(tag = "type")]
15-
enum Kind {
12+
#[derive(Debug, Clone)]
13+
pub enum Kind {
1614
/// Expected and Required field is missing
1715
MissingField {
1816
/// Name of the missing field
@@ -73,21 +71,35 @@ enum Kind {
7371
}
7472

7573
/// Problem Report Entry
76-
#[derive(Debug, Serialize, Clone)]
77-
struct Entry {
74+
#[derive(Debug, Clone)]
75+
pub struct Entry {
7876
/// The kind of problem we are recording.
7977
kind: Kind,
8078
/// Any extra context information we want to add.
8179
context: String,
8280
}
8381

82+
impl Entry {
83+
/// Gets the kind of the problem of the entry.
84+
#[must_use]
85+
pub fn kind(&self) -> &Kind {
86+
&self.kind
87+
}
88+
89+
/// Gets extra information of the entry.
90+
#[must_use]
91+
pub fn context(&self) -> &String {
92+
&self.context
93+
}
94+
}
95+
8496
/// The Problem Report list
85-
#[derive(Debug, Clone, Serialize)]
86-
struct Report(ConcurrentVec<Entry>);
97+
#[derive(Debug, Clone)]
98+
pub struct Report(ConcurrentVec<Entry>);
8799

88100
/// An inner state of the report.
89-
#[derive(Debug, Serialize)]
90-
struct State {
101+
#[derive(Debug)]
102+
pub struct State {
91103
/// What context does the whole report have
92104
context: String,
93105
/// The report itself
@@ -97,7 +109,7 @@ struct State {
97109
/// Problem Report.
98110
///
99111
/// This structure allows making a cheap copies that share the same state.
100-
#[derive(Debug, Clone, Serialize)]
112+
#[derive(Debug, Clone)]
101113
pub struct ProblemReport(Arc<State>);
102114

103115
impl ProblemReport {
@@ -147,6 +159,17 @@ impl ProblemReport {
147159
!self.0.report.0.is_empty()
148160
}
149161

162+
/// Gets entries from the report.
163+
pub fn entries(&self) -> impl Iterator<Item = &ConcurrentElement<Entry>> {
164+
self.0.report.0.iter()
165+
}
166+
167+
/// Gets context from the report.
168+
#[must_use]
169+
pub fn context(&self) -> &String {
170+
&self.0.context
171+
}
172+
150173
/// Add an entry to the report
151174
fn add_entry(
152175
&self,
@@ -500,14 +523,4 @@ mod tests {
500523
// The original report must have the same (problematic) state.
501524
assert!(original.is_problematic());
502525
}
503-
504-
#[test]
505-
fn serialize() {
506-
let report = ProblemReport::new("top level context");
507-
report.invalid_value("field name", "found", "constraint", "context");
508-
509-
let serialized = serde_json::to_string(&report).unwrap();
510-
let expected = r#"{"context":"top level context","report":[{"kind":{"type":"InvalidValue","field":"field name","value":"found","constraint":"constraint"},"context":"context"}]}"#;
511-
assert_eq!(serialized, expected);
512-
}
513526
}

0 commit comments

Comments
 (0)