Skip to main content

tlb_ton/
library.rs

1use tlb::{
2    Cell, Data, Ref,
3    bits::NoArgs,
4    de::{CellDeserialize, CellParser, CellParserError},
5    either::Either,
6    ser::{CellBuilder, CellBuilderError, CellSerialize},
7};
8
9/// ```tlb
10/// libref_hash$0 lib_hash:bits256 = LibRef;
11/// libref_ref$1 library:^Cell = LibRef;
12/// ```
13#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum LibRef<R = Cell> {
16    Hash([u8; 32]),
17    Ref(R),
18}
19
20impl<R> CellSerialize for LibRef<R>
21where
22    R: CellSerialize<Args: NoArgs>,
23{
24    type Args = ();
25
26    #[inline]
27    fn store(&self, builder: &mut CellBuilder, (): Self::Args) -> Result<(), CellBuilderError> {
28        builder.store_as::<_, Either<Data, Ref>>(
29            match self {
30                Self::Hash(hash) => Either::Left(hash),
31                Self::Ref(library) => Either::Right(library),
32            },
33            NoArgs::EMPTY,
34        )?;
35        Ok(())
36    }
37}
38
39impl<'de, R> CellDeserialize<'de> for LibRef<R>
40where
41    R: CellDeserialize<'de, Args: NoArgs>,
42{
43    type Args = ();
44
45    #[inline]
46    fn parse(parser: &mut CellParser<'de>, (): Self::Args) -> Result<Self, CellParserError<'de>> {
47        Ok(
48            match parser.parse_as::<_, Either<Data, Ref>>(NoArgs::EMPTY)? {
49                Either::Left(hash) => Self::Hash(hash),
50                Either::Right(library) => Self::Ref(library),
51            },
52        )
53    }
54}