| Crates.io | file-editor |
| lib.rs | file-editor |
| version | 0.2.0 |
| created_at | 2025-04-26 05:00:28.74337+00 |
| updated_at | 2025-04-29 17:17:02.55521+00 |
| description | Clean, elegant API for creating and editing text files |
| homepage | |
| repository | https://github.com/davidwilliam/file-editor |
| max_upload_size | |
| id | 1649894 |
| size | 53,012 |
Clean, chain-friendly text-file editing for Rust • edition 2024 • v0.2.0
file-editor is a zero-dependency library that makes it painless to create,
modify, and query UTF-8 text files.
All mutating methods return &mut self, so edits compose naturally:
use file_editor::Editor;
fn main() -> std::io::Result<()> {
Editor::create("notes.txt")? // new (or truncate existing)
.append("Rust 🦀 is fun\n")
.prepend("# My Notes\n")
.insert_after("# My Notes", "========\n", true)
.replace("fun", "blazingly fast")
.save()?; // explicit flush to disk
Ok(())
}
By default file-editor has no dependencies.
Activate the regex feature to let every pattern-taking method accept a
compiled [regex::Regex]:
file-editor = { version = "0.2.0", features = ["regex"] }
use file_editor::Editor;
use regex::Regex;
fn main() -> std::io::Result<()> {
let re = Regex::new(r"token=\w+").unwrap();
Editor::open("config.env")?
.mask(&re, "***") // redact every `token=…`
.save()?;
Ok(())
}
| Verb | Method(s) | Notes |
|---|---|---|
| Create / open | Editor::create, Editor::open |
create truncates an existing file |
| Rename | rename |
Renames on disk & updates the internal path |
| Prepend / append | prepend, append |
|
| Insert before / after | insert_before, insert_after |
same_indent flag preserves indentation |
| Replace marker | replace_marker |
Optional same_indent |
| Search pattern | find_lines |
Returns 1-based line numbers |
| Erase / replace / mask | erase, replace, mask |
Operate on all occurrences in the buffer |
| Save | save |
Writes only when the buffer is dirty |
Planned > streaming mode, companion CLI…
# stable crate (no regex):
cargo add file-editor
# with regex support:
cargo add file-editor --features regex
Requires Rust 1.85 or newer (edition 2024).
| Task | Command |
|---|---|
| Build | cargo build |
| Run tests & doctests | cargo test |
| Format | cargo fmt --all |
| Lint (deny warnings) | cargo clippy --all-targets -- -D warnings |
| Coverage (HTML + terminal) | cargo llvm-cov --workspace --open (install once: cargo install cargo-llvm-cov) |
| Generate docs | cargo doc --no-deps --open |
CI runs the same commands on every PR (see .github/workflows/ci.yml).
Everything under examples/ writes into examples/sandbox/ (ignored by Git):
| Example | Demonstrates |
|---|---|
basic.rs |
End-to-end mini workflow |
create.rs |
create + append |
open_rename.rs |
open + rename |
prepend.rs, append.rs |
Header / footer insertion |
insert_before.rs |
Block insertion above a marker |
insert_after.rs |
Block insertion below a marker |
replace_marker.rs |
In-place marker replacement |
find_lines.rs |
Pattern search |
erase.rs |
Deleting fragments |
replace.rs, mask.rs |
Global replacements / masking |
cargo run --example insert_after
cargo fmt --check && cargo clippy --all-targets -- -D warningstests/ (use the tempfile crate)All contributions are released under the MIT license.
Copyright © 2025
Licensed under the MIT License. See LICENSE for details.