2
1
Fork 0

typegen: Pipewire and related support changes

- Added enum singleton support
This commit is contained in:
outfoxxed 2024-05-19 02:20:32 -07:00
parent 57ba1768f8
commit ff5da84a8b
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
9 changed files with 208 additions and 75 deletions

View file

@ -14,8 +14,10 @@ clean:
rm -rf content/docs/types/Quickshell.Wayland rm -rf content/docs/types/Quickshell.Wayland
rm -rf data/modules/Quickshell.DBusMenu rm -rf data/modules/Quickshell.DBusMenu
rm -rf content/docs/types/Quickshell.DBusMenu rm -rf content/docs/types/Quickshell.DBusMenu
rm -rf data/modules/Quickshell.Service.SystemTray rm -rf data/modules/Quickshell.Services.SystemTray
rm -rf content/docs/types/Quickshell.Service.SystemTray rm -rf content/docs/types/Quickshell.Services.SystemTray
rm -rf data/modules/Quickshell.Services.PipeWire
rm -rf content/docs/types/Quickshell.Services.PipeWire
rm -rf data/modules/Quickshell.Hyprland rm -rf data/modules/Quickshell.Hyprland
rm -rf content/docs/types/Quickshell.Hyprland rm -rf content/docs/types/Quickshell.Hyprland
@ -28,13 +30,15 @@ typedocs: clean buildtypegen
{{typegen_bin}} gentypes {{src_path}}/io/module.md build/types/types/Quickshell.Io.json {{typegen_bin}} gentypes {{src_path}}/io/module.md build/types/types/Quickshell.Io.json
{{typegen_bin}} gentypes {{src_path}}/wayland/module.md build/types/types/Quickshell.Wayland.json {{typegen_bin}} gentypes {{src_path}}/wayland/module.md build/types/types/Quickshell.Wayland.json
{{typegen_bin}} gentypes {{src_path}}/dbus/dbusmenu/module.md build/types/types/Quickshell.DBusMenu.json {{typegen_bin}} gentypes {{src_path}}/dbus/dbusmenu/module.md build/types/types/Quickshell.DBusMenu.json
{{typegen_bin}} gentypes {{src_path}}/services/status_notifier/module.md build/types/types/Quickshell.Service.SystemTray.json {{typegen_bin}} gentypes {{src_path}}/services/status_notifier/module.md build/types/types/Quickshell.Services.SystemTray.json
{{typegen_bin}} gentypes {{src_path}}/services/pipewire/module.md build/types/types/Quickshell.Services.PipeWire.json
{{typegen_bin}} gentypes {{src_path}}/wayland/hyprland/module.md build/types/types/Quickshell.Hyprland.json {{typegen_bin}} gentypes {{src_path}}/wayland/hyprland/module.md build/types/types/Quickshell.Hyprland.json
sh -c '{{typegen_bin}} gendocs {{src_path}}/core/module.md data/modules/Quickshell content/docs/types/Quickshell types/* build/types/types/*' sh -c '{{typegen_bin}} gendocs {{src_path}}/core/module.md data/modules/Quickshell content/docs/types/Quickshell types/* build/types/types/*'
sh -c '{{typegen_bin}} gendocs {{src_path}}/io/module.md data/modules/Quickshell.Io content/docs/types/Quickshell.Io types/* build/types/types/*' sh -c '{{typegen_bin}} gendocs {{src_path}}/io/module.md data/modules/Quickshell.Io content/docs/types/Quickshell.Io types/* build/types/types/*'
sh -c '{{typegen_bin}} gendocs {{src_path}}/wayland/module.md data/modules/Quickshell.Wayland content/docs/types/Quickshell.Wayland types/* build/types/types/*' sh -c '{{typegen_bin}} gendocs {{src_path}}/wayland/module.md data/modules/Quickshell.Wayland content/docs/types/Quickshell.Wayland types/* build/types/types/*'
sh -c '{{typegen_bin}} gendocs {{src_path}}/dbus/dbusmenu/module.md data/modules/Quickshell.DBusMenu content/docs/types/Quickshell.DBusMenu types/* build/types/types/*' sh -c '{{typegen_bin}} gendocs {{src_path}}/dbus/dbusmenu/module.md data/modules/Quickshell.DBusMenu content/docs/types/Quickshell.DBusMenu types/* build/types/types/*'
sh -c '{{typegen_bin}} gendocs {{src_path}}/services/status_notifier/module.md data/modules/Quickshell.Service.SystemTray content/docs/types/Quickshell.Service.SystemTray types/* build/types/types/*' sh -c '{{typegen_bin}} gendocs {{src_path}}/services/status_notifier/module.md data/modules/Quickshell.Services.SystemTray content/docs/types/Quickshell.Services.SystemTray types/* build/types/types/*'
sh -c '{{typegen_bin}} gendocs {{src_path}}/services/pipewire/module.md data/modules/Quickshell.Services.PipeWire content/docs/types/Quickshell.Services.PipeWire types/* build/types/types/*'
sh -c '{{typegen_bin}} gendocs {{src_path}}/wayland/hyprland/module.md data/modules/Quickshell.Hyprland content/docs/types/Quickshell.Hyprland types/* build/types/types/*' sh -c '{{typegen_bin}} gendocs {{src_path}}/wayland/hyprland/module.md data/modules/Quickshell.Hyprland content/docs/types/Quickshell.Hyprland types/* build/types/types/*'
serve: typedocs serve: typedocs

