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
use polywrap_uri::Uri;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, LitStr};

/// Construct a URI from compatible types.
#[proc_macro]
pub fn uri(input: TokenStream) -> TokenStream {
    // Parse the input tokens into a syntax tree
    let input = parse_macro_input!(input as LitStr);

    let parse_result = input.value().parse::<Uri>();

    match parse_result {
        Ok(uri) => {
            let authority = uri.authority();
            let path = uri.path();
            let uri = uri.uri();
            // Return the Uri struct
            let expanded = quote! {
                unsafe {
                    Uri::from_parts(#authority.to_owned(), #path.to_owned(), #uri.to_owned())
                }
            };
            TokenStream::from(expanded)
        }
        Err(err) => {
            // Error handling, this will be a compile-time error
            panic!("Failed to parse uri: {:?}", err);
        }
    }
}