Skip to content

Commit

Permalink
refactor: flatten if
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Apr 10, 2024
1 parent 9f41b84 commit a932bca
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,37 +474,39 @@ impl<'v, 's, 'd, 'e> Validator<'v, 's, 'd, 'e> {
}
}

if s.draft_version >= 7 {
// contentEncoding --
let mut decoded = Cow::from(str.as_bytes());
if let Some(decoder) = &s.content_encoding {
match (decoder.func)(str) {
Ok(bytes) => decoded = Cow::from(bytes),
Err(err) => self.add_error(ErrorKind::ContentEncoding {
want: decoder.name,
err,
}),
}
if s.draft_version == 6 {
return;
}

// contentEncoding --
let mut decoded = Cow::from(str.as_bytes());
if let Some(decoder) = &s.content_encoding {
match (decoder.func)(str) {
Ok(bytes) => decoded = Cow::from(bytes),
Err(err) => self.add_error(ErrorKind::ContentEncoding {
want: decoder.name,
err,
}),
}
}

// contentMediaType --
let mut deserialized = None;
if let Some(mt) = &s.content_media_type {
match (mt.func)(decoded.as_ref(), s.content_schema.is_some()) {
Ok(des) => deserialized = des,
Err(e) => {
self.add_error(kind!(ContentMediaType, decoded.into(), mt.name, e));
}
// contentMediaType --
let mut deserialized = None;
if let Some(mt) = &s.content_media_type {
match (mt.func)(decoded.as_ref(), s.content_schema.is_some()) {
Ok(des) => deserialized = des,
Err(e) => {
self.add_error(kind!(ContentMediaType, decoded.into(), mt.name, e));
}
}
}

// contentSchema --
if let (Some(sch), Some(v)) = (s.content_schema, deserialized) {
if let Err(mut e) = self.schemas.validate(&v, sch) {
e.schema_url = &s.loc;
e.kind = kind!(ContentSchema);
self.errors.push(e.clone_static());
}
// contentSchema --
if let (Some(sch), Some(v)) = (s.content_schema, deserialized) {
if let Err(mut e) = self.schemas.validate(&v, sch) {
e.schema_url = &s.loc;
e.kind = kind!(ContentSchema);
self.errors.push(e.clone_static());
}
}
}
Expand Down

0 comments on commit a932bca

Please sign in to comment.