1 // Copyright 2022 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use quote::quote; 6 use syn::parse_macro_input; 7 use syn::DeriveInput; 8 9 /// Implement `argh`'s `FromArgValue` trait for a struct or enum using `from_key_values`. 10 #[proc_macro_derive(FromKeyValues)] keyvalues_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream11pub fn keyvalues_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream { 12 let DeriveInput { 13 ident, generics, .. 14 } = parse_macro_input!(input); 15 let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); 16 17 quote! { 18 impl #impl_generics ::serde_keyvalue::argh::FromArgValue for #ident #ty_generics #where_clause { 19 fn from_arg_value(value: &str) -> std::result::Result<Self, std::string::String> { 20 ::serde_keyvalue::from_key_values(value).map_err(|e| e.to_string()) 21 } 22 } 23 } 24 .into() 25 } 26