1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#![recursion_limit = "256"]
#![deny(
non_upper_case_globals,
non_camel_case_types,
non_snake_case,
unused_mut,
unused_imports,
dead_code,
)]
#[cfg(feature = "derive")]
#[allow(unused_imports)]
#[macro_use]
extern crate lightning_encoding_derive as derive;
#[cfg(feature = "derive")]
pub use derive::{LightningDecode, LightningEncode};
#[allow(unused_imports)]
#[macro_use]
extern crate amplify;
mod big_size;
mod bitcoin;
mod byte_str;
mod collections;
mod error;
mod primitives;
pub mod strategies;
use std::io;
pub use big_size::BigSize;
pub use error::Error;
pub use strategies::Strategy;
pub use strict_encoding::TlvError;
pub trait LightningEncode {
fn lightning_encode<E: io::Write>(&self, e: E) -> Result<usize, Error>;
fn lightning_serialize(&self) -> Result<Vec<u8>, Error> {
let mut encoder = vec![];
self.lightning_encode(&mut encoder)?;
Ok(encoder)
}
}
pub trait LightningDecode
where
Self: Sized,
{
fn lightning_decode<D: io::Read>(d: D) -> Result<Self, Error>;
fn lightning_deserialize(data: impl AsRef<[u8]>) -> Result<Self, Error> {
let mut decoder = io::Cursor::new(data.as_ref());
let rv = Self::lightning_decode(&mut decoder)?;
let consumed = decoder.position() as usize;
if consumed == data.as_ref().len() {
Ok(rv)
} else {
Err(Error::DataNotEntirelyConsumed)
}
}
}
pub fn lightning_serialize<T>(data: &T) -> Result<Vec<u8>, Error>
where
T: LightningEncode,
{
data.lightning_serialize()
}
pub fn lightning_deserialize<T>(data: impl AsRef<[u8]>) -> Result<T, Error>
where
T: LightningDecode,
{
T::lightning_deserialize(data)
}