Skip to content

Commit

Permalink
Stringify rules
Browse files Browse the repository at this point in the history
  • Loading branch information
craigfay committed Apr 25, 2022
1 parent 946395a commit c84bd02
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ struct Config {
#[derive(Default)]
struct Rule {
selector: String,
declarations: HashMap<String, String>,
declarations: Vec<(String, String)>,
}

fn create_rules_from_config(config: Config) -> Vec<Rule> {
fn generate_rules(config: Config) -> Vec<Rule> {
let mut rules = Vec::new();

// Text Color
const TEXT_COLOR_PREFIX: &'static str = "";

for (name, value) in config.color {
let mut rule = Rule::default();
rule.selector = name;
rule.declarations.insert("color".to_string(), value);
rule.selector = format!(".{}", name);
rule.declarations.push(("color".to_string(), value));
rules.push(rule);
}

Expand All @@ -48,11 +48,30 @@ fn create_rules_from_config(config: Config) -> Vec<Rule> {
rules
}

fn stringify_rules(rules: Vec<Rule>) -> String {
let mut css = String::new();

for rule in rules {
let inner = rule.declarations.iter()
.map(|(k, v)| format!("{}:{};", k, v))
.collect::<Vec<String>>()
.join("");

let line = format!("{}{{{}}}\n", rule.selector, inner);
css.push_str(&line);
}

css
}

fn main() {
let path = std::env::args().nth(1)
.unwrap_or("config.json".to_string());

let file = File::open(path).unwrap();
let reader = BufReader::new(file);
let config: Config = serde_json::from_reader(reader).unwrap();
let rules = generate_rules(config);
let css = stringify_rules(rules);
println!("{}", css);
}

0 comments on commit c84bd02

Please sign in to comment.