| 1 | //! An encoder for bencode. Guarantees that the output string is valid bencode |
| 2 | //! |
| 3 | //! # Encoding a structure |
| 4 | //! |
| 5 | //! The easiest way to encode a structure is to implement [`ToBencode`] for it. For most structures, |
| 6 | //! this should be very simple: |
| 7 | //! |
| 8 | //! ``` |
| 9 | //! # use bendy::encoding::{ToBencode, SingleItemEncoder, Error}; |
| 10 | //! |
| 11 | //! struct Message { |
| 12 | //! foo: i32, |
| 13 | //! bar: String, |
| 14 | //! } |
| 15 | //! |
| 16 | //! impl ToBencode for Message { |
| 17 | //! // Atoms have depth zero. The struct wrapper adds one level to that |
| 18 | //! const MAX_DEPTH: usize = 1; |
| 19 | //! |
| 20 | //! fn encode(&self, encoder: SingleItemEncoder) -> Result<(), Error> { |
| 21 | //! encoder.emit_dict(|mut e| { |
| 22 | //! // Use e to emit the values |
| 23 | //! e.emit_pair(b"bar", &self.bar)?; |
| 24 | //! e.emit_pair(b"foo", &self.foo) |
| 25 | //! })?; |
| 26 | //! Ok(()) |
| 27 | //! } |
| 28 | //! } |
| 29 | //! # |
| 30 | //! # fn main() -> Result<(), Error> { |
| 31 | //! # let message = Message{ |
| 32 | //! # foo: 1, |
| 33 | //! # bar: "quux".to_string(), |
| 34 | //! # }; |
| 35 | //! # |
| 36 | //! # message.to_bencode().map(|_| ()) |
| 37 | //! # } |
| 38 | //! ``` |
| 39 | //! |
| 40 | //! Then, messages can be serialized using [`ToBencode::to_bencode`]: |
| 41 | //! |
| 42 | //! ``` |
| 43 | //! # use bendy::encoding::{ToBencode, SingleItemEncoder, Error}; |
| 44 | //! # |
| 45 | //! # struct Message { |
| 46 | //! # foo: i32, |
| 47 | //! # bar: String, |
| 48 | //! # } |
| 49 | //! # |
| 50 | //! # impl ToBencode for Message { |
| 51 | //! # // Atoms have depth zero. The struct wrapper adds one level to that |
| 52 | //! # const MAX_DEPTH: usize = 1; |
| 53 | //! # |
| 54 | //! # fn encode(&self, encoder: SingleItemEncoder) -> Result<(), Error> { |
| 55 | //! # encoder.emit_dict(|mut e| { |
| 56 | //! # // Use e to emit the values. They must be in sorted order here. |
| 57 | //! # // If sorting the dict first is annoying, you can also use |
| 58 | //! # // encoder.emit_and_sort_dict |
| 59 | //! # e.emit_pair(b"bar", &self.bar)?; |
| 60 | //! # e.emit_pair(b"foo", &self.foo) |
| 61 | //! # })?; |
| 62 | //! # Ok(()) |
| 63 | //! # } |
| 64 | //! # } |
| 65 | //! # |
| 66 | //! # fn main() -> Result<(), Error> { |
| 67 | //! let message = Message { |
| 68 | //! foo: 1, |
| 69 | //! bar: "quux".to_string(), |
| 70 | //! }; |
| 71 | //! |
| 72 | //! message.to_bencode() |
| 73 | //! # .map(|_| ()) |
| 74 | //! # } |
| 75 | //! ``` |
| 76 | //! |
| 77 | //! Most primitive types already implement [`ToBencode`]. |
| 78 | //! |
| 79 | //! # Nesting depth limits |
| 80 | //! |
| 81 | //! To allow this to be used on limited platforms, all implementations of [`ToBencode`] include a |
| 82 | //! maximum nesting depth. Atoms (integers and byte strings) are considered to have depth 0. An |
| 83 | //! object (a list or dict) containing only atoms has depth 1, and in general, an object has a depth |
| 84 | //! equal to the depth of its deepest member plus one. |
| 85 | //! |
| 86 | //! ## Dynamic depth |
| 87 | //! |
| 88 | //! In some cases, an object doesn't have a statically known depth. For example, ASTs may be |
| 89 | //! arbitrarily nested. Such objects should have `MAX_DEPTH` set to 0, and callers should construct |
| 90 | //! the Encoder manually, adding an appropriate buffer for the depth: |
| 91 | //! |
| 92 | //! ``` |
| 93 | //! # use bendy::encoding::{ToBencode, Encoder, Error}; |
| 94 | //! # |
| 95 | //! # type ObjectType = u32; |
| 96 | //! # static OBJECT: u32 = 0; |
| 97 | //! # |
| 98 | //! # fn main() -> Result<(), Error> { |
| 99 | //! let mut encoder = Encoder::new().with_max_depth(ObjectType::MAX_DEPTH + 10); |
| 100 | //! |
| 101 | //! encoder.emit(OBJECT)?; |
| 102 | //! encoder.get_output() |
| 103 | //! # .map_err(Error::from) |
| 104 | //! # .map(|_| ()) // ignore a success return value |
| 105 | //! # } |
| 106 | //! ``` |
| 107 | //! |
| 108 | //! # Error handling |
| 109 | //! |
| 110 | //! Once an error occurs during encoding, all future calls to the same encoding stream will fail |
| 111 | //! early with the same error. It is not defined whether any callback or implementation of |
| 112 | //! [`ToBencode::encode`] is called before returning an error; such callbacks should |
| 113 | //! respond to failure by bailing out as quickly as possible. |
| 114 | //! |
| 115 | //! Not all values in [`Error`] can be caused by an encoding operation. Specifically, you only need |
| 116 | //! to worry about [`UnsortedKeys`] and [`NestingTooDeep`]. |
| 117 | //! |
| 118 | //! [`ToBencode::encode`]: self::ToBencode::encode |
| 119 | //! [`UnsortedKeys`]: self::Error#UnsortedKeys |
| 120 | //! [`NestingTooDeep`]: self::Error#NestingTooDeep |
| 121 | |
| 122 | mod encoder; |
| 123 | mod error; |
| 124 | mod printable_integer; |
| 125 | mod to_bencode; |
| 126 | |
| 127 | pub use self::{ |
| 128 | encoder::{Encoder, SingleItemEncoder, SortedDictEncoder, UnsortedDictEncoder}, |
| 129 | error::Error, |
| 130 | printable_integer::PrintableInteger, |
| 131 | to_bencode::{AsString, ToBencode}, |
| 132 | }; |