tgqe 0.0.2

The Great Qin Empire —— A centralized system for integrating and summarizing common compiler front-end library errors and span error output libraries, with optional Ariadne integration.
/**

 *  - Copyright (c) 星灿长风v(Starwindv) 2026/08/03
 *  - License: BSD-3
 *  - Author: 星灿长风v(StarWindv)
 *  - Location: tgqe/src/modules/trans/trans_chumsky.rs
 */
use chumsky::error::{
    Rich, RichReason, Simple,
};
use chumsky::span::SimpleSpan;

use crate::base_types::{
    TgqeCoordinate, TgqeCtx, TgqeErrorInfo,
    TgqeSpan,
};
use crate::enums::TgqeLevelFilter;
use crate::modules::symbols::{
    TGQE_DEFAULT_STR,
    TGQE_ERRTYPE_CHUMSKY_RICH,
    TGQE_ERRTYPE_CHUMSKY_SIMPLE,
    TGQE_FILEPATH_UNKNOWN,
    TGQE_PUBLISHER_CHUMSKY,
};
use crate::trans::unknown_position;

impl From<SimpleSpan> for TgqeSpan {
    fn from(
        chumsky_span: SimpleSpan,
    ) -> Self {
        Self::new(
            TgqeCoordinate::new(
                TGQE_FILEPATH_UNKNOWN,
                -2,
                -2,
                chumsky_span.start as u32,
            ),
            TgqeCoordinate::new(
                TGQE_FILEPATH_UNKNOWN,
                -2,
                -2,
                chumsky_span.end as u32,
            ),
        )
    }
}

impl<T: core::fmt::Display>
    From<Simple<'_, T>> for TgqeErrorInfo
{
    fn from(
        chumsky_simple: Simple<T>,
    ) -> Self {
        let got = chumsky_simple
            .found()
            .map(|tok| tok.to_string())
            .unwrap_or_else(|| {
                TGQE_DEFAULT_STR.to_string()
            });

        Self::new(
            TGQE_ERRTYPE_CHUMSKY_SIMPLE,
            TGQE_DEFAULT_STR,
            &got,
            TgqeLevelFilter::Error,
        )
    }
}

impl<T: core::fmt::Display> From<Rich<'_, T>>
    for TgqeCtx
{
    fn from(value: Rich<'_, T>) -> Self {
        let (expect, got) = match value
            .reason()
        {
            RichReason::ExpectedFound {
                expected,
                found,
            } => (
                expected
                    .iter()
                    .map(ToString::to_string)
                    .collect::<Vec<_>>()
                    .join(", "),
                found
                    .as_ref()
                    .map(|tok| {
                        tok.to_string()
                    })
                    .unwrap_or_else(|| {
                        "end of input"
                            .to_string()
                    }),
            ),
            RichReason::Custom(msg) => (
                TGQE_DEFAULT_STR.to_string(),
                msg.clone(),
            ),
        };

        let hints = value
            .contexts()
            .map(|(label, _)| {
                label.to_string()
            })
            .collect::<Vec<_>>()
            .join(", ");

        Self::new(
            unknown_position(
                value.span().start as u32,
            ),
            TgqeErrorInfo::new(
                TGQE_ERRTYPE_CHUMSKY_RICH,
                &expect,
                &got,
                TgqeLevelFilter::Error,
            ),
            &hints,
            TGQE_DEFAULT_STR,
            TGQE_PUBLISHER_CHUMSKY,
            0,
        )
    }
}