postcard_bindgen_core/
lib.rs

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
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

#[cfg(feature = "generating")]
pub mod code_gen;
#[cfg(feature = "generating")]
pub mod path;
#[cfg(feature = "generating")]
pub mod registry;
#[cfg(feature = "generating")]
pub mod type_info;

#[cfg(feature = "generating")]
pub use genco::lang;

pub enum ArchPointerLen {
    U32,
    U64,
}

impl ArchPointerLen {
    #[allow(unused)]
    pub(crate) fn into_bytes(self) -> usize {
        match self {
            ArchPointerLen::U32 => 4,
            ArchPointerLen::U64 => 8,
        }
    }
}

/// Helper struct to pass the generated language strings to an export function.
#[cfg(feature = "generating")]
#[derive(Debug)]
pub struct Exports<L: genco::lang::Lang> {
    pub files: Vec<ExportFile<L>>,
}

#[cfg(feature = "generating")]
impl<L: genco::lang::Lang> Exports<L> {
    pub fn file(&self, content_type: impl AsRef<str>) -> Option<&genco::Tokens<L>> {
        self.files
            .iter()
            .find(|f| f.content_type.as_str() == content_type.as_ref())
            .map(|f| &f.content)
    }

    pub fn pop_file(&mut self, content_type: impl AsRef<str>) -> Option<genco::Tokens<L>> {
        let index = self
            .files
            .iter()
            .position(|f| f.content_type.as_str() == content_type.as_ref())?;
        Some(self.files.remove(index).content)
    }
}

#[cfg(feature = "generating")]
#[derive(Debug)]
pub struct ExportFile<L: genco::lang::Lang> {
    pub content_type: String,
    pub content: genco::Tokens<L>,
}