1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
mod generators;
mod imp;
mod schema;

use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;

/// Macro for easy [`gio::Settings`] key access.
///
/// The main purpose is to reduce the risk of mistyping a key, using
/// the wrong method to access values, inputing incorrect values,
/// and reduce boilerplate Rust code. Additionally, the summary and
/// the default of the value are included in the documentation of each
/// generated methods. This would be beneficial if you tools like
/// [`rust-analyzer`](https://rust-analyzer.github.io/).
///
/// **⚠️ IMPORTANT ⚠️**
///
/// `gio` needs to be in scope, so unless it's one of the direct crate
/// dependencies, you need to import it because `gen_settings` is using
/// it internally. For example:
///
/// ```rust,ignore
/// use gtk::gio;
/// ```
///
/// ### Example
///
/// ```rust,ignore
/// use gsettings_macro::gen_settings;
///
/// #[gen_settings(file = "./tests/io.github.seadve.test.gschema.xml")]
/// pub struct ApplicationSettings;
///
/// let settings = ApplicationSettings::new("io.github.seadve.test");
///
/// // `i` DBus type
/// settings.set_window_width(100);
/// assert_eq!(settings.window_width(), 100)
///
/// // enums
/// settings.set_alert_sound(AlertSound::Glass);
/// assert_eq!(settings.alert_sound(), AlertSound::Glass);
///
/// // bitflags
/// settings.set_space_style(SpaceStyle::BEFORE_COLON | SpaceStyle::BEFORE_COMMA);
/// assert_eq!(
///     settings.space_style(),
///     SpaceStyle::BEFORE_COLON | SpaceStyle::BEFORE_COMMA
/// );
/// ```
///
/// Note: The file path is relative to the project root or where the
/// `Cargo.toml` file is located.
///
/// ### Generated methods
///
/// The procedural macro generates code for each key for following
/// [`gio::Settings`] methods:
///
/// * `set` -> `set_#key`, which panics when writing in a readonly
/// key, and `try_set_#key`, which behaves the same as the original method.
/// * `get` -> `get_#key`
/// * `connect_changed` -> `connect_#key_changed`
/// * `bind` -> `bind_#key`
/// * `create_action` -> `create_#key_action`
///
/// ### Known DBus types
///
/// The setter and getter methods has the following argument and
/// return type, depending on the key's DBus type code specified.
///
/// | DBus type code | argument type  | return type    |
/// | -------------- | -------------- | -------------- |
/// | b              | `bool`         | `bool`         |
/// | i              | `i32`          | `i32`          |
/// | u              | `u32`          | `u32`          |
/// | x              | `i64`          | `i64`          |
/// | t              | `u64`          | `u64`          |
/// | d              | `f64`          | `f64`          |
/// | (ii)           | `(i32, i32`)   | `(i32, i32`)   |
/// | as             | `&[&str]`      | `Vec<String>`  |
/// | s *            | `&str`         | `String`       |
///
/// \* If the schema's key of DBus type `s` has no choice
/// specified, the argument and return types specified
/// above would be true. Otherwise, it will generate an
/// enum, like described in the next section, and use it as argument
/// and return type, instead of `&str` and `String` respectively.
///
/// The code would fail to compile if the DBus type code is not
/// included above. However, it is possible to skip generating a
/// specific key or DBus type code by using `#[gen_settings_skip]`
/// or define a custom argument and return types using `#[gen_settings_define]`.
/// Usage would be further explained in the following sections.
///
/// ### Enums and Flags
///
/// It will also automatically generate enums or flags. If it is
/// an enum, it would generated a normal Rust enum with each nick
/// specified in the GSchema converted to pascal case as an enum variant.
/// The enum would implement both [`ToVariant`](gio::glib::ToVariant)
/// and [`FromVariant`](gio::glib::FromVariant), [`Clone`],
/// [`Hash`], [`PartialEq`], [`Eq`], [`PartialOrd`], and [`Ord`]. On
/// the other hand, if it is a flag, it would generate bitflags
/// same as the bitflags generated by the [`bitflags`] macro with each
/// nick specified in the GSchema converted to screaming snake case as
/// a const flag.
///
/// The generated types, enum or bitflags, would be public and
/// present in the same scope of where the generated settings
/// struct is defined.
///
/// ### Skipping generating code
///
/// This would be helpful if you want to have full control
/// with the key without the macro intervening. For example:
///
/// ```rust,ignore
/// use gsettings_macro::gen_settings;
///
/// #[gen_settings(
///     file = "./tests/io.github.seadve.test.gschema.xml",
///     id = "io.github.seadve.test"
/// )]
/// // Skip generating code for keys with DBus type `(ss)`
/// #[gen_settings_skip(signature = "(ss)")]
/// // Skip generating code for keys with name `some-key-name`
/// #[gen_settings_skip(key_name = "some-key-name")]
/// pub struct Settings;
///
/// impl Settings {
///     pub fn set_some_key_name(value: &std::path::Path) {
///         // some code here
///     }
/// }
/// ```
///
/// ### Defining custom types
///
/// ```rust,ignore
/// use gsettings_macro::gen_settings;
///
/// use std::path::{Path, PathBuf};
///
/// #[gen_settings(file = "./tests/io.github.seadve.test.gschema.xml")]
/// // Define custom argument and return types for keys with type `(ss)`
/// #[gen_settings_define(
///     signature = "(ss)",
///     arg_type = "(&str, &str)",
///     ret_type = "(String, String)"
/// )]
/// // Define custom argument and return types for key with name `cache-dir`
/// #[gen_settings_define(key_name = "cache-dir", arg_type = "&Path", ret_type = "PathBuf")]
/// pub struct SomeAppSettings;
///
/// let settings = SomeAppSettings::new("io.github.seadve.test");
///
/// settings.set_cache_dir(Path::new("/some_dir"));
/// assert_eq!(settings.cache_dir(), PathBuf::from("/some_dir"));
///
/// settings.set_string_tuple(("hi", "hi2"));
/// assert_eq!(settings.string_tuple(), ("hi".into(), "hi2".into()));
/// ```
///
/// The type specified in `arg_type` and `ret_type` has to be on scope or
/// you can specify the full path.
///
/// If you somehow don't want an enum argument and return types for `s` DBus
/// type code with choices. You can also use this to override that behavior.
///
/// Note: The type has to implement both [`ToVariant`](gio::glib::ToVariant)
/// and [`FromVariant`](gio::glib::FromVariant) or it would fail to compile.
///
/// ### Default trait
///
/// The schema id can be specified as an attribute, making it implement
/// [`Default`] and create a `new` constructor without arguments.
/// Otherwise, it won't implement [`Default`] and would require the
/// schema id as an argument in the constructor.
///
/// The following is an example of defining the attribute in the macro:
///
/// ```rust,ignore
/// use gsettings_macro::gen_settings;
///
/// #[gen_settings(
///     file = "./tests/io.github.seadve.test.gschema.xml",
///     id = "io.github.seadve.test"
/// )]
/// pub struct ApplicationSettings;
///
/// // The id is specified above so it is not needed
/// // to specify it in the constructor.
/// let settings = ApplicationSettings::new();
/// let another_instance = ApplicationSettings::default();
/// ```
///
/// [`gio::Settings`]: https://docs.rs/gio/0.15/gio/struct.Settings.html
/// [`gio::glib::ToVariant`]: https://docs.rs/glib/0.15/glib/variant/trait.ToVariant.html
/// [`gio::glib::FromVariant`]: https://docs.rs/glib/0.15/glib/variant/trait.FromVariant.html
/// [`bitflags`]: https://docs.rs/bitflags/1.0/bitflags/macro.bitflags.html
#[proc_macro_attribute]
#[proc_macro_error]
pub fn gen_settings(attr: TokenStream, item: TokenStream) -> TokenStream {
    imp::gen_settings(attr, item)
}