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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! Face field editing and registry-rule derivation.
//! 注册面字段编辑与注册规范推导。
//!
//! Rendering lives in `render.rs`; this file keeps the edit and rule half.
//! 渲染位于 `render.rs`;本文件保留编辑与规范部分。
use std::path::{Path, PathBuf};
use super::super::FaceManifest;
use crate::RuntimeCheckSpec;
use crate::authoring::parse::*;
use crate::authoring::validation::{
legacy_rule_path_for_source, rule_path_for_source, source_root, validate_kind_name,
validate_name,
};
#[path = "render.rs"]
mod render;
impl FaceManifest {
pub(crate) fn rule_source_path(&self) -> Result<PathBuf, String> {
let source = Path::new(self.values.get("source").map(String::as_str).unwrap_or(""));
let directory = source
.parent()
.ok_or_else(|| "generated face has no module directory".to_owned())?;
if let Some(declared) = self.values.get("registry_rule_path")
&& !declared.trim().is_empty()
{
let path = Path::new(declared);
let relative = path.strip_prefix("src/").unwrap_or(path);
return Ok(source_root().join(relative));
}
let canonical = source_root()
.join(directory)
.join("registry_rule/registry_rule.rs");
if canonical.is_file() {
return Ok(canonical);
}
// Existing faces authored before the dedicated folder remain valid.
// 兼容早期使用 registry/rules/rules.rs 的注册面。
Ok(source_root().join(
Path::new(&legacy_rule_path_for_source(
self.values.get("source").map(String::as_str).unwrap_or(""),
))
.strip_prefix("src/")
.unwrap_or_else(|_| Path::new("")),
))
}
pub(crate) fn rule_source_path_string(&self) -> Result<String, String> {
if let Some(declared) = self.values.get("registry_rule_path")
&& !declared.trim().is_empty()
{
return Ok(declared.clone());
}
Ok(rule_path_for_source(
self.values.get("source").map(String::as_str).unwrap_or(""),
))
}
pub(crate) fn rule_module_path(&self) -> Result<String, String> {
let rule_path = self.rule_source_path_string()?;
let module = rule_path
.strip_prefix("src/")
.unwrap_or(&rule_path)
.rsplit_once('/')
.map_or_else(|| rule_path.as_str(), |(directory, _)| directory)
.replace('/', "::");
Ok(format!("crate::{module}::REGISTRATION_RULE"))
}
pub(crate) fn render_rule_source(&self) -> Result<Option<String>, String> {
if self.values.get("needs_registry").map(String::as_str) != Some("true") {
return Ok(None);
}
let rule = render_registration_rule(
self.values
.get("registration_rule")
.map(String::as_str)
.unwrap_or("ANY"),
)?;
Ok(Some(format!(
"//! Registry rule.\n//! 注册规范。\n\nuse crate::RegistrationRule;\n\npub const REGISTRATION_RULE: RegistrationRule = {rule};\n"
)))
}
pub(crate) fn edit(&mut self, field: &str, value: &str) -> Result<(), String> {
match field {
"kind"
| "preset"
| "parts"
| "handle"
| "name_zh"
| "name_en"
| "summary_zh"
| "summary_en"
| "params"
| "stable_name"
| "exports"
| "registry_name"
| "getting_from_other_registry"
| "registration_rule"
| "admission"
| "handle_traits"
| "handle_contracts"
| "part_traits"
| "part_contracts"
| "requires"
| "provides"
| "runtime_checks"
| "flow"
| "flow_provider" => {
if value.contains(['\n', '\r']) {
return Err("field value cannot contain a newline".to_owned());
}
if field == "registry_name" {
validate_name(value)?;
}
if field == "kind" {
validate_kind_name(value)?;
}
if field == "registration_rule" {
parse_registration_rule_owned(value)?;
}
if field == "admission" {
parse_admission_owned(value)?;
}
if field == "getting_from_other_registry" {
parse_optional_source(value)?;
}
if field == "requires" {
parse_requirements(value)?;
}
if field == "runtime_checks" {
RuntimeCheckSpec::parse_list(value)?;
}
if field == "flow" {
parse_flow_value(value)?;
}
if field == "flow_provider" && !value.trim().is_empty() {
syn::parse_str::<syn::Path>(value)
.map_err(|_| "flow_provider must be a Rust type path".to_owned())?;
}
let key = if field == "admission" {
"admission"
} else {
field
};
self.values.insert(key.to_owned(), value.to_owned());
Ok(())
}
"needs_registry" if matches!(value, "true" | "false") => {
self.values.insert(field.to_owned(), value.to_owned());
Ok(())
}
"needs_registry" => Err("needs_registry must be `true` or `false`".to_owned()),
_ => Err(format!(
"field `{field}` is not editable; use the registration face fields"
)),
}
}
/// Whether this face owns a generated registry-rule source file.
/// 该注册面是否拥有生成的注册规则源文件。
///
/// A face that declares a rule of its own needs one even when it owns no
/// child registry. The two callers used to disagree — the renderer emitted
/// `registry_rule: <module>::REGISTRATION_RULE` for a custom rule while the
/// editor wrote the module only for a registry owner — so saving a leaf face
/// with a custom rule produced source that referenced a file nobody wrote.
/// 声明了自己规则的注册面即使不拥有子注册机也需要它。两个调用方过去口径不一致——
/// 渲染器会为自定义规则发射 `registry_rule: <模块>::REGISTRATION_RULE`,而编辑器
/// 只为拥有注册机的面写该模块——于是保存一个带自定义规则的叶子面会生成引用不存在
/// 文件的源码。
pub(crate) fn owns_rule_source(&self) -> bool {
let rule = self
.values
.get("registration_rule")
.map_or("", String::as_str)
.trim();
self.values.get("needs_registry").map(String::as_str) == Some("true")
|| (!rule.is_empty() && rule != "ANY")
}
/// Whether `registry_rule:` is redundant because the canonical sibling rule
/// derives it.
/// `registry_rule:` 是否因为同目录规范规则已经推导出它而多余。
///
/// Only a face that owns a registry derives the field: the resolver defaults
/// every other face to the permissive rule, which is what a child face wants,
/// since the rule that governs it belongs to its parent. A face whose rule was
/// moved elsewhere keeps naming it.
/// 只有拥有注册机的面才推导该字段:解析器把其余面默认成宽松规则——这正是子面的需要,
/// 因为管它的规则属于它的父级。规则被挪到别处的面仍然写出它。
pub(crate) fn derives_rule_from_the_sibling(&self) -> bool {
if self.values.get("needs_registry").map(String::as_str) != Some("true") {
return false;
}
let source = self.values.get("source").map(String::as_str).unwrap_or("");
let declared = self
.values
.get("registry_rule_path")
.map_or("", String::as_str)
.trim();
declared.is_empty() || declared == rule_path_for_source(source)
}
}
#[cfg(test)]
mod rule_source_ownership_tests {
use super::super::FaceManifest;
use std::collections::BTreeMap;
/// The renderer and the editor must agree on when a face owns a generated
/// rule source: a custom rule needs one even for a leaf face, and a face that
/// owns a registry needs one even with `ANY`. They used to disagree, so
/// saving a leaf face with a custom rule wrote source referencing a module
/// nobody created.
/// 渲染器与编辑器必须对"何时拥有生成的规则源"口径一致:自定义规则即使在叶子面上也
/// 需要它,而拥有注册机的面即使规则是 `ANY` 也需要它。两者过去不一致,于是保存一个
/// 带自定义规则的叶子面会写出引用不存在模块的源码。
#[test]
fn a_custom_rule_makes_a_leaf_face_own_its_rule_source() {
let manifest = |needs_registry: &str, rule: &str| {
let mut manifest = FaceManifest {
values: BTreeMap::new(),
};
manifest
.values
.insert("needs_registry".to_owned(), needs_registry.to_owned());
manifest
.values
.insert("registration_rule".to_owned(), rule.to_owned());
manifest
};
assert!(!manifest("false", "ANY").owns_rule_source());
assert!(manifest("false", "parts:paint").owns_rule_source());
assert!(manifest("true", "ANY").owns_rule_source());
}
/// A face that owns a registry and keeps its rule at the canonical path does
/// not repeat that path to reference the rule: the declaration resolves it
/// from the sibling module instead. A leaf face, or one whose rule lives
/// elsewhere, still names it.
/// 拥有注册机、且规则就在规范路径上的面不必为了引用规则而重复该路径:声明改为从同目录
/// 模块推导它。叶子面、或规则放在别处的面仍然写出它。
#[test]
fn a_registry_face_derives_the_rule_it_keeps_beside_it() {
let manifest = |needs_registry: &str, declared_path: &str| {
let mut manifest = FaceManifest {
values: BTreeMap::new(),
};
// `source` is relative to the package `src` directory, which is the
// form the manifest keeps and `rule_path_for_source` consumes.
// `source` 相对包的 `src` 目录,这正是清单保存、`rule_path_for_source`
// 消费的形式。
manifest
.values
.insert("source".to_owned(), "widget/widget.rs".to_owned());
manifest
.values
.insert("needs_registry".to_owned(), needs_registry.to_owned());
manifest
.values
.insert("registration_rule".to_owned(), "parts:paint".to_owned());
if !declared_path.is_empty() {
manifest
.values
.insert("registry_rule_path".to_owned(), declared_path.to_owned());
}
manifest
};
// Canonical path, whether it is written out or left to the default.
assert!(manifest("true", "").derives_rule_from_the_sibling());
assert!(
manifest("true", "src/widget/registry_rule/registry_rule.rs")
.derives_rule_from_the_sibling()
);
// A rule that was moved elsewhere keeps its explicit reference.
assert!(!manifest("true", "src/widget/other/rules.rs").derives_rule_from_the_sibling());
// A face that owns no registry defaults to the permissive rule, so it
// cannot derive the sibling without changing what it enforces.
assert!(!manifest("false", "").derives_rule_from_the_sibling());
assert!(
!manifest("false", "src/widget/registry_rule/registry_rule.rs")
.derives_rule_from_the_sibling()
);
}
}