Skip to content

Commit

Permalink
don’t use debug output is Display impl for CborValue
Browse files Browse the repository at this point in the history
fixes #14
  • Loading branch information
rkuhn committed Oct 19, 2021
1 parent 43a2459 commit e30ba80
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions src/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,12 @@ fn canonicalise_dict<'a>(bytes: &'a [u8], builder: &mut DictWriter) -> Option<&'
builder.with_key(key, |b| b.write_neg(neg, tags));
}
MAJOR_BYTES => {
let decoded = update(bytes, value_bytes(b, false))?;
if tags.single() == Some(TAG_CBOR_ITEM) {
// drop CBOR item encoding wrapper - may choose to use these later for more efficient skipping
let decoded = update(bytes, value_bytes(b, false))?;
// the line above has advanced the main loop’s reference, here we advance a temporary one
one(&mut decoded.as_ref(), key, builder)?
} else {
let value = update(bytes, value_bytes(b, false))?;
builder.with_key(key, |b| b.write_bytes(value.as_ref(), tags));
builder.with_key(key, |b| b.write_bytes(decoded.as_ref(), tags));
}
}
MAJOR_STR => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<'a> Cbor<'a> {

impl<'a> AsRef<[u8]> for Cbor<'a> {
fn as_ref(&self) -> &[u8] {
&self.0
self.0
}
}

Expand Down
15 changes: 13 additions & 2 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> Display for ValueKind<'a> {
if *x == 0f64 && x.is_sign_negative() {
write!(f, "-0.0")
} else {
write!(f, "{:?}", x)
write!(f, "{}", x)
}
}
Str(s) => write!(f, "\"{}\"", s.escape_debug()),
Expand Down Expand Up @@ -408,10 +408,21 @@ fn bytes_to_float(bytes: &[u8]) -> f64 {

#[cfg(test)]
mod tests {
use crate::CborOwned;
use crate::{CborBuilder, CborOwned, Encoder};

use super::*;

#[test]
fn display() {
fn to_cbor_str(f: f64) -> String {
format!("{}", CborBuilder::new().encode_f64(f))
}
assert_eq!(to_cbor_str(1.0), "1");
assert_eq!(to_cbor_str(-1.1), "-1.1");
assert_eq!(to_cbor_str(0.0), "0");
assert_eq!(to_cbor_str(-0.0), "-0.0");
}

#[test]
fn base64string() {
fn to_cbor(s: &str, tag: u64) -> CborOwned {
Expand Down

0 comments on commit e30ba80

Please sign in to comment.