Files
marqdo/src/host/path.rs
T
cflmy 9331af78e2 release(2.8.0): 合入 CHANGELOG/README/发版说明/skills prove 任务;CI 链接指向 doc/releases
- CHANGELOG.md:v2.8.0 条目(中英双语);
- README.md:版本行 2.7.1→2.8.0 + 愿景第 6 条(知识可证明);
- doc/releases/v2.8.0.md:双语发版说明(亮点/用法/已知限制);
- skills/marqdo-dev/index.mq.md:新增 prove 任务(路径/目录模式/
  require_lean/json;参数用 + 行声明升参,check 与 narrative-ratio 通过);
- 设计文档 §12.5 标记已合入;发版链接统一指向 doc/releases/v2.8.0.md。
2026-11-18 10:29:26 +08:00

118 lines
3.2 KiB
Rust
Executable File

//! Path algebra for Mid M2 (`lib/path`) — no I/O.
use crate::host::as_text;
use std::path::{Component, Path, PathBuf};
use crate::value::Value;
fn path_to_text(p: &Path) -> String {
if p.as_os_str().is_empty() {
".".into()
} else {
p.to_string_lossy().into_owned()
}
}
pub fn join(a: &Value, b: &Value) -> Result<Value, String> {
let a = as_text(a, "a")?;
let b = as_text(b, "b")?;
let out = Path::new(a).join(b);
Ok(Value::Text(path_to_text(&out)))
}
pub fn split(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
let mut parts = Vec::new();
for c in Path::new(p).components() {
match c {
Component::RootDir | Component::CurDir | Component::ParentDir => {
parts.push(Value::Text(c.as_os_str().to_string_lossy().into_owned()));
}
Component::Prefix(prefix) => {
parts.push(Value::Text(prefix.as_os_str().to_string_lossy().into_owned()));
}
Component::Normal(s) => parts.push(Value::Text(s.to_string_lossy().into_owned())),
}
}
Ok(Value::List(parts))
}
pub fn file_name(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
Ok(Value::Text(
Path::new(p)
.file_name()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default(),
))
}
pub fn parent(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
let parent = Path::new(p).parent();
Ok(Value::Text(match parent {
Some(par) if !par.as_os_str().is_empty() => path_to_text(par),
_ => ".".into(),
}))
}
pub fn extension(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
Ok(Value::Text(
Path::new(p)
.extension()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_default(),
))
}
pub fn normalize(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
let mut out = PathBuf::new();
for c in Path::new(p).components() {
match c {
Component::CurDir => {}
Component::ParentDir => {
match out.components().next_back() {
Some(Component::Normal(_)) => {
out.pop();
}
Some(Component::RootDir) | Some(Component::Prefix(_)) => {}
_ => out.push(".."),
}
}
other => out.push(other.as_os_str()),
}
}
Ok(Value::Text(path_to_text(&out)))
}
pub fn is_absolute(path: &Value) -> Result<Value, String> {
let p = as_text(path, "path")?;
Ok(Value::Bool(Path::new(p).is_absolute()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn join_and_name() {
let j = join(
&Value::Text("a/b".into()),
&Value::Text("c.txt".into()),
)
.unwrap();
assert_eq!(j, Value::Text("a/b/c.txt".into()));
assert_eq!(
file_name(&Value::Text("a/b/c.txt".into())).unwrap(),
Value::Text("c.txt".into())
);
assert_eq!(
extension(&Value::Text("c.txt".into())).unwrap(),
Value::Text("txt".into())
);
}
}