View file

@ -53,6 +53,19 @@
{{ if $type.details -}} <a href="#details">More</a> {{- end -}} {{ if $type.details -}} <a href="#details">More</a> {{- end -}}
{{- end -}} {{- end -}}
{{- if $type.variants -}}
<h4>Variants</h4>
<ul>
{{- range $name, $variant := $type.variants -}}
<li>
<span class="typegray">
<a href="#variant.{{ $name }}">{{ $name }}</a>
</span>
</li>
{{- end -}}
</ul>
{{- end -}}
{{- if $type.properties -}} {{- if $type.properties -}}
<h4>Properties</h4> <h4>Properties</h4>
<ul> <ul>
@ -113,24 +126,28 @@
</ul> </ul>
{{- end -}} {{- end -}}
{{- if $type.variants -}}
<h4>Variants</h4>
<ul>
{{- range $name, $variant := $type.variants -}}
<li>
<span class="typegray">
<a href="#variant.{{ $name }}">{{ $name }}</a>
</span>
</li>
{{- end -}}
</ul>
{{- end -}}
{{- if $type.details -}} {{- if $type.details -}}
<h3 id="details">Detailed Description</h3> <h3 id="details">Detailed Description</h3>
{{- $type.details | $.Page.RenderString (dict "display" "block") -}} {{- $type.details | $.Page.RenderString (dict "display" "block") -}}
{{- end -}} {{- end -}}
{{- if $type.variants -}}
<h3>Variant Details</h3>
{{ range $name, $variant := $type.variants }}
<div id="variant.{{ $name }}" class = "qmlpropdef">
<p>{{ $name -}}</p>
</div>
<div class="qmlpropdetails">
{{- if $variant.details -}}
{{- $variant.details | $.Page.RenderString (dict "display" "block") -}}
{{- else -}}
<p style="color: #999999"><i>No details provided.</i></p>
{{- end -}}
</div>
{{- end -}}
{{- end -}}
{{- if $type.properties -}} {{- if $type.properties -}}
<h3>Property Details</h3> <h3>Property Details</h3>
{{ range $propname, $prop := $type.properties }} {{ range $propname, $prop := $type.properties }}
@ -228,20 +245,3 @@
</div> </div>
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- if $type.variants -}}
<h3>Variant Details</h3>
{{ range $name, $variant := $type.variants }}
<div id="variant.{{ $name }}" class = "qmlpropdef">
<p>{{ $name -}}</p>
</div>
<div class="qmlpropdetails">
{{- if $variant.details -}}
{{- $variant.details | $.Page.RenderString (dict "display" "block") -}}
{{- else -}}
<p style="color: #999999"><i>No details provided.</i></p>
{{- end -}}
</div>
{{- end -}}
{{- end -}}

