nichlink_run_method/authoring/parse/
parse.rs1use std::fs;
13use std::path::{Path, PathBuf};
14
15pub use nichlink::authoring::parse::*;
16
17use super::validation::{normalized_path, source_root};
18
19pub(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}