pr 03.03
This commit is contained in:
@@ -125,6 +125,33 @@ impl ImportRef {
|
||||
pub fn new(project: ProjectAlias, module: ModulePath, item: ItemName) -> Self {
|
||||
Self { project, module, item }
|
||||
}
|
||||
|
||||
/// Parse a PBS-style import `from` string and an `item` into an `ImportRef`.
|
||||
///
|
||||
/// Expected `from_str` format: "@<alias>:<module_path>", e.g., "@sdk:input/testing".
|
||||
/// `item_str` must be a single symbol name like "Test" or "ServiceName".
|
||||
pub fn parse_pbs_import<S1: AsRef<str>, S2: AsRef<str>>(from_str: S1, item_str: S2) -> Result<Self, CanonError> {
|
||||
let (project, module) = parse_pbs_from_string(from_str)?;
|
||||
let item = ItemName::new(item_str)?;
|
||||
Ok(ImportRef { project, module, item })
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse the PBS `from` string in the canonical format `@<alias>:<module_path>`.
|
||||
/// Returns `(ProjectAlias, ModulePath)` or a `CanonError` if invalid.
|
||||
pub fn parse_pbs_from_string<S: AsRef<str>>(from_str: S) -> Result<(ProjectAlias, ModulePath), CanonError> {
|
||||
let s = from_str.as_ref().trim();
|
||||
// Must start with '@' and contain a ':' separating alias and module path
|
||||
let rest = s.strip_prefix('@').ok_or(CanonError::InvalidStart("ImportFrom"))?;
|
||||
let mut parts = rest.splitn(2, ':');
|
||||
let alias = parts.next().unwrap_or("");
|
||||
let module = parts.next().unwrap_or("");
|
||||
if alias.is_empty() || module.is_empty() {
|
||||
return Err(CanonError::Empty("ImportFrom"));
|
||||
}
|
||||
let project = ProjectAlias::new(alias)?;
|
||||
let module = ModulePath::parse(module)?;
|
||||
Ok((project, module))
|
||||
}
|
||||
|
||||
/// Export kind — generic, FE-agnostic.
|
||||
@@ -270,4 +297,28 @@ mod tests {
|
||||
assert!(ItemName::new("_Test").is_err());
|
||||
assert!(ItemName::new("Te-st").is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_pbs_from_string_valid_and_normalization() {
|
||||
let (proj, module) = parse_pbs_from_string("@sdk:input/testing").unwrap();
|
||||
assert_eq!(proj.as_str(), "sdk");
|
||||
assert_eq!(module.as_str(), "input/testing");
|
||||
|
||||
// Normalization of module path
|
||||
let (_, module2) = parse_pbs_from_string("@sdk:/Input/./Testing/").unwrap();
|
||||
assert_eq!(module2.as_str(), "input/testing");
|
||||
|
||||
// Windows-style separators normalize
|
||||
let (_, module3) = parse_pbs_from_string("@abc:foo\\bar").unwrap();
|
||||
assert_eq!(module3.as_str(), "foo/bar");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_pbs_from_string_invalid_forms() {
|
||||
assert!(parse_pbs_from_string("").is_err());
|
||||
assert!(parse_pbs_from_string("sdk:input/testing").is_err()); // missing '@'
|
||||
assert!(parse_pbs_from_string("@Sdk:input/testing").is_err()); // invalid alias
|
||||
assert!(parse_pbs_from_string("@sdk:").is_err()); // empty module
|
||||
assert!(parse_pbs_from_string("@sdk:..").is_err()); // parent segments forbidden
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user