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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
use quote::quote; use proc_macro::TokenStream; /// Macro which parses a hostname. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let address: Address = hostname!("host.example.com"); /// let address = hostname!("example.com"); /// # } /// ``` /// /// Won't compile: /// ```compile_fail /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let address = hostname!("example com"); /// # } /// ``` #[proc_macro] pub fn hostname(input: TokenStream) -> TokenStream { let string = extract_string(input); assert!(string.contains(" ") == false, "Hostname may not contain spaces."); //TODO expand checks; maybe split off fqdn!(), domain!() let tokens = quote! { Address::Name( #string.to_string() ) }; tokens.into() } /// Macro which parses an IPv4 address. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let address: Address = ip4!("127.0.0.1"); /// let address = ip4!("255.255.255.255"); /// # } /// ``` /// /// Won't compile: /// ```compile_fail /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let address = ip4!("1277.0.0.1"); /// # } /// ``` #[proc_macro] pub fn ip4(input: TokenStream) -> TokenStream { let string = extract_string(input); string.parse::<std::net::Ipv4Addr>().expect("Failed to parse as IPv4"); //check that it can be parsed let tokens = quote! { Address::Ip4( #string.parse::<std::net::Ipv4Addr>().unwrap() ) }; tokens.into() } /// Macro which parses an IPv6 address. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let address: Address = ip6!("::1"); /// let address = ip6!("2001:db8::"); /// # } /// ``` /// /// Won't compile: /// ```compile_fail /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let address = ip6!("2001:dn8::"); /// # } /// ``` #[proc_macro] pub fn ip6(input: TokenStream) -> TokenStream { let string = extract_string(input); string.parse::<std::net::Ipv6Addr>().expect("Failed to parse as IPv6"); //check that it can be parsed let tokens = quote! { Address::Ip6( #string.parse::<std::net::Ipv6Addr>().unwrap() ) }; tokens.into() } /// Macro which parses a URL. /// /// # Examples /// /// ``` /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # use url::Url; /// # /// # fn main() { /// let url: Url = url!("https://example.com"); /// let url = url!("ftp://127.0.0.1:45"); /// # } /// ``` /// /// Won't compile: /// ```compile_fail /// # #[macro_use] extern crate prefab_macro; /// # use prefab_macro::*; /// # use prefab_core::*; /// # /// # fn main() { /// let url = url!("https://example com"); /// # } /// ``` #[proc_macro] pub fn url(input: TokenStream) -> TokenStream { let string = extract_string(input); url::Url::parse(&string).expect("Failed to parse as URL"); //check that it can be parsed let tokens = quote! { url::Url::parse(&#string).unwrap() }; tokens.into() } fn extract_string(input: TokenStream) -> String { let string = input.to_string(); let string = &string[1..string.len()-1]; //remove quotation marks, assuming user entered a correct string string.to_string() }