import defusedxml.ElementTree as ET
def main() -> None:
tree = ET.parse("ucd.all.flat.xml")
root = tree.getroot()
ucd = {}
for character in root.iter("{http://www.unicode.org/ns/2003/ucd/1.0}char"):
if "cp" in character.attrib:
code_point = character.attrib["cp"]
if "na" in character.attrib:
name = character.attrib["na"]
if name != "":
ucd[name] = f"\\u{{{code_point}}}"
for alias in character.iter(
"{http://www.unicode.org/ns/2003/ucd/1.0}name-alias"
):
if "alias" in alias.attrib:
alias = alias.attrib["alias"]
if alias != "":
ucd[alias] = f"\\u{{{code_point}}}"
print("// trivet")
print("// copyright")
print("")
print("//! Provide the Unicode database.")
print("")
print("/// The Unicode database.")
print(
"""///
/// This is generated from the complete database, which can be obtained from
/// [unicode.org](https://unicode.org/ucd/). It is an array of pairs, with
/// each pair consisting of a Unicode name or alias and the corresponding code
/// point.
"""
)
print("pub const UCD: &[(&str, char)] = &[")
for name, code_point in ucd.items():
print(f" (\"{name.upper()}\", '{code_point}'),")
print("];")
if __name__ == "__main__":
main()