tossicat 0.8.0

입력된 단어에 맞게 같이 입력된 토시(조사)를 적절하게 변환하는 라이브러리
Documentation
//! # 에러를 처리하는 모듈
//!
//! ## ValueErrorType
//! 값 에러의 종류를 정의하는 enum.
//! ```text
//! ex) InvalidTossi: 올바른 토시가 아닙니다.
//! ex) InvalidCharacter: 올바른 한글 문자가 아닙니다.
//! ex) LimitLength: 단어 길이가 50을 초과합니다.
//! ```
//!
//! ## ValueError
//! ValueErrorType을 포함하는 에러 구조체.
//!
//! ## ParseErrorType
//! 문장 파싱 에러의 종류를 정의하는 enum.
//! ```text
//! ex) AreNotBalanced: 괄호 짝이 안 맞습니다.
//! ex) TossiIsEmpty: 토시가 비어 있습니다.
//! ```
//!
//! ## ParseError
//! ParseErrorType을 포함하는 에러 구조체.

use std::fmt;

use crate::verifier::MAX_WORD_LEN;

/// ValueErrorType 의 종류
///
/// - InvalidTossi : 올바른 토시가 아닙니다.
/// - InvalidCharacter : 올바른 한글 문자가 아닙니다.
/// - LimitLength : 단어 길이가 50을 초과합니다.
#[derive(Debug, PartialEq)]
pub enum ValueErrorType {
    InvalidTossi,
    InvalidCharacter,
    LimitLength,
}

impl fmt::Display for ValueErrorType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ValueErrorType::InvalidTossi => write!(f, "This value is not correct tossi"),
            ValueErrorType::InvalidCharacter => {
                write!(f, "This value is not a valid Hangul character")
            }
            ValueErrorType::LimitLength => write!(
                f,
                "The length has been exceeded. Set the word length to less than {MAX_WORD_LEN}"
            ),
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct ValueError {
    pub error: ValueErrorType,
    pub description: String,
}

impl ValueError {
    pub fn new(error: ValueErrorType) -> Self {
        ValueError {
            description: error.to_string(),
            error,
        }
    }
}

impl fmt::Display for ValueError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error)
    }
}

/// SentenceType 의 종류
///
/// - AreNotBalanced : 괄호 짝이 안 맞습니다.
/// - IsNotBrace : "{,}" 과 같은 중괄호가 아닙니다.
/// - NestedParentheses : 중복 중괄호가 있습니다. 여기는 1단계만 처리합니다.
/// - SplitTossiWord : 입력된 것에서 토시와 단어로 분리할 수 없습니다.
/// - TossiIsEmpty : 입력한 문장에 들어 있는 중괄호에 토시가 없습니다.
/// - WordIsEmpty : 입력한 문장에 들어 있는 중괄호에 단어가 없습니다.
#[derive(Debug, PartialEq)]
pub enum ParseErrorType {
    InvalidValue(ValueErrorType),
    AreNotBalanced,
    IsNotBrace,
    NestedParentheses,
    SplitTossiWord,
    TossiIsEmpty,
    WordIsEmpty,
}

impl fmt::Display for ParseErrorType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ParseErrorType::InvalidValue(_value_error) => write!(f, "{_value_error}."),
            ParseErrorType::AreNotBalanced => write!(
                f,
                "The sentence can not be parsed. Please check the sentence has incorrect parentheses."
            ),
            ParseErrorType::IsNotBrace => write!(
                f,
                "The sentence can not be parsed. Please set the parentheses as a brace."
            ),
            ParseErrorType::NestedParentheses => {
                write!(f, "The sentence includes Nested Parentheses.")
            }
            ParseErrorType::SplitTossiWord => write!(
                f,
                "The sentence can not be parsed. Please separate words and tossi with a comma."
            ),
            ParseErrorType::TossiIsEmpty => write!(
                f,
                "The sentence can not be parsed. Please fill the tossi section in the parentheses."
            ),
            ParseErrorType::WordIsEmpty => write!(
                f,
                "The sentence can not be parsed. Please fill the word section in the parentheses."
            ),
        }
    }
}

#[derive(Debug, PartialEq)]
pub struct ParseError {
    pub error: ParseErrorType,
    pub description: String,
}

impl ParseError {
    pub fn new(error: ParseErrorType) -> Self {
        ParseError {
            description: error.to_string(),
            error,
        }
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.error)
    }
}