renamed config-parser and implemented the first set of functions for the
macro extended and cleaned up format.rs and moved it into `config-parser` some other bits and bobs
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"config-parser-common",
|
||||
"config-parser-macro",
|
||||
]
|
||||
|
||||
[package]
|
||||
name = "config-parser"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
|
||||
[dependencies]
|
||||
config-parser-macro = { path = "config-parser-macro" }
|
||||
config-parser-common = { path = "config-parser-common" }
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "config-parser-common"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0.92"
|
||||
quote = "1.0.38"
|
||||
syn = { version = "2.0.94", features = ["full", "extra-traits"] }
|
||||
@@ -0,0 +1,51 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::quote;
|
||||
|
||||
use syn::{Attribute, Error, Field, Meta, Path, punctuated::Punctuated, token::Comma};
|
||||
|
||||
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 => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
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())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,298 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use std::io::{Result, Error};
|
||||
|
||||
pub const ESC: &str = "\x1b";
|
||||
pub const PREFIX: &str = "\x1b[";
|
||||
pub const RESET_ARG: &str = "0";
|
||||
pub const TERMINATION: &str = "m";
|
||||
pub const RESET_SEQ: &str = "\x1b[0m";
|
||||
pub const FG: ColorContext = ColorContext::Foreground;
|
||||
pub const BG: ColorContext = ColorContext::Background;
|
||||
|
||||
pub const STYLES: Styles = Styles {
|
||||
set: StyleCodes {
|
||||
bold: "1",
|
||||
dim: "2",
|
||||
italic: "3",
|
||||
underline: "4",
|
||||
blink: "5",
|
||||
reverse: "7",
|
||||
invisible: "8",
|
||||
strikethrough: "9",
|
||||
},
|
||||
reset: StyleCodes {
|
||||
bold: "22",
|
||||
dim: "22",
|
||||
italic: "23",
|
||||
underline: "24",
|
||||
blink: "25",
|
||||
reverse: "27",
|
||||
invisible: "28",
|
||||
strikethrough: "29",
|
||||
},
|
||||
};
|
||||
|
||||
pub const COLORS: Colors = Colors {
|
||||
fg: ColorCodes {
|
||||
black: "30",
|
||||
red: "31",
|
||||
green: "32",
|
||||
yellow: "33",
|
||||
blue: "34",
|
||||
magenta: "35",
|
||||
cyan: "36",
|
||||
white: "37",
|
||||
extended: "38",
|
||||
default: "39",
|
||||
},
|
||||
bg: ColorCodes {
|
||||
black: "40",
|
||||
red: "41",
|
||||
green: "42",
|
||||
yellow: "43",
|
||||
blue: "44",
|
||||
magenta: "45",
|
||||
cyan: "46",
|
||||
white: "47",
|
||||
extended: "48",
|
||||
default: "49",
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Styles {
|
||||
pub set: StyleCodes,
|
||||
pub reset: StyleCodes,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct StyleCodes {
|
||||
pub bold: &'static str,
|
||||
pub dim: &'static str,
|
||||
pub italic: &'static str,
|
||||
pub underline: &'static str,
|
||||
pub blink: &'static str,
|
||||
pub reverse: &'static str,
|
||||
pub invisible: &'static str,
|
||||
pub strikethrough: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Colors {
|
||||
pub fg: ColorCodes,
|
||||
pub bg: ColorCodes,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ColorCodes {
|
||||
pub black: &'static str,
|
||||
pub red: &'static str,
|
||||
pub green: &'static str,
|
||||
pub yellow: &'static str,
|
||||
pub blue: &'static str,
|
||||
pub magenta: &'static str,
|
||||
pub cyan: &'static str,
|
||||
pub white: &'static str,
|
||||
pub extended: &'static str,
|
||||
pub default: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ColorContext {
|
||||
Foreground,
|
||||
Background,
|
||||
}
|
||||
|
||||
/// prepends input with style string and appends the reset sequence at the end
|
||||
pub fn apply_format(input: &str, style: &str) -> String {
|
||||
format!("{}{}{}", style, input, RESET_SEQ)
|
||||
}
|
||||
|
||||
/// generates a common style sequence of format
|
||||
/// `\x1B[<styles>;<foreground-color>;<background-color>m`
|
||||
/// all elements are optional, if none is supplied the function returns an error
|
||||
pub fn generate_style_sequence(
|
||||
style: Option<&str>,
|
||||
foreground: Option<&str>,
|
||||
background: Option<&str>,
|
||||
) -> String {
|
||||
// assemble sequence as vector of string
|
||||
// this way the semicolons to separate arguments
|
||||
// can be inserted with 'join()'
|
||||
let mut sequence = PREFIX.to_owned();
|
||||
let mut arguments = Vec::<String>::new();
|
||||
if let Some(item) = style {
|
||||
arguments.push(item.to_owned());
|
||||
}
|
||||
if let Some(item) = foreground {
|
||||
arguments.push(item.to_owned());
|
||||
}
|
||||
if let Some(item) = background {
|
||||
arguments.push(item.to_owned());
|
||||
}
|
||||
|
||||
// panic if no arguments provided since this is a programming mistake
|
||||
if arguments.is_empty() {
|
||||
panic!("no arguments provided to 'generate_style_sequence()'");
|
||||
}
|
||||
sequence.push_str(&arguments.join(";"));
|
||||
sequence.push_str(TERMINATION); // terminate sequence with the termination character
|
||||
sequence
|
||||
}
|
||||
|
||||
/// generates a 256 color sequence
|
||||
/// see `generate_rgb_sequence(..)` for details
|
||||
pub fn generate_256color_sequence(context: ColorContext, color: u8) -> String {
|
||||
if color < 16 {
|
||||
return "".to_owned();
|
||||
}
|
||||
let mut sequence = PREFIX.to_owned();
|
||||
// choose context
|
||||
match context {
|
||||
ColorContext::Foreground => sequence.push_str(COLORS.fg.extended),
|
||||
ColorContext::Background => sequence.push_str(COLORS.bg.extended),
|
||||
};
|
||||
// make it a rgb sequence
|
||||
sequence.push_str(";5;");
|
||||
sequence.push_str(&format!("{color}{TERMINATION}"));
|
||||
sequence
|
||||
}
|
||||
|
||||
/// generates a rgb color sequence
|
||||
/// **note**: not all terminal emulators support rgb colors
|
||||
///
|
||||
/// rgb sequences are built the same as 256 color sequences:
|
||||
/// `\x1B[<context>;2;<r>;<g>;<b>m`
|
||||
/// where *context* is either '38' or '48' for foreground and background respectively
|
||||
/// and *r,g,b* are the values of each color channel
|
||||
pub fn generate_rgb_sequence(context: ColorContext, red: u8, green: u8, blue: u8) -> String {
|
||||
let mut sequence = PREFIX.to_owned();
|
||||
// choose context
|
||||
match context {
|
||||
ColorContext::Foreground => sequence.push_str(COLORS.fg.extended),
|
||||
ColorContext::Background => sequence.push_str(COLORS.bg.extended),
|
||||
};
|
||||
// make it a rgb sequence
|
||||
sequence.push_str(";2;");
|
||||
sequence.push_str(&format!("{red};{green};{blue}m"));
|
||||
sequence
|
||||
}
|
||||
|
||||
/// generates a padding string for numbers in a list
|
||||
pub fn make_padding_string(len: usize) -> String {
|
||||
// determine padding needed to align the paths
|
||||
String::from_utf8(vec![b' '; len]).unwrap()
|
||||
}
|
||||
|
||||
/// convert color setting to ansi escape sequence
|
||||
/// 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> {
|
||||
let mut colors: Vec<String> = Vec::<String>::new();
|
||||
let mut styles: Vec<String> = Vec::<String>::new();
|
||||
|
||||
// separate style options
|
||||
let mut tokens: Vec<String> = arg.split([' ', ',', '\"', '\'']).map(|entry| entry.trim().to_lowercase()).collect();
|
||||
tokens.retain(|entry| !entry.is_empty());
|
||||
|
||||
// parse options
|
||||
for option in tokens {
|
||||
// parse numbered colors
|
||||
if let Ok(sequence) = parse_numbered_color(&option) {
|
||||
colors.push(sequence);
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse rgb colors
|
||||
if let Ok(sequence) = parse_rgb_color(&option) {
|
||||
colors.push(sequence);
|
||||
continue;
|
||||
}
|
||||
|
||||
// parse styles and named colors
|
||||
match option.as_str() {
|
||||
// styles
|
||||
"bold" => styles.push(generate_style_sequence(Some(STYLES.set.bold), None, None)),
|
||||
"dim" => styles.push(generate_style_sequence(Some(STYLES.set.dim), None, None)),
|
||||
"italic" => styles.push(generate_style_sequence(Some(STYLES.set.italic), None, None)),
|
||||
"underlined" => styles.push(generate_style_sequence(Some(STYLES.set.underline), None, None)),
|
||||
"blink" => styles.push(generate_style_sequence(Some(STYLES.set.blink), None, None)),
|
||||
"reverse" => styles.push(generate_style_sequence(Some(STYLES.set.reverse), None, None)),
|
||||
"invisible" => styles.push(generate_style_sequence(Some(STYLES.set.invisible), None, None)),
|
||||
"strikethrough" => styles.push(generate_style_sequence(Some(STYLES.set.strikethrough), None, None)),
|
||||
// named colors
|
||||
"black" => colors.push(generate_style_sequence(None, Some(COLORS.fg.black), None)),
|
||||
"red" => colors.push(generate_style_sequence(None, Some(COLORS.fg.red), None)),
|
||||
"green" => colors.push(generate_style_sequence(None, Some(COLORS.fg.green), None)),
|
||||
"yellow" => colors.push(generate_style_sequence(None, Some(COLORS.fg.yellow), None)),
|
||||
"blue" => colors.push(generate_style_sequence(None, Some(COLORS.fg.blue), None)),
|
||||
"magenta" => colors.push(generate_style_sequence(None, Some(COLORS.fg.magenta), None)),
|
||||
"cyan" => colors.push(generate_style_sequence(None, Some(COLORS.fg.cyan), None)),
|
||||
"white" => colors.push(generate_style_sequence(None, Some(COLORS.fg.white), None)),
|
||||
"default" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||
_ => return Err(Error::other(format!("-- could not parse style token `{}` in config file", option))),
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
if colors.len() > 1 {
|
||||
return Err(Error::other(format!("-- too many colors found in setting <{}>", arg)));
|
||||
}
|
||||
if !colors.is_empty() {
|
||||
styles.push(colors.pop().unwrap());
|
||||
}
|
||||
Ok(styles.join(""))
|
||||
}
|
||||
|
||||
fn parse_numbered_color(string: &String) -> Result<String> {
|
||||
// check for numbered color
|
||||
if let Ok(number) = string.parse::<u8>() {
|
||||
return Ok(generate_256color_sequence(
|
||||
ColorContext::Foreground,
|
||||
number,
|
||||
));
|
||||
}
|
||||
Err(Error::other(format!("no numbered color found in '{}'", string)))
|
||||
}
|
||||
|
||||
fn parse_rgb_color(string: &String) -> Result<String> {
|
||||
// check for rgb color
|
||||
if string.as_bytes()[0] == b'#' && string.len() == 7 {
|
||||
let red = match u8::from_str_radix(&string[1..=2], 16) {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return Err(Error::other(format!(
|
||||
"-- failed to parse rgb color `{}` in config file",
|
||||
string
|
||||
)))
|
||||
}
|
||||
};
|
||||
let green = match u8::from_str_radix(&string[3..=4], 16) {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return Err(Error::other(format!(
|
||||
"-- failed to parse rgb color `{}` in config file",
|
||||
string
|
||||
)))
|
||||
}
|
||||
};
|
||||
let blue = match u8::from_str_radix(&string[5..=6], 16) {
|
||||
Ok(value) => value,
|
||||
Err(_) => {
|
||||
return Err(Error::other(format!(
|
||||
"-- failed to parse rgb color `{}` in config file",
|
||||
string
|
||||
)))
|
||||
}
|
||||
};
|
||||
return Ok(generate_rgb_sequence(
|
||||
ColorContext::Foreground,
|
||||
red,
|
||||
green,
|
||||
blue,
|
||||
));
|
||||
}
|
||||
Err(Error::other(format!("no rgb color found in '{}'", string)))
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
pub mod common;
|
||||
pub mod format;
|
||||
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "config-parser-macro"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1.0.92"
|
||||
quote = "1.0.38"
|
||||
syn = { version = "2.0.94", features = ["full", "extra-traits"] }
|
||||
config-parser-common = { path = "../config-parser-common/" }
|
||||
@@ -0,0 +1,100 @@
|
||||
use config_parser_common::common::gen_config_load_function;
|
||||
use proc_macro2::TokenStream;
|
||||
use syn::{parse_macro_input, DeriveInput};
|
||||
use quote::quote;
|
||||
|
||||
/// **for structs only**
|
||||
/// - implements `parse_config(&mut self, input: &String) -> Result<()>`
|
||||
/// which parses a string and fills the fills recognised values into the struct
|
||||
/// - implements `write_default_config() -> Result<String>`
|
||||
/// which write a default configuration, in case the documentation is lacking
|
||||
#[proc_macro_derive(ConfigParser, attributes(color_config, nested_config, no_config))]
|
||||
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let ast = parse_macro_input!(input as DeriveInput);
|
||||
let name = &ast.ident;
|
||||
let config_name = syn::Ident::new("config", name.span()); // TODO: use correct span
|
||||
let fields = if let syn::Data::Struct(syn::DataStruct{ fields: syn::Fields::Named(syn::FieldsNamed{ ref named, .. }), .. }) = ast.data {
|
||||
named
|
||||
} else {
|
||||
panic!("the macro `ConfigParser` applies only to structs!");
|
||||
};
|
||||
let assignments = match gen_config_load_function(fields, &config_name) {
|
||||
Ok(value) => value,
|
||||
Err(_) => panic!("loading config failed"),
|
||||
};
|
||||
|
||||
if name.to_string() == "Settings" {
|
||||
let string = format!("{:#?}", ast);
|
||||
_ = std::fs::write("test.txt", string);
|
||||
}
|
||||
|
||||
let expanded_stream: TokenStream = quote! {
|
||||
impl #name {
|
||||
/// tries to parse config from a string
|
||||
pub fn parse_from_string(&mut self, input: &String) -> std::io::Result<()> {
|
||||
let mut #config_name : std::collections::HashMap<String, String> = Self::parse_config_file(input)?;
|
||||
|
||||
#assignments
|
||||
|
||||
if !#config_name.is_empty() {
|
||||
let leftovers = #config_name.keys().cloned().collect::<Vec<String>>();
|
||||
return Err(std::io::Error::other(format!("the following settings were not recognised: {:#?}", leftovers)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// **do not call**
|
||||
/// this function needs to be public for nested configs but is not intended
|
||||
/// to be called by the user
|
||||
pub fn parse_from_map(&mut self, input: &mut std::collections::HashMap<String, String>) -> std::io::Result<()> {
|
||||
let mut #config_name = input;
|
||||
|
||||
#assignments
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
for line in lines {
|
||||
let mut line = line.trim();
|
||||
// ignore empty lines
|
||||
if line.is_empty() { continue; }
|
||||
|
||||
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());
|
||||
if tokens.len() != 2 {
|
||||
// println!("error in line'", line);
|
||||
continue;
|
||||
}
|
||||
config.insert(tokens[0].to_string(), tokens[1].to_string());
|
||||
}
|
||||
|
||||
}
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
// TODO: implement
|
||||
pub fn write_default_config(&self) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: implement function to parse style settings
|
||||
}
|
||||
}.into();
|
||||
expanded_stream.into()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
|
||||
pub use config_parser_common::format;
|
||||
pub use config_parser_macro::ConfigParser;
|
||||
Reference in New Issue
Block a user