40
typegen/Cargo.lock generated
View file

@ -17,12 +17,38 @@ version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca"
[[package]]
name = "bit-set"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1"
dependencies = [
"bit-vec",
]
[[package]]
name = "bit-vec"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb"
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "fancy-regex"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "531e46835a22af56d1e3b66f04844bed63158bc094a628bec1d321d9b4c44bf2"
dependencies = [
"bit-set",
"regex-automata",
"regex-syntax",
]
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.14.3" version = "0.14.3"
@ -69,18 +95,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "regex"
version = "1.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.4.5" version = "0.4.5"
@ -194,7 +208,7 @@ name = "typegen"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"regex", "fancy-regex",
"serde", "serde",
"serde_json", "serde_json",
"toml", "toml",

View file

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "^1" anyhow = "^1"
regex = "^1.10" fancy-regex = "^0.13"
serde = { version = "^1", features = ["derive"] } serde = { version = "^1", features = ["derive"] }
serde_json = "^1" serde_json = "^1"
toml = "^0.8" toml = "^0.8"

View file

@ -27,6 +27,7 @@ pub struct ClassInfo {
pub properties: HashMap<String, Property>, pub properties: HashMap<String, Property>,
pub functions: Vec<Function>, pub functions: Vec<Function>,
pub signals: HashMap<String, Signal>, pub signals: HashMap<String, Signal>,
pub variants: HashMap<String, Variant>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -133,4 +134,5 @@ pub enum Flag {
Writeonly, Writeonly,
Singleton, Singleton,
Uncreatable, Uncreatable,
Enum,
} }

View file

