From 798a2387cfd0138653599f56ed259d34ef889ba0 Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Fri, 12 Jul 2019 04:10:44 -0700 Subject: [PATCH] Add example for `canonical_json`. --- src/functions.rs | 25 ++++++++++++++++--------- src/lib.rs | 26 +++++++++++++------------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/functions.rs b/src/functions.rs index b96fcb5b..9978a9c9 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -171,6 +171,22 @@ where /// # Errors /// /// Returns an error if the provided JSON value is not a JSON object. +/// +/// # Examples +/// +/// ```rust +/// let input = +/// r#"{ +/// "本": 2, +/// "日": 1 +/// }"#; +/// +/// let value = serde_json::from_str::(input).unwrap(); +/// +/// let canonical = ruma_signatures::canonical_json(&value).unwrap(); +/// +/// assert_eq!(canonical, r#"{"日":1,"本":2}"#); +/// ``` pub fn canonical_json(value: &Value) -> Result { canonical_json_with_fields_to_remove(value, CANONICAL_JSON_FIELDS_TO_REMOVE) } @@ -195,15 +211,6 @@ pub fn canonical_json(value: &Value) -> Result { /// use std::collections::HashMap; /// /// const PUBLIC_KEY: &str = "XGX0JRS2Af3be3knz2fBiRbApjm2Dh61gXDJA8kcJNI"; -/// const SIGNATURE_BYTES: &str = -/// "K8280/U9SSy9IVtjBuVeLr+HpOB4BQFWbg+UZaADMtTdGYI7Geitb76LTrr5QV/7Xg4ahLwYGYZzuHGZKM5ZAQ"; -/// -/// // Decode the public key used to generate the signature into raw bytes. -/// let public_key = base64::decode_config(&PUBLIC_KEY, base64::STANDARD_NO_PAD).unwrap(); -/// -/// // Create a `Signature` from the raw bytes of the signature. -/// let signature_bytes = base64::decode_config(&SIGNATURE_BYTES, base64::STANDARD_NO_PAD).unwrap(); -/// let signature = ruma_signatures::Signature::new("ed25519:1", &signature_bytes).unwrap(); /// /// // Deserialize the signed JSON. /// let value = serde_json::from_str( diff --git a/src/lib.rs b/src/lib.rs index 0357be99..163cc08b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -223,9 +223,9 @@ mod test { assert_eq!( &test_canonical_json( r#"{ - "one": 1, - "two": "Two" - }"# + "one": 1, + "two": "Two" + }"# ), r#"{"one":1,"two":"Two"}"# ); @@ -233,9 +233,9 @@ mod test { assert_eq!( &test_canonical_json( r#"{ - "b": "2", - "a": "1" - }"# + "b": "2", + "a": "1" + }"# ), r#"{"a":"1","b":"2"}"# ); @@ -271,8 +271,8 @@ mod test { assert_eq!( &test_canonical_json( r#"{ - "a": "日本語" - }"# + "a": "日本語" + }"# ), r#"{"a":"日本語"}"# ); @@ -280,9 +280,9 @@ mod test { assert_eq!( &test_canonical_json( r#"{ - "本": 2, - "日": 1 - }"# + "本": 2, + "日": 1 + }"# ), r#"{"日":1,"本":2}"# ); @@ -290,8 +290,8 @@ mod test { assert_eq!( &test_canonical_json( r#"{ - "a": "\u65E5" - }"# + "a": "\u65E5" + }"# ), r#"{"a":"日"}"# );