necessist-core 0.1.0-beta.6

necessist-core
Documentation
// smoelius: This file is a slight modification of:
// https://github.com/smoelius/rustfmt_if_chain/blob/557c32c54b0e0f48da2d029a3a8f70db4c8dbf9b/src/offset_based_rewriter/mod.rs

mod impls;

use impls::LazyRewriter;

#[cfg(feature = "check-rewrites")]
use impls::EagerRewriter;

pub trait Interface {
    fn contents(self) -> String;
    fn rewrite(&mut self, start: usize, end: usize, replacement: &str) -> String;
}

#[derive(Debug)]
pub struct OffsetBasedRewriter<'original> {
    lazy: LazyRewriter<'original>,

    #[cfg(feature = "check-rewrites")]
    eager: EagerRewriter,
}

impl<'original> OffsetBasedRewriter<'original> {
    pub fn new(original: &'original str) -> Self {
        Self {
            lazy: LazyRewriter::new(original),

            #[cfg(feature = "check-rewrites")]
            eager: EagerRewriter::new(original),
        }
    }
}

impl<'original> Interface for OffsetBasedRewriter<'original> {
    #[allow(clippy::let_and_return)]
    fn contents(self) -> String {
        let contents = self.lazy.contents();

        #[cfg(feature = "check-rewrites")]
        {
            let contents_comparator = self.eager.contents();
            assert_eq!(contents, contents_comparator);
        }

        contents
    }

    #[allow(clippy::let_and_return)]
    fn rewrite(&mut self, start: usize, end: usize, replacement: &str) -> String {
        let replaced = self.lazy.rewrite(start, end, replacement);

        #[cfg(feature = "check-rewrites")]
        assert_eq!(replaced, self.eager.rewrite(start, end, replacement));

        replaced
    }
}