This commit is contained in:
Austen Adler 2024-04-08 23:34:43 -04:00
parent 60687e11fa
commit d989a1183f

View File

@ -1,3 +1,8 @@
// Terrible code generator for
// Clone this: https://github.com/home-assistant/core/tree/dev/homeassistant/components/mqtt
// Then run:
// cargo run -- ~/cloned-path/home-assistant.io/source/_integrations/*.mqtt.* > output.rs
#[allow(unused_imports)] #[allow(unused_imports)]
use nom::{ use nom::{
branch::alt, branch::alt,
@ -12,7 +17,10 @@ use nom::{
IResult, IResult,
}; };
use nom::{character::complete::space0, combinator::map_opt, multi::many_till}; use nom::{character::complete::space0, combinator::map_opt, multi::many_till};
use std::{path::Path, rc::Rc}; use std::{
path::{Path, PathBuf},
rc::Rc,
};
const START_MARKER: &str = "{% configuration %}"; const START_MARKER: &str = "{% configuration %}";
const END_MARKER: &str = "{% endconfiguration %}"; const END_MARKER: &str = "{% endconfiguration %}";
@ -20,7 +28,24 @@ const DOCUMENT_WHITESPACE: &str = " ";
// const KNOWN_STRUCTS: RefCell<HashMap<>> = ; // const KNOWN_STRUCTS: RefCell<HashMap<>> = ;
fn main() { fn main() {
parse_file("sample-file.txt"); // println!("");
for file in std::env::args().skip(1) {
let p = PathBuf::from(file);
println!(
"mod {} {{",
p.file_name()
.expect("Not a filename")
.to_string_lossy()
.split_once(".")
.expect("No file extension")
.0
);
parse_file(p);
println!("}}");
}
//
// parse_file("sample-file.txt");
// parse_file("sample-file.txt"); // parse_file("sample-file.txt");
} }
@ -231,12 +256,14 @@ impl LineType {
enum DataType { enum DataType {
List, List,
String, String,
Float,
StringList, StringList,
Map, Map,
Template, Template,
Integer, Integer,
Boolean, Boolean,
Icon, Icon,
Unknown,
} }
impl DataType { impl DataType {
@ -244,13 +271,15 @@ impl DataType {
map_opt(rest, |i| { map_opt(rest, |i| {
Some(match i { Some(match i {
"list" => Self::List, "list" => Self::List,
"float" => Self::Float,
"string" => Self::String, "string" => Self::String,
"map" => Self::Map, "map" => Self::Map,
"template" => Self::Template, "template" => Self::Template,
"integer" => Self::Integer, "integer" => Self::Integer,
"[string, list]" => Self::StringList, "[list]" | "[string, list]" | "[list, string]" => Self::StringList,
"boolean" => Self::Boolean, "boolean" => Self::Boolean,
"icon" => Self::Icon, "icon" => Self::Icon,
"device_class" => Self::Unknown,
_ => return None, _ => return None,
}) })
})(i) })(i)
@ -263,10 +292,13 @@ impl DataType {
Self::List => format!("Vec<{type_name}>"), Self::List => format!("Vec<{type_name}>"),
Self::Map => type_name.to_string(), Self::Map => type_name.to_string(),
Self::Unknown => String::from("_"),
Self::String => String::from("String"), Self::String => String::from("String"),
Self::StringList => String::from("Vec<String>"), Self::StringList => String::from("Vec<String>"),
Self::Template => String::from("Template"), Self::Template => String::from("Template"),
Self::Integer => String::from("usize"), Self::Integer => String::from("usize"),
Self::Float => String::from("f32"),
Self::Boolean => String::from("bool"), Self::Boolean => String::from("bool"),
Self::Icon => String::from("Icon"), Self::Icon => String::from("Icon"),
} }