#[derive(Clone, Copy, Debug)]
pub enum Trim {
All,
#[cfg(any(feature = "markdown", feature = "code"))]
PreserveIndentation,
None,
}
#[cfg(any(feature = "markdown", feature = "code"))]
const NEWLINES: [char; 2] = ['\n', '\r'];
impl Trim {
pub fn trim(self, offset: usize, chunk: &str) -> (usize, &str) {
match self {
Self::All => {
let diff = chunk.len() - chunk.trim_start().len();
(offset + diff, chunk.trim())
}
#[cfg(any(feature = "markdown", feature = "code"))]
Self::PreserveIndentation => {
if chunk.trim().contains(NEWLINES) {
let diff = chunk.len() - chunk.trim_start_matches(NEWLINES).len();
(offset + diff, chunk.trim_start_matches(NEWLINES).trim_end())
} else {
Self::All.trim(offset, chunk)
}
}
Self::None => (offset, chunk),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn trim_all() {
let chunk = " hello world ";
let (offset, chunk) = Trim::All.trim(0, chunk);
assert_eq!(offset, 2);
assert_eq!(chunk, "hello world");
}
#[cfg(any(feature = "markdown", feature = "code"))]
#[test]
fn trim_indentation_fallback() {
let chunk = " hello world ";
let (offset, chunk) = Trim::PreserveIndentation.trim(0, chunk);
assert_eq!(offset, 2);
assert_eq!(chunk, "hello world");
}
#[cfg(any(feature = "markdown", feature = "code"))]
#[test]
fn trim_indentation_preserved() {
let chunk = "\n hello\n world ";
let (offset, chunk) = Trim::PreserveIndentation.trim(0, chunk);
assert_eq!(offset, 1);
assert_eq!(chunk, " hello\n world");
}
}