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
mod key;
mod schema;
use anyhow::{anyhow, Context, Result};
use proc_macro_error::{emit_call_site_error, emit_call_site_warning, proc_macro_error};
use quote::quote;
use syn::{AttributeArgs, ItemStruct, Lit, Meta, NestedMeta};
use std::{fs::File, io::Read};
use crate::schema::{Root as SchemaFileRoot, Schema};
fn parse_schema_path_and_id(args: &AttributeArgs) -> Result<(String, String)> {
let mut schema_path = None;
let mut schema_id = None;
for nested_meta in args.iter() {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
if name_value.path.is_ident("file") {
if let Lit::Str(ref lit_str) = name_value.lit {
schema_path.replace(lit_str.value());
} else {
emit_call_site_error!("expected a string literal after `file = `");
}
} else if name_value.path.is_ident("id") {
if let Lit::Str(ref lit_str) = name_value.lit {
schema_id.replace(lit_str.value());
} else {
emit_call_site_error!("expected a string literal after `id = `");
}
}
}
}
let schema_path = schema_path.ok_or_else(|| anyhow!("expected a file meta"))?;
let schema_id = schema_id.ok_or_else(|| anyhow!("expected a file meta"))?;
Ok((schema_path, schema_id))
}
fn parse_schema(args: &AttributeArgs) -> Result<Schema> {
let (schema_path, schema_id) = parse_schema_path_and_id(args)?;
use quickxml_to_serde::{Config, JsonArray, JsonType, NullValue};
let mut xml_content = String::new();
File::open(&schema_path)
.with_context(|| format!("failed to open file at {}", schema_path))?
.read_to_string(&mut xml_content)?;
let config = Config::new_with_custom_values(true, "", "#text", NullValue::Ignore)
.add_json_type_override(
"schemalist/schema",
JsonArray::Always(JsonType::AlwaysString),
)
.add_json_type_override(
"schemalist/schema/key",
JsonArray::Always(JsonType::AlwaysString),
)
.add_json_type_override(
"schemalist/schema/key/default",
JsonArray::Infer(JsonType::AlwaysString),
)
.add_json_type_override(
"schemalist/schema/key/summary",
JsonArray::Infer(JsonType::AlwaysString),
)
.add_json_type_override(
"schemalist/schema/key/description",
JsonArray::Infer(JsonType::AlwaysString),
)
.add_json_type_override(
"schemalist/schema/key/choices",
JsonArray::Infer(JsonType::AlwaysString),
)
.add_json_type_override(
"schemalist/schema/key/choices/choice",
JsonArray::Always(JsonType::AlwaysString),
);
let json_value = quickxml_to_serde::xml_string_to_json(xml_content, &config)?;
let root: SchemaFileRoot =
serde_json::from_value(json_value).expect("failed to parse schema file");
let mut schema_list = root.schemalist.into_vec();
if schema_list.len() != 1 {
emit_call_site_error!("schema file must have a single schema");
}
let mut schema = schema_list
.pop()
.ok_or_else(|| anyhow!("a schema from file"))?;
schema.id = schema_id;
Ok(schema)
}
#[proc_macro_attribute]
#[proc_macro_error]
pub fn gen_settings(
attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attr = syn::parse_macro_input!(attr as AttributeArgs);
let item = syn::parse_macro_input!(item as ItemStruct);
if !item.fields.is_empty() {
emit_call_site_warning!("any struct field would be ignored")
}
let schema = parse_schema(&attr).expect("failed to parse schema");
let schema_id = schema.id;
let ident = item.ident;
let mut aux_token_stream = proc_macro2::TokenStream::new();
let mut keys_token_stream = proc_macro2::TokenStream::new();
for key in &schema.keys {
for token_stream in key.aux() {
aux_token_stream.extend(token_stream);
}
keys_token_stream.extend(key.to_token_stream())
}
let expanded = quote! {
#aux_token_stream
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct #ident(gio::Settings);
impl #ident {
pub fn new() -> Self {
Self(gio::Settings::new(#schema_id))
}
#keys_token_stream
}
impl std::ops::Deref for #ident {
type Target = gio::Settings;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for #ident {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Default for #ident {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for #ident {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Debug::fmt(&self.0, f)
}
}
};
proc_macro::TokenStream::from(expanded)
}