1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! A collection of utilities for making modifications to existing files, e.g. to
//! add/remove lines from them, etc.

use crate::Result;
use regex::Regex;
use std::path::Path;

#[derive(Copy, Clone, PartialEq)]
enum Operation {
    InsertBefore,
    InsertBeforeAll,
    InsertAfter,
    InsertAfterAll,
    Prepend,
    Append,
    RemoveLine,
    RemoveLineAll,
    Replace,
    ReplaceAll,
}

/// Insert the given text into the given file immediately before the first text matched
/// by the given regular expression.
/// Returns an error if any error is encountered when trying to make the update, e.g. if the given
/// file doesn't exist.
/// Otherwise, it will return true if the text was inserted and false if not, i.e. when the given
/// regular expression did not match any line in the file.
///
/// Note: The current implementation does the insertion in memory, so this could run into issues
/// if used on a particularly large file.
pub fn insert_before(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
    modify(file, Some(regex), Some(text), Operation::InsertBefore)
}

/// Like insert_before, but this function will insert the given text before every match rather than
/// just the first one
pub fn insert_before_all(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
    modify(file, Some(regex), Some(text), Operation::InsertBeforeAll)
}

/// Insert the given text into the given file immediately after the first text matched
/// by the given regular expression.
/// Returns an error if any error is encountered when trying to make the update, e.g. if the given
/// file doesn't exist.
/// Otherwise, it will return true if the text was inserted and false if not, i.e. when the given
/// regular expression did not match any line in the file.
///
/// Note: The current implementation does the insertion in memory, so this could run into issues
/// if used on a particularly large file.
pub fn insert_after(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
    modify(file, Some(regex), Some(text), Operation::InsertAfter)
}

/// Like insert_after, but this function will insert the given text after every match rather than
/// just the first one
pub fn insert_after_all(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
    modify(file, Some(regex), Some(text), Operation::InsertBeforeAll)
}

/// Prepend the given text at the start of the given file.
/// Returns an error if any error is encountered when trying to make the update, e.g. if the given
/// file doesn't exist.
pub fn prepend(file: &Path, text: &str) -> Result<()> {
    modify(file, None, Some(text), Operation::Prepend)?;
    Ok(())
}

/// Append the given text at the end of the given file.
/// Returns an error if any error is encountered when trying to make the update, e.g. if the given
/// file doesn't exist.
pub fn append(file: &Path, text: &str) -> Result<()> {
    modify(file, None, Some(text), Operation::Append)?;
    Ok(())
}

/// Remove the first line in the given file that matches the given regex. Returns true if the file
/// was modified.
pub fn remove_line(file: &Path, regex: &Regex) -> Result<bool> {
    modify(file, Some(regex), None, Operation::RemoveLine)
}

/// Like remove_line, but removes all matching lines in the given file.
pub fn remove_line_all(file: &Path, regex: &Regex) -> Result<bool> {
    modify(file, Some(regex), None, Operation::RemoveLineAll)
}

/// Returns Ok(true) if any line in the given file matches the given regex
pub fn contains(file: &Path, regex: &Regex) -> Result<bool> {
    let content = std::fs::read_to_string(file)?;
    for line in content.lines() {
        if regex.is_match(line) {
            return Ok(true);
        }
    }
    Ok(false)
}

/// Replaces the first occurrence of the given regex with the given text, note that the regex is evaluated
/// on a per line basis are therefore cannot span multiple lines
pub fn replace(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
    modify(file, Some(regex), Some(text), Operation::Replace)
}

/// Like replace, but replaces occurences of the given regex on all lines in the file
pub fn replace_all(file: &Path, regex: &Regex, text: &str) -> Result<bool> {
    modify(file, Some(regex), Some(text), Operation::ReplaceAll)
}

fn modify(
    file: &Path,
    regex: Option<&Regex>,
    text: Option<&str>,
    operation: Operation,
) -> Result<bool> {
    let mut output = String::new();
    let orig = std::fs::read_to_string(file)?;
    let mut modified = false;

    let do_all = match operation {
        Operation::InsertAfterAll
        | Operation::InsertBeforeAll
        | Operation::RemoveLineAll
        | Operation::ReplaceAll => true,
        _ => false,
    };

    for line in orig.lines() {
        if modified && !do_all {
            output += line;
        } else {
            match operation {
                Operation::Prepend => {
                    output += text.unwrap();
                    output += line;
                    modified = true;
                }
                Operation::Append => {
                    modified = true;
                }
                _ => {
                    if regex.unwrap().is_match(line) {
                        let m = regex.unwrap().find(line).unwrap();
                        match operation {
                            Operation::InsertBefore | Operation::InsertBeforeAll => {
                                let (first, second) = line.split_at(m.start());
                                output += &format!("{}{}{}", first, text.unwrap(), second);
                            }
                            Operation::InsertAfter | Operation::InsertAfterAll => {
                                let (first, second) = line.split_at(m.end());
                                output += &format!("{}{}{}", first, text.unwrap(), second);
                            }
                            Operation::RemoveLine | Operation::RemoveLineAll => {
                                continue;
                            }
                            Operation::Replace => {
                                output += &regex.unwrap().replace(line, text.unwrap());
                            }
                            Operation::ReplaceAll => {
                                output += &regex.unwrap().replace_all(line, text.unwrap());
                            }
                            _ => unreachable!(),
                        }
                        modified = true;
                    } else {
                        output += line;
                    }
                }
            }
        }
        output += "\n";
    }
    if operation == Operation::Append {
        output += text.unwrap();
    }

    std::fs::write(file, output)?;

    Ok(modified)
}