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_winnow.rs
 */
use core::fmt;

use winnow::error::{
    ContextError, InputError, ParseError,
};

use crate::base_types::{
    TgqeCtx, TgqeErrorInfo,
};
use crate::enums::TgqeLevelFilter;
use crate::modules::symbols::{
    TGQE_DEFAULT_STR,
    TGQE_ERRTYPE_WINNOW_CONTEXT,
    TGQE_ERRTYPE_WINNOW_INPUT,
    TGQE_ERRTYPE_WINNOW_PARSE,
    TGQE_PUBLISHER_WINNOW,
};
use crate::trans::unknown_position;

impl<I: fmt::Display, E: fmt::Display>
    From<ParseError<I, E>> for TgqeCtx
{
    fn from(
        winnow_pe: ParseError<I, E>,
    ) -> Self {
        let offset =
            winnow_pe.offset() as u32;
        let got =
            winnow_pe.inner().to_string();
        let hints =
            winnow_pe.input().to_string();

        Self::new(
            unknown_position(offset),
            TgqeErrorInfo::new(
                TGQE_ERRTYPE_WINNOW_PARSE,
                TGQE_DEFAULT_STR,
                &got,
                TgqeLevelFilter::Error,
            ),
            &hints,
            TGQE_DEFAULT_STR,
            TGQE_PUBLISHER_WINNOW,
            0,
        )
    }
}

impl From<ContextError> for TgqeCtx {
    fn from(
        winnow_ce: ContextError,
    ) -> Self {
        let hints = winnow_ce
            .context()
            .map(|ctx| ctx.to_string())
            .collect::<Vec<_>>()
            .join("\n");
        let got = winnow_ce
            .cause()
            .map(|cause| cause.to_string())
            .unwrap_or_else(|| {
                TGQE_DEFAULT_STR.to_string()
            });

        Self::new(
            unknown_position(0),
            TgqeErrorInfo::new(
                TGQE_ERRTYPE_WINNOW_CONTEXT,
                TGQE_DEFAULT_STR,
                &got,
                TgqeLevelFilter::Error,
            ),
            &hints,
            TGQE_DEFAULT_STR,
            TGQE_PUBLISHER_WINNOW,
            0,
        )
    }
}

impl<I: Clone + fmt::Display>
    From<InputError<I>> for TgqeCtx
{
    fn from(
        winnow_ie: InputError<I>,
    ) -> Self {
        let got =
            winnow_ie.input.to_string();

        Self::new(
            unknown_position(0),
            TgqeErrorInfo::new(
                TGQE_ERRTYPE_WINNOW_INPUT,
                TGQE_DEFAULT_STR,
                &got,
                TgqeLevelFilter::Error,
            ),
            TGQE_DEFAULT_STR,
            TGQE_DEFAULT_STR,
            TGQE_PUBLISHER_WINNOW,
            0,
        )
    }
}