Fix tabs in comments
This commit is contained in:
parent
bf988bec2f
commit
8c4d2dbd93
2 changed files with 40 additions and 40 deletions
|
@ -75,9 +75,9 @@ pub fn raw(bytes: &[u8]) -> Term<'_> {
|
||||||
/// use nettext::enc::*;
|
/// use nettext::enc::*;
|
||||||
///
|
///
|
||||||
/// assert_eq!(encode(list([
|
/// assert_eq!(encode(list([
|
||||||
/// string("Hello"),
|
/// string("Hello"),
|
||||||
/// string("world")
|
/// string("world")
|
||||||
/// ])).unwrap(), b"Hello world");
|
/// ])).unwrap(), b"Hello world");
|
||||||
/// ```
|
/// ```
|
||||||
pub fn list<'a, I: IntoIterator<Item = Term<'a>>>(terms: I) -> Term<'a> {
|
pub fn list<'a, I: IntoIterator<Item = Term<'a>>>(terms: I) -> Term<'a> {
|
||||||
let mut tmp = Vec::with_capacity(8);
|
let mut tmp = Vec::with_capacity(8);
|
||||||
|
@ -96,9 +96,9 @@ pub fn list<'a, I: IntoIterator<Item = Term<'a>>>(terms: I) -> Term<'a> {
|
||||||
/// use nettext::enc::*;
|
/// use nettext::enc::*;
|
||||||
///
|
///
|
||||||
/// assert_eq!(encode(dict([
|
/// assert_eq!(encode(dict([
|
||||||
/// ("a", string("Hello")),
|
/// ("a", string("Hello")),
|
||||||
/// ("b", string("world"))
|
/// ("b", string("world"))
|
||||||
/// ])).unwrap(), b"{\n a = Hello,\n b = world,\n}");
|
/// ])).unwrap(), b"{\n a = Hello,\n b = world,\n}");
|
||||||
/// ```
|
/// ```
|
||||||
pub fn dict<'a, I: IntoIterator<Item = (&'a str, Term<'a>)>>(pairs: I) -> Term<'a> {
|
pub fn dict<'a, I: IntoIterator<Item = (&'a str, Term<'a>)>>(pairs: I) -> Term<'a> {
|
||||||
let mut tmp = HashMap::new();
|
let mut tmp = HashMap::new();
|
||||||
|
@ -164,13 +164,13 @@ impl<'a> Term<'a> {
|
||||||
// ---- encoding function ----
|
// ---- encoding function ----
|
||||||
|
|
||||||
/// Generate the nettext representation of a term
|
/// Generate the nettext representation of a term
|
||||||
pub fn encode<'a>(t: Term<'a>) -> Result<Vec<u8>, Error> {
|
pub fn encode(t: Term<'_>) -> Result<Vec<u8>, Error> {
|
||||||
let mut buf = Vec::with_capacity(128);
|
let mut buf = Vec::with_capacity(128);
|
||||||
encode_aux(&mut buf, t.0, 0)?;
|
encode_aux(&mut buf, t.0, 0)?;
|
||||||
Ok(buf)
|
Ok(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn encode_aux<'a>(buf: &mut Vec<u8>, term: T<'a>, indent: usize) -> Result<(), Error> {
|
fn encode_aux(buf: &mut Vec<u8>, term: T<'_>, indent: usize) -> Result<(), Error> {
|
||||||
match term {
|
match term {
|
||||||
T::Str(s) => buf.extend_from_slice(s),
|
T::Str(s) => buf.extend_from_slice(s),
|
||||||
T::OwnedStr(s) => buf.extend_from_slice(&s),
|
T::OwnedStr(s) => buf.extend_from_slice(&s),
|
||||||
|
|
64
src/lib.rs
64
src/lib.rs
|
@ -3,49 +3,49 @@
|
||||||
//! ```
|
//! ```
|
||||||
//! use nettext::enc::*;
|
//! use nettext::enc::*;
|
||||||
//! use nettext::dec::*;
|
//! use nettext::dec::*;
|
||||||
//! use nettext::crypto::{self, Signer, Verifier};
|
//! use nettext::crypto::{self, Signer, Verifier};
|
||||||
//!
|
//!
|
||||||
//! let keypair = crypto::generate_keypair();
|
//! let keypair = crypto::generate_keypair();
|
||||||
//!
|
//!
|
||||||
//! // Encode a fist object that represents a payload that will be hashed and signed
|
//! // Encode a fist object that represents a payload that will be hashed and signed
|
||||||
//! let text1 = encode(list([
|
//! let text1 = encode(list([
|
||||||
//! string("CALL"),
|
//! string("CALL"),
|
||||||
//! string("myfunction"),
|
//! string("myfunction"),
|
||||||
//! dict([
|
//! dict([
|
||||||
//! ("a", string("hello")),
|
//! ("a", string("hello")),
|
||||||
//! ("b", string("world")),
|
//! ("b", string("world")),
|
||||||
//! ("c", raw(b"{ a = 12, b = 42 }")),
|
//! ("c", raw(b"{ a = 12, b = 42 }")),
|
||||||
//! ]),
|
//! ]),
|
||||||
//! keypair.public.term(),
|
//! keypair.public.term(),
|
||||||
//! ])).unwrap();
|
//! ])).unwrap();
|
||||||
//! eprintln!("{}", std::str::from_utf8(&text1).unwrap());
|
//! eprintln!("{}", std::str::from_utf8(&text1).unwrap());
|
||||||
//!
|
//!
|
||||||
//! let hash = crypto::Blake2Sum::compute(&text1);
|
//! let hash = crypto::Blake2Sum::compute(&text1);
|
||||||
//! let sign = keypair.sign(&text1);
|
//! let sign = keypair.sign(&text1);
|
||||||
//!
|
//!
|
||||||
//! // Encode a second object that represents the signed and hashed payload
|
//! // Encode a second object that represents the signed and hashed payload
|
||||||
//! let text2 = encode(dict([
|
//! let text2 = encode(dict([
|
||||||
//! ("hash", hash.term()),
|
//! ("hash", hash.term()),
|
||||||
//! ("signature", sign.term()),
|
//! ("signature", sign.term()),
|
||||||
//! ("payload", raw(&text1)),
|
//! ("payload", raw(&text1)),
|
||||||
//! ])).unwrap();
|
//! ])).unwrap();
|
||||||
//! eprintln!("{}", std::str::from_utf8(&text2).unwrap());
|
//! eprintln!("{}", std::str::from_utf8(&text2).unwrap());
|
||||||
//!
|
//!
|
||||||
//! // Decode and check everything is fine
|
//! // Decode and check everything is fine
|
||||||
//! let object1 = decode(&text2).unwrap();
|
//! let object1 = decode(&text2).unwrap();
|
||||||
//! let [hash, signature, payload] = object1.dict_of(["hash", "signature", "payload"], false).unwrap();
|
//! let [hash, signature, payload] = object1.dict_of(["hash", "signature", "payload"], false).unwrap();
|
||||||
//! assert!(hash.b2sum().unwrap().verify(payload.raw()).is_ok());
|
//! assert!(hash.b2sum().unwrap().verify(payload.raw()).is_ok());
|
||||||
//! assert_eq!(payload.raw(), text1);
|
//! assert_eq!(payload.raw(), text1);
|
||||||
//!
|
//!
|
||||||
//! let object2 = decode(payload.raw()).unwrap();
|
//! let object2 = decode(payload.raw()).unwrap();
|
||||||
//!
|
//!
|
||||||
//! let [verb, arg1, arg2, pubkey] = object2.list_of().unwrap();
|
//! let [verb, arg1, arg2, pubkey] = object2.list_of().unwrap();
|
||||||
//! let pubkey = pubkey.public_key().unwrap();
|
//! let pubkey = pubkey.public_key().unwrap();
|
||||||
//! assert!(pubkey.verify(payload.raw(), &signature.signature().unwrap()).is_ok());
|
//! assert!(pubkey.verify(payload.raw(), &signature.signature().unwrap()).is_ok());
|
||||||
//!
|
//!
|
||||||
//! assert_eq!(verb.string().unwrap(), "CALL");
|
//! assert_eq!(verb.string().unwrap(), "CALL");
|
||||||
//! assert_eq!(arg1.string().unwrap(), "myfunction");
|
//! assert_eq!(arg1.string().unwrap(), "myfunction");
|
||||||
//! assert_eq!(pubkey, keypair.public);
|
//! assert_eq!(pubkey, keypair.public);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! The value of `text1` would be as follows:
|
//! The value of `text1` would be as follows:
|
||||||
|
|
Loading…
Reference in a new issue