Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ clap = { version = "4.2.7", features = ["derive", "deprecated", "wrap_help"] }
[dev-dependencies]
assert_cmd = "1.0.3"
anyhow = "1.0.38"
rstest = "0.17.0"

[build-dependencies]
clap = "4.2.7"
Expand Down
7 changes: 6 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ w - match full words only
/// use captured values like $1, $2, etc.
pub replace_with: String,

#[arg(long)]
/// Overwrite file instead of creating tmp file and swaping atomically
pub no_swap: bool,

/// The path to file(s). This is optional - sd can also read from STDIN.
///{n}{n}Note: sd modifies files in-place by default. See documentation for
///
/// Note: sd modifies files in-place by default. See documentation for
/// examples.
pub files: Vec<std::path::PathBuf>,
}
Expand Down
22 changes: 20 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,31 @@ use crate::{Error, Replacer, Result};

use is_terminal::IsTerminal;

/// The source files we regard as the input.
#[derive(Debug)]
pub(crate) enum Source {
pub enum Source {
Stdin,
Files(Vec<PathBuf>),
}

impl Source {
pub(crate) fn recursive() -> Result<Self> {
pub fn with_file<T>(file: T) -> Self
where T: Into<PathBuf>
{
Self::with_files(vec![file])
}

pub fn with_files<T>(files: Vec<T>) -> Self
where T: Into<PathBuf>
{
Self::Files(
files.into_iter()
.map(|file_path| file_path.into())
.collect()
)
}

pub fn recursive() -> Result<Self> {
Ok(Self::Files(
ignore::WalkBuilder::new(".")
.hidden(false)
Expand All @@ -36,6 +53,7 @@ impl App {
pub(crate) fn new(source: Source, replacer: Replacer) -> Self {
Self { source, replacer }
}

pub(crate) fn run(&self, preview: bool) -> Result<()> {
let is_tty = std::io::stdout().is_terminal();

Expand Down
55 changes: 55 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
pub mod input;
pub mod error;
pub mod replacer;
pub mod utils;

pub use self::input::Source;
pub use error::{Error, Result};
use input::App;
pub(crate) use replacer::Replacer;

pub struct ReplaceConf {
pub source: Source,
pub preview: bool,
pub no_swap: bool,
pub literal_mode: bool,
pub flags: Option<String>,
pub replacements: Option<usize>,
}

impl Default for ReplaceConf {
fn default() -> Self {
Self {
source: Source::Stdin,
preview: false,
no_swap: false,
literal_mode: false,
flags: None,
replacements: None,
}
}
}

/// For example:
///
/// ```no_run
/// replace("foo", "bar", ReplaceConf {
/// source: Source::with_files(vec!["./foo.md", "./bar.md", "./foobar.md"]),
/// ..ReplaceConf::default()
/// })
/// ```
pub fn replace(find: String, replace_with: String, replace_conf: ReplaceConf) -> Result<()> {
Copy link

@fujidaiti fujidaiti Jun 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thank you! One thing I'm wondering, though, is it possible to use &str instead of String as the type for find and replace_with? Otherwise, we would always have to create and move Strings. In the current implementation, for example, replace("foo", "bar", ...) is not possible.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course.

App::new(
replace_conf.source,
Replacer::new(
find,
replace_with,
replace_conf.literal_mode,
replace_conf.flags,
replace_conf.replacements,
replace_conf.no_swap,
)?,
)
.run(replace_conf.preview)?;
Ok(())
}
28 changes: 9 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
mod cli;
mod error;
mod input;

pub(crate) mod replacer;
pub(crate) mod utils;

pub(crate) use self::input::{App, Source};
pub(crate) use error::{Error, Result};
use replacer::Replacer;

use clap::Parser;

use sd::{Result, Source, replace, ReplaceConf};

fn main() -> Result<()> {
let options = cli::Options::parse();

Expand All @@ -22,16 +15,13 @@ fn main() -> Result<()> {
Source::Stdin
};

App::new(
replace(options.find, options.replace_with, ReplaceConf {
source,
Replacer::new(
options.find,
options.replace_with,
options.literal_mode,
options.flags,
options.replacements,
)?,
)
.run(options.preview)?;
preview: options.preview,
no_swap: options.no_swap,
literal_mode: options.literal_mode,
flags: options.flags,
replacements: options.replacements,
})?;
Ok(())
}
66 changes: 44 additions & 22 deletions src/replacer.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use crate::{utils, Error, Result};
use regex::bytes::Regex;
use std::{fs, fs::File, io::prelude::*, path::Path};
use std::{
fs,
fs::{File, OpenOptions},
io::{prelude::*, SeekFrom},
path::Path,
};

pub(crate) struct Replacer {
regex: Regex,
replace_with: Vec<u8>,
is_literal: bool,
replacements: usize,
no_swap: bool,
}

impl Replacer {
Expand All @@ -16,6 +22,7 @@ impl Replacer {
is_literal: bool,
flags: Option<String>,
replacements: Option<usize>,
no_swap: bool,
) -> Result<Self> {
let (look_for, replace_with) = if is_literal {
(regex::escape(&look_for), replace_with.into_bytes())
Expand Down Expand Up @@ -61,6 +68,7 @@ impl Replacer {
replace_with,
is_literal,
replacements: replacements.unwrap_or(0),
no_swap,
})
}

Expand Down Expand Up @@ -128,29 +136,42 @@ impl Replacer {
return Ok(());
}

let source = File::open(path)?;
let meta = fs::metadata(path)?;
let mmap_source = unsafe { Mmap::map(&source)? };
let replaced = self.replace(&mmap_source);

let target = tempfile::NamedTempFile::new_in(
path.parent()
.ok_or_else(|| Error::InvalidPath(path.to_path_buf()))?,
)?;
let file = target.as_file();
file.set_len(replaced.len() as u64)?;
file.set_permissions(meta.permissions())?;

if !replaced.is_empty() {
let mut mmap_target = unsafe { MmapMut::map_mut(file)? };
mmap_target.deref_mut().write_all(&replaced)?;
mmap_target.flush_async()?;
}
if self.no_swap {
let mut source =
OpenOptions::new().read(true).write(true).open(path)?;
let mut buffer = Vec::new();
source.read_to_end(&mut buffer)?;

let replaced = self.replace(&buffer);

source.seek(SeekFrom::Start(0))?;
source.write_all(&replaced)?;
source.set_len(replaced.len() as u64)?;
} else {
let source = File::open(path)?;
let meta = fs::metadata(path)?;
let mmap_source = unsafe { Mmap::map(&source)? };
let replaced = self.replace(&mmap_source);

let target = tempfile::NamedTempFile::new_in(
path.parent()
.ok_or_else(|| Error::InvalidPath(path.to_path_buf()))?,
)?;
let file = target.as_file();
file.set_len(replaced.len() as u64)?;
file.set_permissions(meta.permissions())?;

if !replaced.is_empty() {
let mut mmap_target = unsafe { MmapMut::map_mut(file)? };
mmap_target.deref_mut().write_all(&replaced)?;
mmap_target.flush_async()?;
}

drop(mmap_source);
drop(source);
drop(mmap_source);
drop(source);
target.persist(fs::canonicalize(path)?)?;
}

target.persist(fs::canonicalize(path)?)?;
Ok(())
}
}
Expand All @@ -173,6 +194,7 @@ mod tests {
literal,
flags.map(ToOwned::to_owned),
None,
false,
)
.unwrap();
assert_eq!(
Expand Down
Loading