{"id":21586062,"url":"https://github.com/magiclen/path-dedot","last_synced_at":"2025-04-10T20:15:20.386Z","repository":{"id":46978690,"uuid":"145141243","full_name":"magiclen/path-dedot","owner":"magiclen","description":"A library for extending `Path` and `PathBuf` in order to parse the path which contains dots.","archived":false,"fork":false,"pushed_at":"2023-09-09T11:34:02.000Z","size":80,"stargazers_count":4,"open_issues_count":1,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T17:55:20.306Z","etag":null,"topics":["linux","macos","path","rust","windows"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magiclen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-08-17T16:08:57.000Z","updated_at":"2023-05-06T04:53:19.000Z","dependencies_parsed_at":"2024-11-24T18:00:38.190Z","dependency_job_id":null,"html_url":"https://github.com/magiclen/path-dedot","commit_stats":{"total_commits":100,"total_committers":3,"mean_commits":"33.333333333333336","dds":"0.020000000000000018","last_synced_commit":"09163c3f5a3bab42a34d6f4e7cb508e1f109dd16"},"previous_names":[],"tags_count":51,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fpath-dedot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fpath-dedot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fpath-dedot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magiclen%2Fpath-dedot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magiclen","download_url":"https://codeload.github.com/magiclen/path-dedot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248176830,"owners_count":21060133,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["linux","macos","path","rust","windows"],"created_at":"2024-11-24T15:12:31.655Z","updated_at":"2025-04-10T20:15:20.365Z","avatar_url":"https://github.com/magiclen.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"Path Dedot\n====================\n\n[![CI](https://github.com/magiclen/path-dedot/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/path-dedot/actions/workflows/ci.yml)\n\nThis is a library for extending `Path` and `PathBuf` in order to parse the path which contains dots.\n\nPlease read the following examples to know the parsing rules.\n\n## Examples\n\nIf a path starts with a single dot, the dot means your program's **current working directory** (CWD).\n\n```rust\nuse std::path::Path;\nuse std::env;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"./path/to/123/456\");\n\nassert_eq!(Path::join(env::current_dir().unwrap().as_path(), Path::new(\"path/to/123/456\")).to_str().unwrap(), p.parse_dot().unwrap().to_str().unwrap());\n```\n\nIf a path starts with a pair of dots, the dots means the parent of the CWD. If the CWD is **root**, the parent is still **root**.\n\n```rust\nuse std::path::Path;\nuse std::env;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"../path/to/123/456\");\n\nlet cwd = env::current_dir().unwrap();\n\nlet cwd_parent = cwd.parent();\n\nmatch cwd_parent {\n   Some(cwd_parent) =\u003e {\n      assert_eq!(Path::join(\u0026cwd_parent, Path::new(\"path/to/123/456\")).to_str().unwrap(), p.parse_dot().unwrap().to_str().unwrap());\n   }\n   None =\u003e {\n      assert_eq!(Path::join(Path::new(\"/\"), Path::new(\"path/to/123/456\")).to_str().unwrap(), p.parse_dot().unwrap().to_str().unwrap());\n   }\n}\n```\n\nIn addition to starting with, the **Single Dot** and **Double Dots** can also be placed to other positions. **Single Dot** means noting and will be ignored. **Double Dots** means the parent.\n\n```rust\nuse std::path::Path;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"/path/to/../123/456/./777\");\n\nassert_eq!(\"/path/123/456/777\", p.parse_dot().unwrap().to_str().unwrap());\n```\n\n```rust\nuse std::path::Path;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"/path/to/../123/456/./777/..\");\n\nassert_eq!(\"/path/123/456\", p.parse_dot().unwrap().to_str().unwrap());\n```\n\nYou should notice that `parse_dot` method does **not** aim to get an **absolute path**. A path which does not start with a `MAIN_SEPARATOR`, **Single Dot** and **Double Dots**, will not have each of them after the `parse_dot` method is used.\n\n```rust\nuse std::path::Path;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"path/to/../123/456/./777/..\");\n\nassert_eq!(\"path/123/456\", p.parse_dot().unwrap().to_str().unwrap());\n```\n\n**Double Dots** which is not placed at the start cannot get the parent beyond the original path. Why not? With this constraint, you can insert an absolute path to the start as a virtual root in order to protect your file system from being exposed.\n\n```rust\nuse std::path::Path;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"path/to/../../../../123/456/./777/..\");\n\nassert_eq!(\"123/456\", p.parse_dot().unwrap().to_str().unwrap());\n```\n\n```rust\nuse std::path::Path;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"/path/to/../../../../123/456/./777/..\");\n\nassert_eq!(\"/123/456\", p.parse_dot().unwrap().to_str().unwrap());\n```\n\n### Starting from a given current working directory\n\nWith the `parse_dot_from` function, you can provide the current working directory that the relative paths should be resolved from.\n\n```rust\nuse std::env;\nuse std::path::Path;\n\nuse path_dedot::*;\n\nlet p = Path::new(\"../path/to/123/456\");\nlet cwd = env::current_dir().unwrap();\n\nprintln!(\"{}\", p.parse_dot_from(cwd).unwrap().to_str().unwrap());\n```\n\n## Caching\n\nBy default, the `parse_dot` method creates a new `PathBuf` instance of the CWD every time in its operation. The overhead is obvious. Although it allows us to safely change the CWD at runtime by the program itself (e.g. using the `std::env::set_current_dir` function) or outside controls (e.g. using gdb to call `chdir`), we don't need that in most cases.\n\nIn order to parse paths with better performance, this crate provides three ways to cache the CWD.\n\n### once_cell_cache\n\nEnabling the `once_cell_cache` feature can let this crate use `once_cell` to cache the CWD. It's thread-safe and does not need to modify any code, but once the CWD is cached, it cannot be changed anymore at runtime.\n\n```toml\n[dependencies.path-dedot]\nversion = \"*\"\nfeatures = [\"once_cell_cache\"]\n```\n\n### lazy_static_cache\n\nEnabling the `lazy_static_cache` feature can let this crate use `lazy_static` to cache the CWD. It's thread-safe and does not need to modify any code, but once the CWD is cached, it cannot be changed anymore at runtime.\n\n```toml\n[dependencies.path-dedot]\nversion = \"*\"\nfeatures = [\"lazy_static_cache\"]\n```\n\n### unsafe_cache\n\nEnabling the `unsafe_cache` feature can let this crate use a mutable static variable to cache the CWD. It allows the program to change the CWD at runtime by the program itself, but it's not thread-safe.\n\nYou need to use the `update_cwd` function to initialize the CWD first. The function should also be used to update the CWD after the CWD is changed.\n\n```toml\n[dependencies.path-dedot]\nversion = \"*\"\nfeatures = [\"unsafe_cache\"]\n```\n\n```rust\nuse std::path::Path;\n\nuse path_dedot::*;\n\nunsafe {\n    update_cwd();\n}\n\nlet p = Path::new(\"./path/to/123/456\");\n\nprintln!(\"{}\", p.parse_dot().unwrap().to_str().unwrap());\n\nstd::env::set_current_dir(\"/\").unwrap();\n\nunsafe {\n    update_cwd();\n}\n\nprintln!(\"{}\", p.parse_dot().unwrap().to_str().unwrap());\n```\n\n## Benchmark\n\n#### No-cache\n\n```bash\ncargo bench\n```\n\n#### once_cell_cache\n\n```bash\ncargo bench --features once_cell_cache\n```\n\n#### lazy_static_cache\n\n```bash\ncargo bench --features lazy_static_cache\n```\n\n#### unsafe_cache\n\n```bash\ncargo bench --features unsafe_cache\n```\n\n## Crates.io\n\nhttps://crates.io/crates/path-dedot\n\n## Documentation\n\nhttps://docs.rs/path-dedot\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fpath-dedot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagiclen%2Fpath-dedot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagiclen%2Fpath-dedot/lists"}