From d989a1183fef95873b140625819d5a81925dbb1a Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Mon, 8 Apr 2024 23:34:43 -0400 Subject: [PATCH] Fixes --- .../src/bin/codegen.rs | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/homeassistant-mqtt-discovery/src/bin/codegen.rs b/homeassistant-mqtt-discovery/src/bin/codegen.rs index 47c5dc7..f66de14 100644 --- a/homeassistant-mqtt-discovery/src/bin/codegen.rs +++ b/homeassistant-mqtt-discovery/src/bin/codegen.rs @@ -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> = ; 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"), 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"), }