html_bindgen/parse/
mod.rs

1mod aria;
2mod elements;
3mod webidls;
4
5pub use aria::{
6    parse_aria_elements, parse_aria_properties, parse_aria_roles, ParsedAriaElement,
7    ParsedAriaProperty, ParsedAriaRole,
8};
9use convert_case::{Case, Casing};
10pub use elements::{parse_elements, parse_struct_name, ParsedElement};
11pub use webidls::{parse_webidls, ParsedInterface};
12
13use serde::{Deserialize, Serialize};
14use std::fmt::Display;
15
16/// An attribute
17#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
18pub struct Attribute {
19    pub name: String,
20    pub description: String,
21    pub field_name: String,
22    pub ty: AttributeType,
23}
24
25#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
26pub enum AttributeType {
27    Bool,
28    String,
29    Integer,
30    Float,
31    Identifier(String),
32    Enumerable(Vec<String>),
33}
34
35impl Display for AttributeType {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            AttributeType::Bool => write!(f, "bool"),
39            AttributeType::String => write!(f, "String"),
40            AttributeType::Integer => write!(f, "i64"),
41            AttributeType::Float => write!(f, "f64"),
42            AttributeType::Identifier(_) => todo!("identifier attr not yet implemented"),
43            AttributeType::Enumerable(_) => todo!("enum attr not yet implemented"),
44        }
45    }
46}
47
48/// Each element in HTML falls into zero or more categories that group elements
49/// with similar characteristics together.
50#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
51pub enum ParsedCategory {
52    Metadata,
53    Flow,
54    Sectioning,
55    Heading,
56    Phrasing,
57    Embedded,
58    Interactive,
59    Palpable,
60    ScriptSupporting,
61    Transparent,
62}
63
64/// Each element in HTML has zero or more relationships to other elements.
65///
66/// This also holds the custom "Element" relationship, which is used to
67/// represent specific elements.
68#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
69pub enum ParsedRelationship {
70    Element(String),
71    Category(ParsedCategory),
72}
73
74impl From<ParsedCategory> for ParsedRelationship {
75    fn from(value: ParsedCategory) -> Self {
76        ParsedRelationship::Category(value)
77    }
78}
79
80pub fn normalize_field_name(name: &str) -> String {
81    match name.to_case(Case::Snake).as_str() {
82        "loop" => "loop_".to_owned(),
83        "type" => "type_".to_owned(),
84        "for" => "for_".to_owned(),
85        "as" => "as_".to_owned(),
86        "async" => "async_".to_owned(),
87        other => other.to_owned(),
88    }
89}