Skip to content

Commit ce4e462

Browse files
gtk4-macros: Make the blueprint compiler feature opt-in
Otherwise tests would fail if the user doesn't have blueprint-compiler installed locally
1 parent 823a29b commit ce4e462

File tree

8 files changed

+31
-22
lines changed

8 files changed

+31
-22
lines changed

gtk4-macros/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ rust-version = "1.64"
1515
proc-macro = true
1616

1717
[features]
18+
default = []
1819
xml_validation = ["quick-xml"]
20+
blueprint = []
1921

2022
[dependencies]
2123
anyhow = "1.0"

gtk4-macros/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Currently, the minimum supported Rust version is `1.64.0`.
2121
| Feature | Description |
2222
| --- | ----------- |
2323
| `xml_validation` | Check the existence of `#[template_child]` fields in the UI file. Only works with `#[template(string = "")]` |
24+
| `blueprint` | Adds blueprint usage support in `#[template(string = "")]` |
2425

2526
### See Also
2627

gtk4-macros/src/attribute_parser.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,26 @@ pub enum TemplateSource {
1616
File(String),
1717
Resource(String),
1818
Xml(String),
19+
#[cfg(feature = "blueprint")]
1920
Blueprint(String),
2021
}
2122

2223
impl TemplateSource {
2324
fn from_string_source(source: String) -> Result<Self> {
2425
for c in source.chars() {
26+
#[cfg(feature = "blueprint")]
2527
if c.is_ascii_alphabetic() {
2628
// blueprint code starts with some alphabetic letters
2729
return Ok(Self::Blueprint(source));
2830
} else if c == '<' {
2931
// xml tags starts with '<' symbol
3032
return Ok(Self::Xml(source));
3133
}
34+
#[cfg(not(feature = "blueprint"))]
35+
if c == '<' {
36+
// xml tags starts with '<' symbol
37+
return Ok(Self::Xml(source));
38+
}
3239
}
3340
bail!("Unknown lanuage")
3441
}

gtk4-macros/src/composite_template_derive.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use syn::Data;
1111
use std::collections::HashMap;
1212
use std::string::ToString;
1313

14-
use crate::{attribute_parser::*, blueprint::*, util::*};
14+
#[cfg(feature = "blueprint")]
15+
use crate::blueprint::*;
16+
use crate::{attribute_parser::*, util::*};
1517

1618
fn gen_set_template(source: &TemplateSource, crate_ident: &proc_macro2::Ident) -> TokenStream {
1719
match source {
@@ -33,6 +35,7 @@ fn gen_set_template(source: &TemplateSource, crate_ident: &proc_macro2::Ident) -
3335
#template.as_bytes(),
3436
);
3537
},
38+
#[cfg(feature = "blueprint")]
3639
TemplateSource::Blueprint(blueprint) => {
3740
let template =
3841
compile_blueprint(blueprint.as_bytes()).expect("can't compile blueprint");

gtk4-macros/src/lib.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! The crate aims to provide useful macros to use with the GTK 4 Rust bindings.
66
77
mod attribute_parser;
8+
#[cfg(feature = "blueprint")]
89
mod blueprint;
910
mod composite_template_derive;
1011
mod template_callbacks_attribute;
@@ -41,15 +42,12 @@ use syn::{parse_macro_input, DeriveInput};
4142
///
4243
/// ```no_run
4344
/// # fn main() {}
44-
/// use gtk::prelude::*;
45-
/// use gtk::glib;
46-
/// use gtk::CompositeTemplate;
47-
/// use gtk::subclass::prelude::*;
45+
/// use gtk::{glib, prelude::*, subclass::prelude::*};
4846
///
4947
/// mod imp {
5048
/// use super::*;
5149
///
52-
/// #[derive(Debug, Default, CompositeTemplate)]
50+
/// #[derive(Debug, Default, gtk::CompositeTemplate)]
5351
/// #[template(file = "test/template.ui")]
5452
/// pub struct MyWidget {
5553
/// #[template_child]
@@ -90,17 +88,16 @@ use syn::{parse_macro_input, DeriveInput};
9088
/// ```
9189
///
9290
/// The [`CompositeTemplate`] macro can also be used with [Blueprint](https://jwestman.pages.gitlab.gnome.org/blueprint-compiler/)
91+
/// if the feature `blueprint` is enabled.
9392
///
94-
/// ```rust
95-
/// ```no_run
93+
/// ```ignore
9694
/// # fn main() {}
97-
/// use gtk::prelude::*;
98-
/// use gtk::glib;
99-
/// use gtk::CompositeTemplate;
100-
/// use gtk::subclass::prelude::*;
95+
/// use gtk::{glib, prelude::*, subclass::prelude::*};
10196
///
10297
/// mod imp {
103-
/// #[derive(Debug, Default, CompositeTemplate)]
98+
/// use super::*;
99+
///
100+
/// #[derive(Debug, Default, gtk::CompositeTemplate)]
104101
/// #[template(string = "
105102
/// template MyWidget : Widget {
106103
/// Label label {
@@ -234,15 +231,12 @@ pub fn composite_template_derive(input: TokenStream) -> TokenStream {
234231
///
235232
/// ```no_run
236233
/// # fn main() {}
237-
/// use gtk::prelude::*;
238-
/// use gtk::glib;
239-
/// use gtk::CompositeTemplate;
240-
/// use gtk::subclass::prelude::*;
234+
/// use gtk::{glib, prelude::*, subclass::prelude::*};
241235
///
242236
/// mod imp {
243237
/// use super::*;
244238
///
245-
/// #[derive(Debug, Default, CompositeTemplate)]
239+
/// #[derive(Debug, Default, gtk::CompositeTemplate)]
246240
/// #[template(file = "test/template_callbacks.ui")]
247241
/// pub struct MyWidget {
248242
/// #[template_child]

gtk4-macros/tests/templates.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn template_child_without_attribute() {
200200
let _: MyWidget3 = glib::Object::new();
201201
}
202202

203-
#[cfg(target_os = "linux")]
203+
#[cfg(feature = "blueprint")]
204204
mod imp4 {
205205
use super::*;
206206

@@ -246,13 +246,13 @@ mod imp4 {
246246
impl WidgetImpl for MyWidget4 {}
247247
}
248248

249-
#[cfg(target_os = "linux")]
249+
#[cfg(feature = "blueprint")]
250250
glib::wrapper! {
251251
pub struct MyWidget4(ObjectSubclass<imp4::MyWidget4>) @extends gtk::Widget;
252252
}
253253

254254
#[gtk::test]
255-
#[cfg(target_os = "linux")]
255+
#[cfg(feature = "blueprint")]
256256
fn blueprint_template() {
257257
let _: MyWidget4 = glib::Object::new();
258258
}

gtk4/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ v4_6 = ["ffi/v4_6", "v4_4", "gdk/v4_6", "gsk/v4_6", "pango/v1_50"]
2323
v4_8 = ["ffi/v4_8", "v4_6", "gdk/v4_8"]
2424
v4_10 = ["ffi/v4_10", "v4_8", "gdk/v4_10"]
2525
xml_validation = ["gtk4-macros/xml_validation"]
26+
blueprint = ["gtk4-macros/blueprint"]
2627
unsafe-assume-initialized = []
2728

2829
# Versions from https://gitlab.gnome.org/GNOME/gnome-build-meta/-/tree/gnome-43/elements/sdk

gtk4/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ gtk = { git = "https://github.com/gtk-rs/gtk4-rs.git", package = "gtk4" }
191191
| `gnome_43` | Enable all version feature flags of this crate and its dependencies to match the GNOME 43 SDK |
192192
| `gnome_42` | Enable all version feature flags of this crate and its dependencies to match the GNOME 42 SDK |
193193
| `unsafe-assume-initialized` | Disable checks that gtk is initialized, for use in C ABI libraries |
194-
| `xml_validation` | Enable `xml_validation` feature of gtk4-macros
194+
| `xml_validation` | Enable `xml_validation` feature of gtk4-macros |
195+
| `blueprint` | Enable `blueprint` feature of gtk4-macros |
195196

196197
### See Also
197198

0 commit comments

Comments
 (0)