ecformat 0.1.1

command line tool to keep files correct in respect of your EditorConfig
Documentation
// SPDX-FileCopyrightText: Contributors to ecformat project <https://codeberg.org/BaumiCoder/ecformat>
//
// SPDX-License-Identifier: BlueOak-1.0.0

//! Module with helper functions for test code.

use std::{fs, path::PathBuf};

use ec4rs::PropertyKey;

/// Module with helper functions for the tests regarding the `charset` property of EditorConfig.
#[allow(dead_code)] // this specific code is not used by all Integration Tests (= crates)
pub(crate) mod charset {
    use std::path::Path;

    use ec4rs::property::Charset;

    /// Determine the charset used in a test file from its name.
    /// The file names (without extension) have to be exactly the name of the charset.
    pub fn get_charset_from_file_name(file_path: &Path) -> Result<Charset, String> {
        match file_path.file_stem().unwrap().to_str().unwrap() {
            "utf-8" => Ok(Charset::Utf8),
            "latin1" => Ok(Charset::Latin1),
            "utf-16le" => Ok(Charset::Utf16Le),
            "utf-16be" => Ok(Charset::Utf16Be),
            "utf-8-bom" => Ok(Charset::Utf8Bom),
            other => Err(String::from(other)),
        }
    }
}

/// Module with helper functions for the tests regarding the `end_of_line` property of EditorConfig.
#[allow(dead_code)] // this specific code is not used by all Integration Tests (= crates)
pub(crate) mod end_of_line {
    use std::path::Path;

    use ec4rs::property::EndOfLine;

    /// Determine the end of line characters used in a test file from its name.
    /// The file names (without extension) have to contain a "-" separated list
    /// of end of line shorthands.
    pub fn get_end_of_line_from_file_name(file_path: &Path) -> Vec<EndOfLine> {
        file_path
            .file_stem()
            .unwrap()
            .to_str()
            .unwrap()
            .split("-")
            .map(|name| match name {
                "LF" => EndOfLine::Lf,
                "CRLF" => EndOfLine::CrLf,
                "CR" => EndOfLine::Cr,
                other => panic!(
                    "Unknown end of line character short hand '{other}' found in file name of {}",
                    file_path.display()
                ),
            })
            .collect()
    }
}

/// Helper to access test files
pub(crate) struct TestFileHelper {
    dir_name: &'static str,
}

impl TestFileHelper {
    /// Creates a test file helper for the given EditorConfig property
    pub(crate) fn new<T: PropertyKey>() -> TestFileHelper {
        TestFileHelper { dir_name: T::key() }
    }

    /// Provides an iterator over all files of the test file helper
    /// without a `.editorconfig` or `.license` extension.
    pub(crate) fn get_test_file_paths(&self) -> impl Iterator<Item = PathBuf> {
        let test_dir = fs::read_dir(self.get_test_dir()).expect("tests resources have to exist");
        test_dir
            .into_iter()
            .map(|entry| entry.expect("test files have to be accessible").path())
            .filter(|path| {
                path.extension()
                    .is_none_or(|ext| ext != "editorconfig" && ext != "license")
            })
            .inspect(|path| println!("Test file {}", path.display()))
    }

    /// Returns the directory for the test files of the test file helper
    pub(crate) fn get_test_dir(&self) -> PathBuf {
        ["tests", "resources", self.dir_name].into_iter().collect()
    }
}