1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use super::fonts::FontRegistry;
use super::Size;
use crate::Options;
use std::collections::HashMap;
use std::collections::HashSet;
use std::string::ToString;

#[derive(Clone, Debug)]
pub struct Header {
    breakpoint: Size,
    font_families: HashSet<String>,
    font_registry: FontRegistry,
    media_queries: HashMap<String, Size>,
    preview: Option<String>,
    title: Option<String>,
    styles: HashSet<String>,
}

impl Header {
    pub fn title(&self) -> Option<&String> {
        self.title.as_ref()
    }

    pub fn set_title(&mut self, title: String) {
        self.title = Some(title);
    }

    pub fn breakpoint(&self) -> &Size {
        &self.breakpoint
    }

    pub fn set_breakpoint(&mut self, value: Size) {
        self.breakpoint = value;
    }

    pub fn preview(&self) -> Option<&String> {
        self.preview.as_ref()
    }

    pub fn set_preview(&mut self, preview: String) {
        self.preview = Some(preview);
    }

    pub fn has_media_queries(&self) -> bool {
        !self.media_queries.is_empty()
    }

    pub fn add_media_query<K: ToString>(&mut self, classname: K, size: Size) {
        self.media_queries.insert(classname.to_string(), size);
    }

    pub fn maybe_add_style<K: ToString>(&mut self, style: Option<K>) {
        match style {
            Some(value) => self.add_style(value),
            None => (),
        }
    }

    pub fn add_style<K: ToString>(&mut self, style: K) {
        self.styles.insert(style.to_string());
    }

    pub fn maybe_add_font_families(&mut self, font_family_list: Option<String>) {
        match font_family_list {
            Some(value) => self.add_font_families(value),
            None => (),
        }
    }

    pub fn add_font_families(&mut self, font_family_list: String) {
        let result = font_family_list
            .split(",")
            .map(|v| v.trim().to_string())
            .collect::<Vec<String>>();
        for item in result {
            self.font_families.insert(item);
        }
    }

    pub fn register_font(&mut self, name: &str, href: &str) {
        self.font_registry.add(name, href);
    }

    pub fn get_styles(&self) -> Vec<String> {
        self.styles.iter().cloned().collect()
    }

    pub fn get_font_families(&self) -> Vec<String> {
        self.font_families.iter().cloned().collect()
    }

    pub fn get_used_font_families(&self) -> Vec<&String> {
        let mut res = vec![];
        for name in self.font_families.iter() {
            if let Some(url) = self.font_registry.get(name) {
                res.push(url);
            }
        }
        res
    }

    pub fn get_media_queries(&self) -> &HashMap<String, Size> {
        &self.media_queries
    }
}

impl From<&Options> for Header {
    fn from(value: &Options) -> Self {
        Header {
            breakpoint: value.breakpoint.clone(),
            font_families: HashSet::new(),
            font_registry: value.fonts.clone(),
            media_queries: HashMap::new(),
            preview: None,
            title: None,
            styles: HashSet::new(),
        }
    }
}