Skip to main content

agent_first_http/shared/
path.rs

1//! Lexical path helpers shared by artifact and cookie-jar resolution.
2
3use std::path::{Component, PathBuf};
4
5/// Make `path` absolute and collapse `.`/`..` lexically (no filesystem
6/// access, no symlink resolution). Relative paths are joined onto the current
7/// working directory. Used so the JSON contract always reports absolute
8/// artifact and cookie-jar paths regardless of how `--out`/`--cookie-jar`
9/// were passed.
10#[must_use]
11pub(crate) fn absolute_lexical(path: PathBuf) -> PathBuf {
12    let mut out = if path.is_absolute() {
13        PathBuf::new()
14    } else {
15        std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))
16    };
17    for component in path.components() {
18        match component {
19            Component::Prefix(prefix) => out.push(prefix.as_os_str()),
20            Component::RootDir => out.push(component.as_os_str()),
21            Component::CurDir => {}
22            Component::ParentDir => {
23                out.pop();
24            }
25            Component::Normal(part) => out.push(part),
26        }
27    }
28    out
29}