* Move the design-link path helpers into one module `component_link.rs`'s path rules for links to other design files (base_dir, absolute, relative, stored, resolve, rebased) move, unchanged, into `cmd/design_link.rs` with their test, so other kinds of link (#3891's drawing files, linked parameters #5354, #5113's extract) share them instead of copying or re-exporting them. No change in behaviour. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * design_link: keep base_dir and relative private Only design_link.rs uses them; the module's surface is absolute, stored, resolve and rebased (review on #5420). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * component_link: import Document after the rebase Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Carel Theron <careltheron@Carels-MacBook-Pro.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
103 lines
4.6 KiB
Rust
103 lines
4.6 KiB
Rust
//! Links from a design to other design files (Insert Design's linked components, and the kinds of
|
|
//! link to come): where a link keeps its file and how it finds it again. A link keeps the file
|
|
//! relative to the linking design's folder once that design is saved, so a folder of designs can
|
|
//! move together; before the design has a file the path is kept whole.
|
|
|
|
use std::path::{Component as PathPart, Path, PathBuf};
|
|
|
|
use solidcraft_doc::{DesignLink, Document};
|
|
|
|
/// The folder relative link paths are kept against: the design's own, once it has a file.
|
|
fn base_dir(path: Option<&str>) -> Option<PathBuf> {
|
|
path.and_then(|p| absolute(Path::new(p)).parent().map(Path::to_path_buf))
|
|
}
|
|
|
|
/// A path as given, made absolute when the platform can, with `.` and `..` worked out.
|
|
pub(super) fn absolute(p: &Path) -> PathBuf {
|
|
let full = std::path::absolute(p).unwrap_or_else(|_| p.to_path_buf());
|
|
let mut out = PathBuf::new();
|
|
for c in full.components() {
|
|
match c {
|
|
PathPart::CurDir => {}
|
|
PathPart::ParentDir if matches!(out.components().next_back(), Some(PathPart::Normal(_))) => {
|
|
out.pop();
|
|
}
|
|
c => out.push(c.as_os_str()),
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// `target` relative to the folder `base` (both absolute), when they share a root.
|
|
fn relative(base: &Path, target: &Path) -> Option<PathBuf> {
|
|
let (b, t): (Vec<_>, Vec<_>) = (base.components().collect(), target.components().collect());
|
|
let root = |c: &[PathPart]| c.iter().take_while(|x| matches!(x, PathPart::Prefix(_) | PathPart::RootDir)).count();
|
|
if root(&b) == 0 || b.get(..root(&b)) != t.get(..root(&b)) {
|
|
return None;
|
|
}
|
|
let common = b.iter().zip(&t).take_while(|(x, y)| x == y).count();
|
|
let mut out = PathBuf::new();
|
|
for _ in common..b.len() {
|
|
out.push("..");
|
|
}
|
|
for c in t.iter().skip(common) {
|
|
out.push(c.as_os_str());
|
|
}
|
|
Some(out)
|
|
}
|
|
|
|
/// How a link stores `file` for a design saved at `design` (`None`: not saved yet).
|
|
pub(super) fn stored(design: Option<&str>, file: &Path) -> String {
|
|
let file = absolute(file);
|
|
match base_dir(design).and_then(|d| relative(&d, &file)) {
|
|
// Relative paths are written with `/`, so a design moves between systems with its parts.
|
|
Some(rel) => rel.components().map(|c| c.as_os_str().to_string_lossy().into_owned()).collect::<Vec<_>>().join("/"),
|
|
None => file.to_string_lossy().into_owned(),
|
|
}
|
|
}
|
|
|
|
/// The file a stored link path names, for a design saved at `design`.
|
|
pub(super) fn resolve(design: Option<&str>, link: &str) -> PathBuf {
|
|
let p = Path::new(link);
|
|
match base_dir(design) {
|
|
// Browser storage keys do not let the OS resolve `..` for us.
|
|
Some(d) if p.is_relative() => absolute(&d.join(p)),
|
|
_ => p.to_path_buf(),
|
|
}
|
|
}
|
|
|
|
/// The links of `doc` stored for a design saved at `to` instead of at `from`.
|
|
pub(super) fn rebased(doc: &Document, from: Option<&str>, to: &str) -> std::collections::BTreeMap<u64, DesignLink> {
|
|
doc.links.iter().map(|(c, l)| (*c, DesignLink { path: stored(Some(to), &resolve(from, &l.path)), feature: l.feature })).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn link_paths_are_kept_relative_to_the_design_and_follow_save_as() {
|
|
let root = absolute(Path::new("projects"));
|
|
let part = root.join("parts").join("plate.solidcraft");
|
|
let design = root.join("assembly").join("case.solidcraft");
|
|
let design = design.to_string_lossy().into_owned();
|
|
assert_eq!(stored(Some(&design), &part), "../parts/plate.solidcraft");
|
|
assert_eq!(resolve(Some(&design), "../parts/plate.solidcraft"), part);
|
|
// A design path given relative to the working folder works the same.
|
|
assert_eq!(stored(Some("case.solidcraft"), &absolute(Path::new("parts/plate.solidcraft"))), "parts/plate.solidcraft");
|
|
// Before the design has a file the path is kept whole; saving it makes it relative.
|
|
let unsaved = stored(None, &part);
|
|
assert!(Path::new(&unsaved).is_absolute(), "{unsaved}");
|
|
let mut doc = Document::default();
|
|
doc.links.insert(7, DesignLink { path: unsaved, feature: 3 });
|
|
let moved = rebased(&doc, None, &design);
|
|
assert_eq!(moved.get(&7).map(|l| l.path.as_str()), Some("../parts/plate.solidcraft"));
|
|
let beside = root.join("parts").join("asm.solidcraft").to_string_lossy().into_owned();
|
|
assert_eq!(
|
|
rebased(&Document { links: moved, ..Document::default() }, Some(&design), &beside).get(&7).map(|l| l.path.as_str()),
|
|
Some("plate.solidcraft")
|
|
);
|
|
}
|
|
}
|