torrust_serde_bencode/lib.rs
1//! This crate is a Rust library for using the [Serde](https://github.com/serde-rs/serde)
2//! serialization framework with bencode data.
3//!
4//! # Examples
5//!
6//! ```
7//! use serde_derive::{Serialize, Deserialize};
8//!
9//! #[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
10//! struct Product {
11//! name: String,
12//! price: u32,
13//! }
14//!
15//! fn main() -> Result<(), Box<dyn std::error::Error>> {
16//! let apple = Product {
17//! name: "Apple".to_string(),
18//! price: 130,
19//! };
20//!
21//! let serialized = serde_bencode::to_string(&apple)?;
22//!
23//! assert_eq!(serialized, "d4:name5:Apple5:pricei130ee".to_string());
24//!
25//! let deserialized: Product = serde_bencode::from_str(&serialized)?;
26//!
27//! assert_eq!(
28//! deserialized,
29//! Product {
30//! name: "Apple".to_string(),
31//! price: 130,
32//! }
33//! );
34//!
35//! Ok(())
36//! }
37//! ```
38
39pub mod de;
40pub mod error;
41pub mod ser;
42pub mod value;
43
44pub use de::{from_bytes, from_str, Deserializer};
45pub use error::{Error, Result};
46pub use ser::{to_bytes, to_string, Serializer};