Skip to content

Commit 9a50c85

Browse files
committed
ref: handled errors propperly
1 parent 2c17478 commit 9a50c85

5 files changed

Lines changed: 74 additions & 83 deletions

File tree

crates/lupus/src/engine.rs

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -125,97 +125,92 @@ mod tests {
125125
use crate::schema::{DecodeContext, EncodeContext, JsonSchema};
126126

127127
#[test]
128-
fn text_into_data_raw() {
128+
fn text_into_data_raw() -> Result<(), Box<dyn std::error::Error>> {
129129
let mut engine = Engine::new();
130130
engine.register(JsonCodec);
131131
engine.register(TextCodec);
132132

133133
let input = "This is just a long long string";
134-
let result = engine
135-
.convert(
136-
input.as_bytes(),
137-
Format::Text,
138-
Format::Json,
139-
&DecodeContext::default(),
140-
&EncodeContext::default(),
141-
)
142-
.unwrap();
134+
let result = engine.convert(
135+
input.as_bytes(),
136+
Format::Text,
137+
Format::Json,
138+
&DecodeContext,
139+
&EncodeContext::default(),
140+
)?;
143141

144142
assert_eq!(result, br#""This is just a long long string""#);
143+
Ok(())
145144
}
146145

147146
#[test]
148-
fn normalizing_text_to_data_wraps_the_text_as_a_string() {
147+
fn normalizing_text_to_data_wraps_the_text_as_a_string()
148+
-> Result<(), Box<dyn std::error::Error>> {
149149
let engine = Engine::new();
150150

151151
assert_eq!(
152-
engine
153-
.normalize_for_target(
154-
Artifact::Text("unstructured".to_string()),
155-
ArtifactKind::Data,
156-
)
157-
.unwrap(),
152+
engine.normalize_for_target(
153+
Artifact::Text("unstructured".to_string()),
154+
ArtifactKind::Data,
155+
)?,
158156
Artifact::Data(Data::String("unstructured".to_string()))
159157
);
158+
Ok(())
160159
}
161160

162161
#[test]
163-
fn json_xml_json_round_trip_does_not_add_formatting_text() {
162+
fn json_xml_json_round_trip_does_not_add_formatting_text()
163+
-> Result<(), Box<dyn std::error::Error>> {
164164
let mut engine = Engine::new();
165165
engine.register(JsonCodec);
166166
engine.register(XmlCodec);
167-
let decode_ctx = DecodeContext::default();
167+
let decode_ctx = DecodeContext;
168168
let encode_ctx = EncodeContext::default();
169169
let json =
170170
br#"{"user":{"@id":"7","email":["ada@example.com","ada@work.example"],"name":"Ada"}}"#;
171171

172-
let xml = engine
173-
.convert(json, Format::Json, Format::Xml, &decode_ctx, &encode_ctx)
174-
.unwrap();
172+
let xml = engine.convert(json, Format::Json, Format::Xml, &decode_ctx, &encode_ctx)?;
175173
assert_eq!(
176174
xml,
177175
br#"<user id="7"><email>ada@example.com</email><email>ada@work.example</email><name>Ada</name></user>"#
178176
);
179177

180-
let result = engine
181-
.convert(&xml, Format::Xml, Format::Json, &decode_ctx, &encode_ctx)
182-
.unwrap();
178+
let result = engine.convert(&xml, Format::Xml, Format::Json, &decode_ctx, &encode_ctx)?;
183179
assert_eq!(result, json);
180+
Ok(())
184181
}
185182

186183
#[test]
187-
fn codecs_pretty_print_structured_output_when_requested() {
184+
fn codecs_pretty_print_structured_output_when_requested()
185+
-> Result<(), Box<dyn std::error::Error>> {
188186
let mut engine = Engine::new();
189187
engine.register(JsonCodec);
190188
engine.register(XmlCodec);
191-
let decode_ctx = DecodeContext::default();
189+
let decode_ctx = DecodeContext;
192190
let encode_ctx = EncodeContext { pretty: true };
193191

194-
let xml = engine
195-
.convert(
196-
br#"{"user":{"name":"Ada","email":"ada@example.com"}}"#,
197-
Format::Json,
198-
Format::Xml,
199-
&decode_ctx,
200-
&encode_ctx,
201-
)
202-
.unwrap();
192+
let xml = engine.convert(
193+
br#"{"user":{"name":"Ada","email":"ada@example.com"}}"#,
194+
Format::Json,
195+
Format::Xml,
196+
&decode_ctx,
197+
&encode_ctx,
198+
)?;
203199
assert_eq!(
204200
xml,
205201
b"<user>\n <email>ada@example.com</email>\n <name>Ada</name>\n</user>"
206202
);
207203

208-
let json = engine
209-
.convert(&xml, Format::Xml, Format::Json, &decode_ctx, &encode_ctx)
210-
.unwrap();
204+
let json = engine.convert(&xml, Format::Xml, Format::Json, &decode_ctx, &encode_ctx)?;
211205
assert_eq!(
212206
json,
213207
b"{\n \"user\": {\n \"email\": \"ada@example.com\",\n \"name\": \"Ada\"\n }\n}"
214208
);
209+
Ok(())
215210
}
216211

217212
#[test]
218-
fn canonical_xml_array_converts_to_csv() {
213+
fn canonical_xml_array_converts_to_csv() -> Result<(), Box<dyn std::error::Error>> {
219214
let mut engine = Engine::new();
220215
engine.register(XmlCodec);
221216
engine.register(CsvCodec);
@@ -230,24 +225,24 @@ mod tests {
230225
</item>
231226
</data>"#;
232227

233-
let csv = engine
234-
.convert(
235-
xml,
236-
Format::Xml,
237-
Format::Csv,
238-
&DecodeContext::default(),
239-
&EncodeContext::default(),
240-
)
241-
.unwrap();
228+
let csv = engine.convert(
229+
xml,
230+
Format::Xml,
231+
Format::Csv,
232+
&DecodeContext,
233+
&EncodeContext::default(),
234+
)?;
242235

243236
assert_eq!(
244237
csv,
245238
b"email,name\nada@example.com,Ada Lovelace\ngrace@example.com,Grace Hopper\n"
246239
);
240+
Ok(())
247241
}
248242

249243
#[test]
250-
fn validate_decodes_xml_before_applying_json_schema() {
244+
fn validate_decodes_xml_before_applying_json_schema() -> Result<(), Box<dyn std::error::Error>>
245+
{
251246
let mut engine = Engine::new();
252247
engine.register(XmlCodec);
253248
let schema = JsonSchema {
@@ -268,13 +263,12 @@ mod tests {
268263
.to_string(),
269264
};
270265

271-
engine
272-
.validate(
273-
b"<user><name>Ada</name></user>",
274-
Format::Xml,
275-
&schema,
276-
&DecodeContext::default(),
277-
)
278-
.unwrap();
266+
engine.validate(
267+
b"<user><name>Ada</name></user>",
268+
Format::Xml,
269+
&schema,
270+
&DecodeContext,
271+
)?;
272+
Ok(())
279273
}
280274
}

crates/lupus/src/formats/csv.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,10 @@ mod tests {
185185
use super::{parse_csv, write_csv};
186186

187187
#[test]
188-
fn csv_round_trips_quoted_flat_rows() {
188+
fn csv_round_trips_quoted_flat_rows() -> Result<(), Box<dyn std::error::Error>> {
189189
let input = "email,name\nada@example.com,\"Ada, Countess\"\n";
190-
let data = parse_csv(input).unwrap();
191-
assert_eq!(write_csv(&data).unwrap(), input);
190+
let data = parse_csv(input)?;
191+
assert_eq!(write_csv(&data)?, input);
192+
Ok(())
192193
}
193194
}

crates/lupus/src/formats/http_form.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,12 @@ mod tests {
130130
use crate::schema::{DecodeContext, EncodeContext};
131131

132132
#[test]
133-
fn form_round_trips_flat_string_objects() {
133+
fn form_round_trips_flat_string_objects() -> Result<(), Box<dyn std::error::Error>> {
134134
let codec = HttpFormCodec;
135135
let encoded = b"email=grace%40example.com&name=Grace+Hopper";
136-
let artifact = codec.decode(encoded, &DecodeContext::default()).unwrap();
136+
let artifact = codec.decode(encoded, &DecodeContext)?;
137137
assert!(matches!(artifact, Artifact::Data(Data::Object(_))));
138-
assert_eq!(
139-
codec.encode(&artifact, &EncodeContext::default()).unwrap(),
140-
encoded
141-
);
138+
assert_eq!(codec.encode(&artifact, &EncodeContext::default())?, encoded);
139+
Ok(())
142140
}
143141
}

crates/lupus/src/formats/protobuf.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ mod tests {
129129
use crate::schema::{DecodeContext, EncodeContext};
130130

131131
#[test]
132-
fn protobuf_value_object_round_trips_nested_data() {
132+
fn protobuf_value_object_round_trips_nested_data() -> Result<(), Box<dyn std::error::Error>> {
133133
let data = Data::Object(BTreeMap::from([(
134134
"user".to_string(),
135135
Data::Object(BTreeMap::from([
@@ -142,13 +142,12 @@ mod tests {
142142
])),
143143
)]));
144144
let codec = ProtobufCodec;
145-
let encoded = codec
146-
.encode(&Artifact::Data(data.clone()), &EncodeContext::default())
147-
.unwrap();
148-
let decoded = codec.decode(&encoded, &DecodeContext::default()).unwrap();
145+
let encoded = codec.encode(&Artifact::Data(data.clone()), &EncodeContext::default())?;
146+
let decoded = codec.decode(&encoded, &DecodeContext)?;
149147
assert_eq!(decoded, Artifact::Data(data));
150-
let rendered = String::from_utf8(encoded).unwrap();
148+
let rendered = String::from_utf8(encoded)?;
151149
assert!(rendered.contains("\"structValue\""));
152150
assert!(rendered.contains("\"stringValue\":\"Ada\""));
151+
Ok(())
153152
}
154153
}

crates/lupus/src/formats/xml.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,10 @@ mod tests {
408408
use super::*;
409409

410410
#[test]
411-
fn xml_codec_decodes_and_encodes_elements() {
411+
fn xml_codec_decodes_and_encodes_elements() -> Result<(), Box<dyn std::error::Error>> {
412412
let codec = XmlCodec;
413413
let ctx = DecodeContext;
414-
let artifact = codec
415-
.decode(br#"<user id="7"><name>Ada</name></user>"#, &ctx)
416-
.unwrap();
414+
let artifact = codec.decode(br#"<user id="7"><name>Ada</name></user>"#, &ctx)?;
417415

418416
let Artifact::Markup(markup) = artifact else {
419417
panic!("expected markup");
@@ -434,17 +432,18 @@ mod tests {
434432
})],
435433
})
436434
);
435+
Ok(())
437436
}
438437

439438
#[test]
440-
fn xml_codec_preserves_comments_when_encoding_markup() {
439+
fn xml_codec_preserves_comments_when_encoding_markup() -> Result<(), Box<dyn std::error::Error>>
440+
{
441441
let codec = XmlCodec;
442442
let ctx = DecodeContext;
443-
let artifact = codec.decode(b"<root><!--x--></root>", &ctx).unwrap();
444-
let encoded = codec
445-
.encode(&artifact, &EncodeContext { pretty: false })
446-
.unwrap();
443+
let artifact = codec.decode(b"<root><!--x--></root>", &ctx)?;
444+
let encoded = codec.encode(&artifact, &EncodeContext { pretty: false })?;
447445

448-
assert_eq!(String::from_utf8(encoded).unwrap(), "<root><!--x--></root>");
446+
assert_eq!(String::from_utf8(encoded)?, "<root><!--x--></root>");
447+
Ok(())
449448
}
450449
}

0 commit comments

Comments
 (0)