Skip to content

Commit

Permalink
refactor: change breadcrumb filename template
Browse files Browse the repository at this point in the history
  • Loading branch information
niqodea committed Oct 7, 2024
1 parent 05796d7 commit bf670d4
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 21 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

## 0.2.0

* Change breadcrumb filename template from `.{name}.bc` to `..{name}`

## 0.1.0

* Initial release
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "breadcrumbs"
version = "0.1.0"
version = "0.2.0"
edition = "2021"

[dependencies]
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ ln -s ../../module1/sub1/file1.txt
- **Brittleness**: Modifying the directory structure can not only break links, but may also unintentionally reference existing files, leading to unpredictable issues.

### Breadcrumbs Approach
Using Breadcrumbs, the "upward" path from `sub2` to the `project` directory becomes more intuitive, such as `.project.bc/module1/sub1/file1.txt`.
Using Breadcrumbs, the "upward" path from `sub2` to the `project` directory becomes more intuitive, such as `..project/module1/sub1/file1.txt`.

**How It Works**:
1. **Initialization**: A breadcrumb symlink named `.project.bc` is created inside the `project` directory, pointing to the `project` directory itself (`.`).
2. **Progression**: As we traverse towards `sub2`, at each step, another breadcrumb symlink `.project.bc` is created pointing to the parent directory's breadcrumb (`../.project.bc`).
1. **Initialization**: A breadcrumb symlink named `..project` is created inside the `project` directory, pointing to the `project` directory itself (`.`).
2. **Progression**: As we traverse towards `sub2`, at each step, another breadcrumb symlink `..project` is created pointing to the parent directory's breadcrumb (`../..project`).
3. **End Result**: By the time we reach `sub2`, we've established a trail of breadcrumb symlinks, guiding us from `sub2` back to the `project` directory seamlessly.

After setting up breadcrumbs for the given example, we get the following:
Expand All @@ -52,21 +52,21 @@ project/
├─── module2
│ │
│ ├─── sub2/
│ │ └─── .project.bc -> ../.project.bc
│ │ └─── ..project -> ../..project
│ │
│ └─── .project.bc -> ../.project.bc
│ └─── ..project -> ../..project
└─── .project.bc -> .
└─── ..project -> .
```

Then, to create a symlink inside `sub2` to `file1.txt`, just run:

```sh
ln -s .project.bc/module1/sub1/file1.txt
ln -s ..project/module1/sub1/file1.txt
```

**Benefits**:
- **Clarity**: Instead of puzzling over multiple `../`, the `.project.bc/` breadcrumb explicitly indicates the journey up to the `project` directory.
- **Clarity**: Instead of puzzling over multiple `../`, the `..project/` breadcrumb explicitly indicates the journey up to the `project` directory.
- **Maintainability**: Breadcrumbs simplify symlink updates; adding or removing hierarchy levels often requires minimal to no breadcrumb adjustments.
- **Robustness**: When a significant structural change occurs, the breadcrumb symlink explicitly fails, preventing unintended file references.
- **Documentation**: The presence of breadcrumbs highlights inter-module file references, offering clear insights into file dependencies.
Expand All @@ -80,8 +80,8 @@ The repository also includes a ready-to-use script that showcases how this metho
Download the tarball and extract:

```
wget https://github.com/niqodea/breadcrumbs/releases/download/v0.1.0/breadcrumbs-v0.1.0-x86_64-unknown-linux-gnu.tar.gz
tar -xzf breadcrumbs-v0.1.0-x86_64-unknown-linux-gnu.tar.gz
wget https://github.com/niqodea/breadcrumbs/releases/download/v0.2.0/breadcrumbs-v0.2.0-x86_64-unknown-linux-gnu.tar.gz
tar -xzf breadcrumbs-v0.2.0-x86_64-unknown-linux-gnu.tar.gz
```

then `cp` the `breadcrumbs` binary in the `bin` directory.
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions examples/breadcrumbs/submodule2/..project
1 change: 0 additions & 1 deletion examples/breadcrumbs/submodule2/.project.bc

This file was deleted.

1 change: 1 addition & 0 deletions examples/breadcrumbs/submodule2/sub2/..project
1 change: 0 additions & 1 deletion examples/breadcrumbs/submodule2/sub2/.project.bc

This file was deleted.

2 changes: 1 addition & 1 deletion examples/breadcrumbs/submodule2/sub2/file1.txt
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use walkdir::WalkDir;
#[derive(Parser)]
#[command(
name = "breadcrumbs",
version = "0.1.0",
version = "0.2.0",
about = "Manage breadcrumb symlinks"
)]
struct Args {
Expand Down Expand Up @@ -110,7 +110,7 @@ fn scatter(
Some(name) => name,
None => root_path.file_name().unwrap().to_str().unwrap().to_string(),
};
let breadcrumb_name = format!(".{name}.bc");
let breadcrumb_name = format!("..{name}");

let max_depth = max_depth.unwrap_or(usize::MAX);

Expand Down Expand Up @@ -163,7 +163,7 @@ fn trail(to: PathBuf, from: PathBuf, name: Option<String>) -> Result<String, Str
Some(name) => name,
None => to_path.file_name().unwrap().to_str().unwrap().to_string(),
};
let breadcrumb_name = format!(".{name}.bc");
let breadcrumb_name = format!("..{name}");

let trail_path = from_path.strip_prefix(&to_path).map_err(|_| {
format!(
Expand Down Expand Up @@ -212,11 +212,11 @@ fn rename(breadcrumb: PathBuf, name: String) -> Result<String, String> {
.unwrap()
.to_string();

if !old_breadcrumb_name.starts_with('.') || !old_breadcrumb_name.ends_with(".bc") {
if !old_breadcrumb_name.starts_with("..") {
return Err("not a breadcrumb".to_string());
}

let new_breadcrumb_name = format!(".{name}.bc");
let new_breadcrumb_name = format!("..{name}");

let root_path = breadcrumb
.canonicalize()
Expand Down Expand Up @@ -264,7 +264,7 @@ fn vacuum(breadcrumb: PathBuf) -> Result<String, String> {
.unwrap()
.to_string();

if !breadcrumb_name.starts_with('.') || !breadcrumb_name.ends_with(".bc") {
if !breadcrumb_name.starts_with("..") {
return Err("not a breadcrumb".to_string());
}

Expand Down

0 comments on commit bf670d4

Please sign in to comment.