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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use std::io::{Read, Write};
use amplify::flags::FlagVec;
use amplify::num::u24;
use amplify::{Slice32, Wrapper};
use super::{strategies, Strategy};
use crate::{Error, LightningDecode, LightningEncode};
impl LightningEncode for u8 {
fn lightning_encode<E: Write>(&self, mut e: E) -> Result<usize, Error> {
e.write_all(&[*self])?;
Ok(1)
}
}
impl LightningDecode for u8 {
fn lightning_decode<D: Read>(mut d: D) -> Result<Self, Error> {
let mut buf = [0u8; 1];
d.read_exact(&mut buf)?;
Ok(buf[0])
}
}
impl LightningEncode for u16 {
fn lightning_encode<E: Write>(&self, mut e: E) -> Result<usize, Error> {
let bytes = self.to_be_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl LightningDecode for u16 {
fn lightning_decode<D: Read>(mut d: D) -> Result<Self, Error> {
let mut buf = [0u8; 2];
d.read_exact(&mut buf)?;
Ok(u16::from_be_bytes(buf))
}
}
impl LightningEncode for u24 {
fn lightning_encode<E: Write>(&self, mut e: E) -> Result<usize, Error> {
let bytes = self.to_be_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl LightningDecode for u24 {
fn lightning_decode<D: Read>(mut d: D) -> Result<Self, Error> {
let mut buf = [0u8; 3];
d.read_exact(&mut buf)?;
Ok(u24::from_be_bytes(buf))
}
}
impl LightningEncode for u32 {
fn lightning_encode<E: Write>(&self, mut e: E) -> Result<usize, Error> {
let bytes = self.to_be_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl LightningDecode for u32 {
fn lightning_decode<D: Read>(mut d: D) -> Result<Self, Error> {
let mut buf = [0u8; 4];
d.read_exact(&mut buf)?;
Ok(u32::from_be_bytes(buf))
}
}
impl LightningEncode for u64 {
fn lightning_encode<E: Write>(&self, mut e: E) -> Result<usize, Error> {
let bytes = self.to_be_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl LightningDecode for u64 {
fn lightning_decode<D: Read>(mut d: D) -> Result<Self, Error> {
let mut buf = [0u8; 8];
d.read_exact(&mut buf)?;
Ok(u64::from_be_bytes(buf))
}
}
impl LightningEncode for usize {
fn lightning_encode<E: Write>(&self, mut e: E) -> Result<usize, Error> {
let count = match *self {
count if count > u16::MAX as usize => {
return Err(Error::TooLargeData(count))
}
count => count as u16,
};
let bytes = count.to_be_bytes();
e.write_all(&bytes)?;
Ok(bytes.len())
}
}
impl LightningDecode for usize {
fn lightning_decode<D: Read>(mut d: D) -> Result<Self, Error> {
let mut buf = [0u8; 2];
d.read_exact(&mut buf)?;
Ok(u16::from_be_bytes(buf) as usize)
}
}
impl LightningEncode for FlagVec {
fn lightning_encode<E: Write>(&self, e: E) -> Result<usize, Error> {
let flags = self.shrunk();
flags.as_inner().lightning_encode(e)
}
}
impl LightningDecode for FlagVec {
fn lightning_decode<D: Read>(d: D) -> Result<Self, Error> {
let flags = Vec::<u8>::lightning_decode(d)?;
Ok(FlagVec::from_inner(flags))
}
}
impl LightningEncode for Slice32 {
fn lightning_encode<E: Write>(&self, e: E) -> Result<usize, Error> {
self.as_inner().lightning_encode(e)
}
}
impl LightningDecode for Slice32 {
fn lightning_decode<D: Read>(d: D) -> Result<Self, Error> {
LightningDecode::lightning_decode(d).map(Slice32::from_inner)
}
}
mod _chrono {
use chrono::{DateTime, NaiveDateTime, Utc};
use super::*;
impl Strategy for NaiveDateTime {
type Strategy = strategies::AsStrict;
}
impl Strategy for DateTime<Utc> {
type Strategy = strategies::AsStrict;
}
}