reached usable state with procedural macro
This commit is contained in:
@@ -1,51 +1,39 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
pub fn parse_config_file(input: &String) -> std::io::Result<std::collections::HashMap<String, String>> {
|
||||
let mut config = std::collections::HashMap::<String, String>::new();
|
||||
let lines = input.lines();
|
||||
|
||||
use syn::{Attribute, Error, Field, Meta, Path, punctuated::Punctuated, token::Comma};
|
||||
for line in lines {
|
||||
let line = line.trim();
|
||||
// ignore empty lines
|
||||
if line.is_empty() { continue; }
|
||||
|
||||
pub fn gen_config_load_function(fields: &Punctuated<Field, Comma>, config_map_name: &syn::Ident) -> Result<TokenStream, Error> {
|
||||
let mut assignments : TokenStream = TokenStream::new();
|
||||
'field_loop: for field in fields.iter() {
|
||||
let attr = &field.attrs;
|
||||
let name = match &field.ident {
|
||||
Some(value) => value,
|
||||
// skip anonymous fields
|
||||
None => continue 'field_loop,
|
||||
};
|
||||
let name_string: String = name.to_string();
|
||||
let ty = &field.ty;
|
||||
for attribute in attr {
|
||||
if let Attribute{ meta: Meta::Path( Path{segments: attr_name, ..} ), .. } = attribute {
|
||||
match attr_name.first() {
|
||||
Some(value) => if value.ident == "nested_config" {
|
||||
assignments.extend(quote! {
|
||||
self.#name.parse_from_map(&mut #config_map_name);
|
||||
});
|
||||
continue 'field_loop;
|
||||
} else if value.ident == "no_config" {
|
||||
continue 'field_loop;
|
||||
},
|
||||
None => (),
|
||||
}
|
||||
if line.starts_with("[") {
|
||||
// check for table
|
||||
if !line.ends_with("]") {
|
||||
// TODO: implement error handling
|
||||
} else if line.contains(' ') {
|
||||
// TODO: implement error handling
|
||||
}
|
||||
//let tokens = line.split('.');
|
||||
// TODO: implement hirarchical map
|
||||
} else {
|
||||
// check for config
|
||||
let mut tokens: Vec<&str> = line.split('=').map(|entry| entry.trim()).collect();
|
||||
tokens.retain(|entry| !entry.is_empty());
|
||||
// check for valid input
|
||||
if tokens.len() != 2 {
|
||||
// println!("error in line'", line);
|
||||
continue;
|
||||
}
|
||||
//// clean up value: remove quotes.. TODO:
|
||||
//let mut options: Vec<&str> = tokens[1].split(['\'', '\"']).collect::<Vec<&str>>().join(',');
|
||||
|
||||
config.insert(tokens[0].to_string(), tokens[1].to_string());
|
||||
}
|
||||
assignments.extend(quote! {
|
||||
self.#name = match #config_map_name.get(#name_string) {
|
||||
Some(value) => match value.parse::<#ty>() {
|
||||
Ok(parsed) => {
|
||||
_ = #config_map_name.remove(#name_string);
|
||||
parsed
|
||||
},
|
||||
Err(_) => {
|
||||
self.#name.clone()
|
||||
},
|
||||
},
|
||||
None => self.#name.clone(),
|
||||
};
|
||||
});
|
||||
|
||||
}
|
||||
Ok(assignments.into())
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
|
||||
@@ -189,7 +189,7 @@ pub fn make_padding_string(len: usize) -> String {
|
||||
/// input format is a quoted string (either double or single)
|
||||
/// the style can be a combination of **one** color and
|
||||
/// one or more style options (bold, italic, underlined, strikethrough)
|
||||
pub fn parse_style(arg: String) -> Result<String> {
|
||||
pub fn parse_style(arg: &String) -> Result<String> {
|
||||
let mut colors: Vec<String> = Vec::<String>::new();
|
||||
let mut styles: Vec<String> = Vec::<String>::new();
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
|
||||
pub mod common;
|
||||
pub mod format;
|
||||
|
||||
pub use common::parse_config_file;
|
||||
|
||||
Reference in New Issue
Block a user