@ -1,7 +1,7 @@
use std::borrow::Cow; use std::borrow::Cow;
use anyhow::{anyhow, bail, Context}; use anyhow::{anyhow, bail, Context};
use regex::Regex; use fancy_regex::Regex;
use serde::Deserialize; use serde::Deserialize;
use crate::typespec; use crate::typespec;
@ -45,6 +45,7 @@ pub struct ClassInfo<'a> {
pub properties: Vec<Property<'a>>, pub properties: Vec<Property<'a>>,
pub invokables: Vec<Invokable<'a>>, pub invokables: Vec<Invokable<'a>>,
pub signals: Vec<Signal<'a>>, pub signals: Vec<Signal<'a>>,
pub enums: Vec<EnumInfo<'a>>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -108,7 +109,8 @@ pub struct Parser {
pub signal_regex: Regex, pub signal_regex: Regex,
pub fn_param_regex: Regex, pub fn_param_regex: Regex,
pub defaultprop_classinfo_regex: Regex, pub defaultprop_classinfo_regex: Regex,
pub enum_regex: Regex, pub enum_ns_regex: Regex,
pub enum_class_regex: Regex,
pub enum_variant_regex: Regex, pub enum_variant_regex: Regex,
} }
@ -130,16 +132,17 @@ impl Default for ParseContext<'_> {
impl Parser { impl Parser {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
class_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*class\s+(?<name>\w+)(?:\s*:\s*public\s+(?<super>\w+)(\s*,(\s*\w+)*)*)?\s*\{(?<body>[\s\S]*?)};"#).unwrap(), class_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*class\s+(?<name>\w+)(?:\s*:\s*public\s+(?<super>\w+)(\s*,(\s*\w+)*)*)?\s*\{(?<body>[\s\S]*?)(?!};\s*Q_ENUM)};"#).unwrap(),
macro_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*(?<hide>QSDOC_HIDE\s)?(?<type>(Q|QML|QSDOC)_\w+)\s*(\(\s*(?<args>.*)\s*\))?;"#).unwrap(), macro_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*(?<hide>QSDOC_HIDE\s)?(?<type>(Q|QML|QSDOC)_\w+)\s*(\(\s*(?<args>.*)\s*\))?;"#).unwrap(),
property_regex: Regex::new(r#"^\s*(?<type>(\w|::|, |<|>|\*)+)\*?\s+(?<name>\w+)(\s+(MEMBER\s+(?<member>\w+)|READ\s+(?<read>\w+)|WRITE\s+(?<write>\w+)|NOTIFY\s+(?<notify>\w+)|(?<const>CONSTANT)))+\s*$"#).unwrap(), property_regex: Regex::new(r#"^\s*(?<type>(\w|::|, |<|>|\*)+)\*?\s+(?<name>\w+)(\s+(MEMBER\s+(?<member>\w+)|READ\s+(?<read>\w+)|WRITE\s+(?<write>\w+)|NOTIFY\s+(?<notify>\w+)|(?<const>CONSTANT)))+\s*$"#).unwrap(),
fn_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*Q_INVOKABLE\s+(\[\[.*\]\]\s+)?(?<type>(\w|::|<|>)+\*?)\s+(?<name>\w+)\((?<params>[\s\S]*?)\)(\s*const)?;"#).unwrap(), fn_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*Q_INVOKABLE\s+(\[\[.*\]\]\s+)?(static\s+)?(?<type>(\w|::|<|>)+\*?)\s+(?<name>\w+)\((?<params>[\s\S]*?)\)(\s*const)?;"#).unwrap(),
signal_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*void\s+(?<name>\w+)\((?<params>[\s\S]*?)\);"#).unwrap(), signal_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*void\s+(?<name>\w+)\((?<params>[\s\S]*?)\);"#).unwrap(),
fn_param_regex: Regex::new(r#"(const\s+)?(?<type>(\w|::|<|>)+\*?)&?\s+(?<name>\w+)(,|$)"#).unwrap(), fn_param_regex: Regex::new(r#"(const\s+)?(?<type>(\w|::|<|>)+\*?)&?\s+(?<name>\w+)(,|$)"#).unwrap(),
signals_regex: Regex::new(r#"signals:(?<signals>(\s*(\s*///.*\s*)*void .*;)*)"#).unwrap(), signals_regex: Regex::new(r#"signals:(?<signals>(\s*(\s*///.*\s*)*void .*;)*)"#).unwrap(),
defaultprop_classinfo_regex: Regex::new(r#"^\s*"DefaultProperty", "(?<prop>.+)"\s*$"#).unwrap(), defaultprop_classinfo_regex: Regex::new(r#"^\s*"DefaultProperty", "(?<prop>.+)"\s*$"#).unwrap(),
enum_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*namespace (?<namespace>\w+)\s*\{[\s\S]*?(QML_ELEMENT|QML_NAMED_ELEMENT\((?<qml_name>\w+)\));[\s\S]*?enum\s*(?<enum_name>\w+)\s*\{(?<body>[\s\S]*?)\};[\s\S]*?\}"#).unwrap(), enum_ns_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*namespace (?<namespace>\w+)\s*\{[\s\S]*?(QML_ELEMENT|QML_NAMED_ELEMENT\((?<qml_name>\w+)\));[\s\S]*?enum\s*(?<enum_name>\w+)\s*\{(?<body>[\s\S]*?)\};[\s\S]*?\}"#).unwrap(),
enum_variant_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*(?<name>\w+)\s*=\s*\d+,"#).unwrap(), enum_class_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*enum\s*(?<enum_name>\w+)\s*\{(?<body>[\s\S]*?)\};\s+Q_ENUM\(.+\);"#).unwrap(),
enum_variant_regex: Regex::new(r#"(?<comment>(\s*\/\/\/.*\n)+)?\s*(?<name>\w+)\s*=\s*.+,"#).unwrap(),
} }
} }
@ -149,6 +152,8 @@ impl Parser {
ctx: &mut ParseContext<'a>, ctx: &mut ParseContext<'a>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
for class in self.class_regex.captures_iter(text) { for class in self.class_regex.captures_iter(text) {
let class = class?;
let comment = class.name("comment").map(|m| m.as_str()); let comment = class.name("comment").map(|m| m.as_str());
let name = class.name("name").unwrap().as_str(); let name = class.name("name").unwrap().as_str();
let mut superclass = class.name("super").map(|m| m.as_str()); let mut superclass = class.name("super").map(|m| m.as_str());
@ -163,9 +168,12 @@ impl Parser {
let mut invokables = Vec::new(); let mut invokables = Vec::new();
let mut notify_signals = Vec::new(); let mut notify_signals = Vec::new();
let mut signals = Vec::new(); let mut signals = Vec::new();
let mut enums = Vec::new();
(|| { (|| {
for macro_ in self.macro_regex.captures_iter(body) { for macro_ in self.macro_regex.captures_iter(body) {
let macro_ = macro_?;
if macro_.name("hide").is_some() { if macro_.name("hide").is_some() {
continue continue
} }
@ -196,7 +204,7 @@ impl Parser {
self.property_regex self.property_regex
.captures(args.ok_or_else(|| { .captures(args.ok_or_else(|| {
anyhow!("expected args for Q_PROPERTY") anyhow!("expected args for Q_PROPERTY")
})?) })?)?
.ok_or_else(|| anyhow!("unable to parse Q_PROPERTY"))?; .ok_or_else(|| anyhow!("unable to parse Q_PROPERTY"))?;
let member = prop.name("member").is_some(); let member = prop.name("member").is_some();
@ -220,7 +228,7 @@ impl Parser {
"Q_CLASSINFO" => { "Q_CLASSINFO" => {
let classinfo = self.defaultprop_classinfo_regex.captures( let classinfo = self.defaultprop_classinfo_regex.captures(
args.ok_or_else(|| anyhow!("expected args for Q_CLASSINFO"))?, args.ok_or_else(|| anyhow!("expected args for Q_CLASSINFO"))?,
); )?;
if let Some(classinfo) = classinfo { if let Some(classinfo) = classinfo {
let prop = classinfo.name("prop").unwrap().as_str(); let prop = classinfo.name("prop").unwrap().as_str();
@ -246,6 +254,8 @@ impl Parser {
} }
for invokable in self.fn_regex.captures_iter(body) { for invokable in self.fn_regex.captures_iter(body) {
let invokable = invokable?;
let comment = invokable.name("comment").map(|m| m.as_str()); let comment = invokable.name("comment").map(|m| m.as_str());
let type_ = invokable.name("type").unwrap().as_str(); let type_ = invokable.name("type").unwrap().as_str();
let name = invokable.name("name").unwrap().as_str(); let name = invokable.name("name").unwrap().as_str();
@ -254,6 +264,8 @@ impl Parser {
let mut params = Vec::new(); let mut params = Vec::new();
for param in self.fn_param_regex.captures_iter(params_raw) { for param in self.fn_param_regex.captures_iter(params_raw) {
let param = param?;
let type_ = param.name("type").unwrap().as_str(); let type_ = param.name("type").unwrap().as_str();
let name = param.name("name").unwrap().as_str(); let name = param.name("name").unwrap().as_str();
@ -269,9 +281,13 @@ impl Parser {
} }
for signal_set in self.signals_regex.captures_iter(body) { for signal_set in self.signals_regex.captures_iter(body) {
let signal_set = signal_set?;
let signals_body = signal_set.name("signals").unwrap().as_str(); let signals_body = signal_set.name("signals").unwrap().as_str();
for signal in self.signal_regex.captures_iter(signals_body) { for signal in self.signal_regex.captures_iter(signals_body) {
let signal = signal?;
if signal.name("invokable").is_some() { if signal.name("invokable").is_some() {
continue; continue;
} }
@ -287,6 +303,8 @@ impl Parser {
let mut params = Vec::new(); let mut params = Vec::new();
for param in self.fn_param_regex.captures_iter(params_raw) { for param in self.fn_param_regex.captures_iter(params_raw) {
let param = param?;
let type_ = param.name("type").unwrap().as_str(); let type_ = param.name("type").unwrap().as_str();
let name = param.name("name").unwrap().as_str(); let name = param.name("name").unwrap().as_str();
@ -301,6 +319,23 @@ impl Parser {
} }
} }
for enum_ in self.enum_class_regex.captures_iter(body) {
let enum_ = enum_?;
let comment = enum_.name("comment").map(|m| m.as_str());
let enum_name = enum_.name("enum_name").unwrap().as_str();
let body = enum_.name("body").unwrap().as_str();
let variants = self.parse_enum_variants(body)?;
enums.push(EnumInfo {
namespace: name,
enum_name,
qml_name: enum_name,
comment,
variants,
});
}
Ok::<_, anyhow::Error>(()) Ok::<_, anyhow::Error>(())
})() })()
.with_context(|| format!("while parsing class `{name}`"))?; .with_context(|| format!("while parsing class `{name}`"))?;
@ -318,6 +353,7 @@ impl Parser {
properties, properties,
invokables, invokables,
signals, signals,
enums,
}); });
} }
@ -325,7 +361,9 @@ impl Parser {
} }
pub fn parse_enums<'a>(&self, text: &'a str, ctx: &mut ParseContext<'a>) -> anyhow::Result<()> { pub fn parse_enums<'a>(&self, text: &'a str, ctx: &mut ParseContext<'a>) -> anyhow::Result<()> {
for enum_ in self.enum_regex.captures_iter(text) { for enum_ in self.enum_ns_regex.captures_iter(text) {
let enum_ = enum_?;
let comment = enum_.name("comment").map(|m| m.as_str()); let comment = enum_.name("comment").map(|m| m.as_str());
let namespace = enum_.name("namespace").unwrap().as_str(); let namespace = enum_.name("namespace").unwrap().as_str();
let enum_name = enum_.name("enum_name").unwrap().as_str(); let enum_name = enum_.name("enum_name").unwrap().as_str();
@ -334,15 +372,7 @@ impl Parser {
.map(|m| m.as_str()) .map(|m| m.as_str())
.unwrap_or(namespace); .unwrap_or(namespace);
let body = enum_.name("body").unwrap().as_str(); let body = enum_.name("body").unwrap().as_str();
let variants = self.parse_enum_variants(body)?;
let mut variants = Vec::new();
for variant in self.enum_variant_regex.captures_iter(body) {
let comment = variant.name("comment").map(|m| m.as_str());
let name = variant.name("name").unwrap().as_str();
variants.push(Variant { name, comment });
}
ctx.enums.push(EnumInfo { ctx.enums.push(EnumInfo {
namespace, namespace,
@ -356,6 +386,21 @@ impl Parser {
Ok(()) Ok(())
} }
pub fn parse_enum_variants<'a>(&self, body: &'a str) -> anyhow::Result<Vec<Variant<'a>>> {
let mut variants = Vec::new();
for variant in self.enum_variant_regex.captures_iter(body) {
let variant = variant?;
let comment = variant.name("comment").map(|m| m.as_str());
let name = variant.name("name").unwrap().as_str();
variants.push(Variant { name, comment });
}
Ok(variants)
}
pub fn parse<'a>(&self, text: &'a str, ctx: &mut ParseContext<'a>) -> anyhow::Result<()> { pub fn parse<'a>(&self, text: &'a str, ctx: &mut ParseContext<'a>) -> anyhow::Result<()> {
self.parse_classes(text, ctx)?; self.parse_classes(text, ctx)?;
self.parse_enums(text, ctx)?; self.parse_enums(text, ctx)?;
@ -370,13 +415,28 @@ impl ParseContext<'_> {
typemap: self typemap: self
.classes .classes
.iter() .iter()
.filter_map(|class| { .flat_map(|class| {
Some(typespec::QmlTypeMapping { let Some(qmlname) = class.qml_name else { return Vec::new() };
let mut classes = Vec::new();
classes.push(typespec::QmlTypeMapping {
// filters gadgets // filters gadgets
name: class.qml_name?.to_string(), name: qmlname.to_string(),
cname: class.name.to_string(), cname: class.name.to_string(),
module: Some(module.to_string()), module: Some(module.to_string()),
}) });
// dirty hack to fix unknowns in resolution
if let Some(e) = class.enums.iter().find(|e| e.enum_name == "Enum") {
classes.push(typespec::QmlTypeMapping {
// filters gadgets
name: qmlname.to_string(),
cname: format!("{}::{}", e.namespace, e.enum_name),
module: Some(module.to_string()),
});
}
classes
}) })
.collect(), .collect(),
classes: self classes: self
@ -400,6 +460,28 @@ impl ParseContext<'_> {
properties: class.properties.iter().map(|p| (*p).into()).collect(), properties: class.properties.iter().map(|p| (*p).into()).collect(),
functions: class.invokables.iter().map(|f| f.as_typespec()).collect(), functions: class.invokables.iter().map(|f| f.as_typespec()).collect(),
signals: class.signals.iter().map(|s| s.as_typespec()).collect(), signals: class.signals.iter().map(|s| s.as_typespec()).collect(),
enums: class
.enums
.iter()
.map(|enum_| {
let (description, details) = enum_
.comment
.map(parse_details_desc)
.unwrap_or((None, None));
typespec::Enum {
name: enum_.qml_name.to_string(),
module: Some(module.to_string()),
cname: Some(format!(
"{}::{}",
enum_.namespace, enum_.enum_name
)),
description,
details,
varaints: enum_.variants.iter().map(|v| (*v).into()).collect(),
}
})
.collect(),
}) })
}) })
.collect(), .collect(),

View file

@ -119,11 +119,14 @@ pub fn resolve_types(
list = true; list = true;
} else if ctype.starts_with("QList<") { } else if ctype.starts_with("QList<") {
ctype = &ctype[6..ctype.len() - 1]; ctype = &ctype[6..ctype.len() - 1];
if ctype.ends_with('*') { list = true;
ctype = &ctype[0..ctype.len() - 1]; } else if ctype.starts_with("QVector<") {
ctype = &ctype[8..ctype.len() - 1];
list = true;
} }
list = true; if ctype.ends_with('*') {
ctype = &ctype[0..ctype.len() - 1];
} }
let mut type_ = qmlparamtype(ctype, typespec); let mut type_ = qmlparamtype(ctype, typespec);
@ -218,6 +221,20 @@ pub fn resolve_types(
.map(|signal| (signal.name.clone(), solvesignal(signal, &typespec))) .map(|signal| (signal.name.clone(), solvesignal(signal, &typespec)))
.collect::<HashMap<_, _>>(); .collect::<HashMap<_, _>>();
let coreenum = class.enums.iter().find(|e| e.name == "Enum");
let variants = match coreenum {
Some(e) => e
.varaints
.iter()
.map(|variant| {
(variant.name.clone(), outform::Variant {
details: variant.details.clone(),
})
})
.collect(),
None => HashMap::new(),
};
let type_ = outform::ClassInfo { let type_ = outform::ClassInfo {
superclass, superclass,
description: class.description.clone(), description: class.description.clone(),
@ -225,7 +242,9 @@ pub fn resolve_types(
flags: { flags: {
let mut flags = Vec::new(); let mut flags = Vec::new();
if class.singleton { if coreenum.is_some() {
flags.push(Flag::Enum);
} else if class.singleton {
flags.push(Flag::Singleton); flags.push(Flag::Singleton);
} else if class.uncreatable { } else if class.uncreatable {
flags.push(Flag::Uncreatable); flags.push(Flag::Uncreatable);
@ -236,6 +255,7 @@ pub fn resolve_types(
properties, properties,
functions, functions,
signals, signals,
variants,
}; };
outtypes.insert(mapping.name.clone(), outform::TypeInfo::Class(type_)); outtypes.insert(mapping.name.clone(), outform::TypeInfo::Class(type_));

View file

@ -38,6 +38,7 @@ pub struct Class {
pub properties: Vec<Property>, pub properties: Vec<Property>,
pub functions: Vec<Function>, pub functions: Vec<Function>,
pub signals: Vec<Signal>, pub signals: Vec<Signal>,
pub enums: Vec<Enum>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]

View file

@ -20,11 +20,21 @@
"cname": "qint32", "cname": "qint32",
"module": null "module": null
}, },
{
"name": "int",
"cname": "quint32",
"module": null
},
{ {
"name": "real", "name": "real",
"cname": "qreal", "cname": "qreal",
"module": null "module": null
}, },
{
"name": "real",
"cname": "float",
"module": null
},
{ {
"name": "point", "name": "point",
"cname": "QPointF", "cname": "QPointF",