From 81b24d8996f393184eb8e220ee06e63049ac6cb8 Mon Sep 17 00:00:00 2001 From: moreal Date: Tue, 2 Nov 2021 15:01:06 +0900 Subject: [PATCH] feat: introduce new aliases --- src/codec/types.rs | 43 +++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 4 +++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/codec/types.rs b/src/codec/types.rs index b19d739..b8201e4 100644 --- a/src/codec/types.rs +++ b/src/codec/types.rs @@ -1,14 +1,53 @@ use num_bigint::BigInt; use std::collections::BTreeMap; +/// The type alias of `BTreepMap` to reduce code size. +/// +/// ``` +/// use bencodex::{ Encode, BencodexDictionary }; +/// +/// let mut dict = BencodexDictionary::new(); +/// dict.insert("foo".into(), "bar".into()); +/// +/// let mut buf = vec![]; +/// dict.encode(&mut buf); +/// assert_eq!(buf, b"du3:foou3:bare") +/// ``` +pub type BencodexDictionary = BTreeMap; +/// The type alias of `Vec` to reduce code size. +/// +/// ``` +/// use bencodex::{ Encode, BencodexList }; +/// +/// let mut list = BencodexList::new(); +/// list.push("foo".to_string().into()); +/// list.push("bar".to_string().into()); +/// +/// let mut buf = vec![]; +/// list.encode(&mut buf); +/// assert_eq!(buf, b"lu3:foou3:bare") +/// ``` +pub type BencodexList = Vec; + +/// The constant of `BencodexValue::Null`. +/// +/// ``` +/// use bencodex::{ Encode, BENCODEX_NULL }; +/// +/// let mut buf = vec![]; +/// BENCODEX_NULL.encode(&mut buf); +/// assert_eq!(buf, b"n") +/// ``` +pub const BENCODEX_NULL: BencodexValue = BencodexValue::Null(()); + #[derive(PartialEq, Debug, Clone)] pub enum BencodexValue { Binary(Vec), Text(String), Boolean(bool), Number(BigInt), - List(Vec), - Dictionary(BTreeMap), + List(BencodexList), + Dictionary(BencodexDictionary), Null(()), } diff --git a/src/lib.rs b/src/lib.rs index a986d0f..06c2b21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,6 @@ pub mod codec; pub use codec::decode::{Decode, DecodeError}; pub use codec::encode::Encode; -pub use codec::types::{BencodexKey, BencodexValue}; +pub use codec::types::{ + BencodexDictionary, BencodexKey, BencodexList, BencodexValue, BENCODEX_NULL, +};