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
mod elements;
mod webidls;

use convert_case::{Case, Casing};
pub use elements::{parse_elements, parse_struct_name, ParsedElement};
pub use webidls::{parse_webidls, ParsedInterface};

use serde::{Deserialize, Serialize};
use std::fmt::Display;

/// An attribute
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Attribute {
    pub name: String,
    pub description: String,
    pub field_name: String,
    pub ty: AttributeType,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum AttributeType {
    Bool,
    String,
    Integer,
    Float,
    Identifier(String),
    Enumerable(Vec<String>),
}

impl Display for AttributeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AttributeType::Bool => write!(f, "bool"),
            AttributeType::String => write!(f, "String"),
            AttributeType::Integer => write!(f, "i64"),
            AttributeType::Float => write!(f, "f64"),
            AttributeType::Identifier(_) => todo!("identifier attr not yet implemented"),
            AttributeType::Enumerable(_) => todo!("enum attr not yet implemented"),
        }
    }
}

/// Each element in HTML falls into zero or more categories that group elements
/// with similar characteristics together.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ParsedCategory {
    Metadata,
    Flow,
    Sectioning,
    Heading,
    Phrasing,
    Embedded,
    Interactive,
    Palpable,
    ScriptSupporting,
    Transparent,
}

/// Each element in HTML has zero or more relationships to other elements.
///
/// This also holds the custom "Element" relationship, which is used to
/// represent specific elements.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum ParsedRelationship {
    Element(String),
    Category(ParsedCategory),
}

impl From<ParsedCategory> for ParsedRelationship {
    fn from(value: ParsedCategory) -> Self {
        ParsedRelationship::Category(value)
    }
}

pub fn normalize_field_name(name: &str) -> String {
    match name.to_case(Case::Snake).as_str() {
        "loop" => "loop_".to_owned(),
        "type" => "type_".to_owned(),
        "for" => "for_".to_owned(),
        "as" => "as_".to_owned(),
        "async" => "async_".to_owned(),
        other => other.to_owned(),
    }
}