Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add library crate #195

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
119 changes: 119 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
pub mod error;
pub mod input;
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>,
}

pub struct ReplaceConfBuilder {
inner: ReplaceConf,
}

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

impl ReplaceConf {
pub fn builder() -> ReplaceConfBuilder {
ReplaceConfBuilder {
inner: Self::default(),
}
}
}

impl ReplaceConfBuilder {
pub fn set_source(mut self, source: Source) -> Self {
self.inner.source = source;
self
}

pub fn build(self) -> ReplaceConf {
self.inner
}
}

/// For example:
///
/// ```no_run
/// use sd::{replace, ReplaceConf, Source};
///
/// replace("foo", "bar", ReplaceConf {
/// source: Source::with_files(vec!["./foo.md", "./bar.md", "./foobar.md"]),
/// ..ReplaceConf::default()
/// });
/// ```
pub fn replace<F, R>(
find: F,
replace_with: R,
replace_conf: ReplaceConf,
) -> Result<()>
where
F: Into<String>,
R: Into<String>,
{
App::new(
replace_conf.source,
Replacer::new(
find.into(),
replace_with.into(),
replace_conf.literal_mode,
replace_conf.flags,
replace_conf.replacements,
replace_conf.no_swap,
)?,
)
.run(replace_conf.preview)?;
Ok(())
}

#[cfg(test)]
mod tests {
use std::{fs::{File, self}, io::{Write, Read}};

use super::*;

#[test]
fn it_works() {
// Create files for test.
for p in &vec!["./for-test-foo", "./for-test-bar"] {
let mut f = File::create(p).unwrap();
f.write_all(b"foo bar foz").unwrap();
}

// Run it.
let replace_conf = ReplaceConf::builder()
.set_source(Source::with_files(vec!["./for-test-foo", "./for-test-bar"]))
.build();
replace("foo", "bar", replace_conf).unwrap();

// Assert and cleanup.
for p in &vec!["./for-test-foo", "./for-test-bar"] {
let mut f = File::open(p).unwrap();
let mut buf = vec![];
f.read_to_end(&mut buf).unwrap();
assert_eq!(buf, b"bar bar foz");
fs::remove_file(p).unwrap();
}
}
}
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