Skip to content

Commit

Permalink
Overload 'validate_control_cbor' based on serde features.
Browse files Browse the repository at this point in the history
  Turns out the inline 'cfg' macro doesn't work as the code is still included even if serde_cbor isn't required. The overloading fixes this.
  • Loading branch information
KtorZ committed Oct 1, 2023
1 parent ccfe319 commit 42a7651
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::value::Value;
use std::collections::BTreeMap; // used in Value::Map
use std::collections::HashMap;
use std::collections::VecDeque;
use std::convert::TryFrom;
use std::convert::TryInto;
use std::mem::discriminant;

Expand Down Expand Up @@ -1051,27 +1050,28 @@ fn validate_control(ctl: &Control, value: &Value, ctx: &Context) -> ValidateResu
}
}

fn validate_control_cbor(ctl_cbor: &CtlOpCbor, value: &Value, ctx: &Context) -> ValidateResult {
if cfg!(feature = "serde_cbor") {
use serde_cbor::Value as CBOR_Value;
#[cfg(not(feature = "serde_cbor"))]
fn validate_control_cbor(_ctl_cbor: &CtlOpCbor, _value: &Value, _ctx: &Context) -> ValidateResult {
Err(ValidateError::Unsupported(
"'.cbor' control operator; enable serde_cbor feature to support.".into(),
))
}

match value {
Value::Bytes(bytes) => {
let cbor_value: CBOR_Value = serde_cbor::from_slice(bytes)
.map_err(|e| ValidateError::ValueError(format!("{}", e)))?;
#[cfg(feature = "serde_cbor")]
fn validate_control_cbor(ctl_cbor: &CtlOpCbor, value: &Value, ctx: &Context) -> ValidateResult {
use serde_cbor::Value as CBOR_Value;
use std::convert::TryFrom;

let nested_value = Value::try_from(cbor_value)?;
match value {
Value::Bytes(bytes) => {
let cbor_value: CBOR_Value = serde_cbor::from_slice(bytes)
.map_err(|e| ValidateError::ValueError(format!("{}", e)))?;

validate(&nested_value, ctl_cbor.node.as_ref(), ctx)
}
_ => Err::<(), ValidateError>(mismatch("Bytes")),
}?;
let nested_value = Value::try_from(cbor_value)?;

Ok(())
} else {
Err(ValidateError::Unsupported(
"'.cbor' control operator; enable serde_cbor feature to support.".into(),
))
validate(&nested_value, ctl_cbor.node.as_ref(), ctx)
}
_ => Err::<(), ValidateError>(mismatch("Bytes")),
}
}

Expand Down

0 comments on commit 42a7651

Please sign in to comment.