Skip to content

Commit

Permalink
feat: introduce new aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Nov 2, 2021
1 parent 761b53d commit 81b24d8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
43 changes: 41 additions & 2 deletions src/codec/types.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
use num_bigint::BigInt;
use std::collections::BTreeMap;

/// The type alias of `BTreepMap<BencodexKey, BencodexValue>` 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<BencodexKey, BencodexValue>;
/// The type alias of `Vec<BencodexValue>` 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<BencodexValue>;

/// 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<u8>),
Text(String),
Boolean(bool),
Number(BigInt),
List(Vec<BencodexValue>),
Dictionary(BTreeMap<BencodexKey, BencodexValue>),
List(BencodexList),
Dictionary(BencodexDictionary),
Null(()),
}

Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

0 comments on commit 81b24d8

Please sign in to comment.