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)]
use nom::{
branch::alt,
@ -12,7 +17,10 @@ use nom::{
IResult,
};
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 END_MARKER: &str = "{% endconfiguration %}";
@ -20,7 +28,24 @@ const DOCUMENT_WHITESPACE: &str = " ";
// const KNOWN_STRUCTS: RefCell<HashMap<>> = ;
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");
}
@ -231,12 +256,14 @@ impl LineType {
enum DataType {
List,
String,
Float,
StringList,
Map,
Template,
Integer,
Boolean,
Icon,
Unknown,
}
impl DataType {
@ -244,13 +271,15 @@ impl DataType {
map_opt(rest, |i| {
Some(match i {
"list" => Self::List,
"float" => Self::Float,
"string" => Self::String,
"map" => Self::Map,
"template" => Self::Template,
"integer" => Self::Integer,
"[string, list]" => Self::StringList,
"[list]" | "[string, list]" | "[list, string]" => Self::StringList,
"boolean" => Self::Boolean,
"icon" => Self::Icon,
"device_class" => Self::Unknown,
_ => return None,
})
})(i)
@ -263,10 +292,13 @@ impl DataType {
Self::List => format!("Vec<{type_name}>"),
Self::Map => type_name.to_string(),
Self::Unknown => String::from("_"),
Self::String => String::from("String"),
Self::StringList => String::from("Vec<String>"),
Self::Template => String::from("Template"),
Self::Integer => String::from("usize"),
Self::Float => String::from("f32"),
Self::Boolean => String::from("bool"),
Self::Icon => String::from("Icon"),
}