Skip to main content

nichlink_run_method/authoring/parse/
parse.rs

1//! Parsing and rendering helpers for authored registration faces.
2//! 注册面创作文件的解析与渲染辅助函数。
3//!
4//! The pure source-text transformations live in the kernel `authoring`
5//! module; this shim keeps the historical
6//! `nichlink_run_method::authoring::parse` path and re-implements the two
7//! filesystem-bound entry points as thin wrappers.
8//! 纯源码文本变换位于 kernel 的 `authoring` 模块;本 shim 保留
9//! `nichlink_run_method::authoring::parse` 历史路径,并把两个绑定文件
10//! 系统的入口重新实现为薄包装。
11
12use std::fs;
13use std::path::{Path, PathBuf};
14
15pub use nichlink::authoring::parse::*;
16
17use super::validation::{normalized_path, source_root};
18
19/// Read the parent's declared `kind` from its attached source file.
20/// 从父注册面的附属源文件读取它声明的 `kind`。
21pub(super) fn kind_from_source_path(source: &str) -> Option<String> {
22    let path = source_root().join(source);
23    let text = fs::read_to_string(path).ok()?;
24    nichlink::authoring::parse::kind_from_source_text(&text)
25}
26
27pub(super) fn source_path_from_file(path: &Path) -> String {
28    if let Ok(relative) = path.strip_prefix(source_root()) {
29        return normalized_path(relative);
30    }
31    let roots = ["compile_error_demo", "control", "engine", "trimmed_core"];
32    let mut components = path.components();
33    while let Some(component) = components.next() {
34        let text = component.as_os_str().to_string_lossy();
35        if roots.contains(&text.as_ref()) {
36            let mut result = PathBuf::from(text.as_ref());
37            result.extend(components.map(|part| part.as_os_str()));
38            return normalized_path(&result);
39        }
40    }
41    normalized_path(path)
42}
43
44pub(super) fn rule_syntax_for_source(path: &Path) -> Result<String, String> {
45    let Some(parent) = path.parent() else {
46        return Ok("ANY".to_owned());
47    };
48    let canonical = parent.join("registry_rule/registry_rule.rs");
49    let legacy = parent.join("registry/rules/rules.rs");
50    let Ok(text) = fs::read_to_string(&canonical).or_else(|_| fs::read_to_string(&legacy)) else {
51        return Ok("ANY".to_owned());
52    };
53    Ok(nichlink::authoring::parse::rule_syntax_from_text(&text))
54}