use itertools::Itertools;
use std::convert::Infallible;
use super::TransformField;
use crate::actions::transforms::result::{OptionUnwrapTransformResultExt, TransformResult};
#[derive(Debug)]
pub struct Trim;
impl TransformField for Trim {
type Err = Infallible;
async fn transform_field(
&mut self,
value: Option<&str>,
) -> Result<TransformResult<String>, Self::Err> {
Ok(value.map(trim).unwrap_or_empty())
}
}
fn trim(s: &str) -> String {
s.trim()
.lines()
.map(|line| line.trim().to_owned())
.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one_line() {
const S: &str = "\n\n\n \nHello, World! \n \n";
assert_eq!(trim(S), "Hello, World!");
}
#[test]
fn multi_line() {
const S: &str = "\n\n\n \nHello, \n World! \n \n";
assert_eq!(trim(S), "Hello,\nWorld!");
}
}