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
use proc_macro2::TokenStream;
use quote::quote;

pub(crate) struct TokenStreamPair {
    native: TokenStream,
    mapped: TokenStream,
}

impl TokenStreamPair {
    pub(crate) fn new() -> TokenStreamPair {
        Self::from_streams(TokenStream::new(), TokenStream::new())
    }

    pub(crate) fn from_streams(native: TokenStream, mapped: TokenStream) -> TokenStreamPair {
        TokenStreamPair { native, mapped }
    }

    pub(crate) fn extend(mut self, other: TokenStreamPair) -> Self {
        self.native.extend(other.native);
        self.mapped.extend(other.mapped);
        self
    }

    pub(crate) fn extend_native(&mut self, other: TokenStream) {
        self.native.extend(other);
    }

    pub(crate) fn into_token_stream(self) -> TokenStream {
        let Self { native, mapped } = self;

        quote! {
            #[allow(missing_docs)]
            mod generated {
                #[allow(nonstandard_style)]
                /// Native WASI types. These types can be written to and read from WASM memory.
                pub mod native {
                    #native

                    #[doc(hidden)]
                    #[derive(Copy, Clone, Debug)]
                    pub struct Private(());
                }

                #mapped
            }

            pub use self::generated::*;
        }
    }
}