feat: Added new crate harmony_macros with ip! macro to facilitate creating ip addresses
This commit is contained in:
23
harmony-rs/harmony_macros/src/lib.rs
Normal file
23
harmony-rs/harmony_macros/src/lib.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::quote;
|
||||
use syn::{parse_macro_input, LitStr};
|
||||
|
||||
#[proc_macro]
|
||||
pub fn ip(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as LitStr);
|
||||
let ip_str = input.value();
|
||||
|
||||
if let Ok(_) = ip_str.parse::<std::net::Ipv4Addr>() {
|
||||
let expanded = quote! { std::net::IpAddr::V4(#ip_str.parse::<std::net::Ipv4Addr>().unwrap()) };
|
||||
return TokenStream::from(expanded);
|
||||
}
|
||||
|
||||
if let Ok(_) = ip_str.parse::<std::net::Ipv6Addr>() {
|
||||
let expanded = quote! { std::net::IpAddr::V4(#ip_str.parse::<std::net::Ipv6Addr>().unwrap()) };
|
||||
return TokenStream::from(expanded);
|
||||
}
|
||||
|
||||
panic!("Invalid IP address: {}", ip_str);
|
||||
}
|
||||
Reference in New Issue
Block a user