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
use alloc::{string::String, vec::Vec};
mod byte_arrays;
pub mod sequences;
mod short_tx_id;
mod signature;
mod u24;
mod u256;

pub use byte_arrays::{b016m::B016M, b0255::B0255, b032::B032, b064k::B064K, bytes::Bytes};
pub use sequences::{option::Sv2Option, seq0255::Seq0255, seq064k::Seq064K};

pub use short_tx_id::ShortTxId;
pub use signature::Signature;
pub use u24::U24;
pub use u256::U256;

pub type Bool = bool;
pub type U8 = u8;
pub type U16 = u16;
pub type U32 = u32;
pub type U64 = u64;
pub type Pubkey<'u> = U256<'u>;
// rust string are valid UTF-8 Sv2 string (STR0255) are raw bytes. So there are Sv2 string not
// representable as Str0255.
pub type Str0255<'a> = B0255<'a>;

pub trait GetSize {
    fn get_size(&self) -> usize;
}

pub trait FixedSize {
    const FIXED_SIZE: usize;
}

impl<T: FixedSize> GetSize for T {
    fn get_size(&self) -> usize {
        T::FIXED_SIZE
    }
}

impl FixedSize for bool {
    const FIXED_SIZE: usize = 1;
}

impl FixedSize for u8 {
    const FIXED_SIZE: usize = 1;
}

impl FixedSize for u16 {
    const FIXED_SIZE: usize = 2;
}

impl FixedSize for u32 {
    const FIXED_SIZE: usize = 4;
}

impl FixedSize for u64 {
    const FIXED_SIZE: usize = 8;
}

impl GetSize for [u8] {
    fn get_size(&self) -> usize {
        self.len()
    }
}

impl GetSize for String {
    fn get_size(&self) -> usize {
        // String is Str0255 1 byte len + x bytes
        self.len() + 1
    }
}

impl GetSize for Vec<u8> {
    fn get_size(&self) -> usize {
        self.len()
    }
}