poexam 0.0.10

Blazingly fast PO linter.
// SPDX-FileCopyrightText: 2026 Sébastien Helleu <flashcode@flashtux.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later

//! Implementation of the `pipes` rule: check missing/extra pipes.

use crate::checker::Checker;
use crate::diagnostic::{Diagnostic, Severity};
use crate::po::entry::Entry;
use crate::po::message::Message;
use crate::rules::rule::RuleChecker;

pub struct PipesRule;

impl RuleChecker for PipesRule {
    fn name(&self) -> &'static str {
        "pipes"
    }

    fn description(&self) -> &'static str {
        "Check for missing or extra pipe characters in translation."
    }

    fn is_default(&self) -> bool {
        true
    }

    fn is_check(&self) -> bool {
        true
    }

    /// Check for missing or extra pipes in the translation.
    ///
    /// Wrong entry:
    /// ```text
    /// msgid "syntax: ./test -f|-h|-v"
    /// msgstr "syntaxe : ./test -f|-h"
    /// ```
    ///
    /// Correct entry:
    /// ```text
    /// msgid "syntax: ./test -f|-h|-v"
    /// msgstr "syntaxe : ./test -f|-h|-v"
    /// ```
    ///
    /// Diagnostics reported:
    /// - [`info`](Severity::Info): `missing pipes '|' (# / #)`
    /// - [`info`](Severity::Info): `extra pipes '|' (# / #)`
    fn check_msg(
        &self,
        checker: &Checker,
        _entry: &Entry,
        msgid: &Message,
        msgstr: &Message,
    ) -> Vec<Diagnostic> {
        let id_count = msgid.value.matches('|').count();
        let str_count = msgstr.value.matches('|').count();
        let msg = match id_count.cmp(&str_count) {
            std::cmp::Ordering::Equal => return vec![],
            std::cmp::Ordering::Greater => {
                format!("missing pipes '|' ({id_count} / {str_count})")
            }
            std::cmp::Ordering::Less => {
                format!("extra pipes '|' ({id_count} / {str_count})")
            }
        };
        self.new_diag(checker, Severity::Info, msg)
            .map(|d| {
                d.with_msgs_hl(
                    msgid,
                    msgid
                        .value
                        .match_indices('|')
                        .map(|(idx, value)| (idx, idx + value.len())),
                    msgstr,
                    msgstr
                        .value
                        .match_indices('|')
                        .map(|(idx, value)| (idx, idx + value.len())),
                )
            })
            .into_iter()
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{diagnostic::Diagnostic, rules::rule::Rules};

    fn check_pipes(content: &str) -> Vec<Diagnostic> {
        let mut checker = Checker::new(content.as_bytes());
        let rules = Rules::new(vec![Box::new(PipesRule {})]);
        checker.do_all_checks(&rules);
        checker.diagnostics
    }

    #[test]
    fn test_no_pipes() {
        let diags = check_pipes(
            r#"
msgid "tested"
msgstr "testé"
"#,
        );
        assert!(diags.is_empty());
    }

    #[test]
    fn test_pipes_ok() {
        let diags = check_pipes(
            r#"
msgid "tested|here"
msgstr "testé|ici"
"#,
        );
        assert!(diags.is_empty());
    }

    #[test]
    fn test_pipes_error_noqa() {
        let diags = check_pipes(
            r#"
#, noqa:pipes
msgid "tested|"
msgstr "testé"
"#,
        );
        assert!(diags.is_empty());
    }

    #[test]
    fn test_pipes_error() {
        let diags = check_pipes(
            r#"
msgid "tested|"
msgstr "testé"

msgid "tested"
msgstr "testé|"
"#,
        );
        assert_eq!(diags.len(), 2);
        let diag = &diags[0];
        assert_eq!(diag.severity, Severity::Info);
        assert_eq!(diag.message, "missing pipes '|' (1 / 0)");
        let diag = &diags[1];
        assert_eq!(diag.severity, Severity::Info);
        assert_eq!(diag.message, "extra pipes '|' (0 / 1)");
    }
}