Skip to main content

wp_error/
parse_error.rs

1use derive_more::From;
2use orion_error::ErrorCode;
3use orion_error::StructError;
4use orion_error::UvsFrom;
5use orion_error::UvsReason;
6use serde::Serialize;
7use thiserror::Error;
8
9/*
10fn translate_position(input: &[u8], index: usize) -> (usize, usize) {
11    if input.is_empty() {
12        return (0, index);
13    }
14
15    let safe_index = index.min(input.len() - 1);
16    let column_offset = index - safe_index;
17    let index = safe_index;
18
19    let nl = input[0..index]
20        .iter()
21        .rev()
22        .enumerate()
23        .find(|(_, b)| **b == b'\n')
24        .map(|(nl, _)| index - nl - 1);
25    let line_start = match nl {
26        Some(nl) => nl + 1,
27        None => 0,
28    };
29    let line = input[0..line_start].iter().filter(|b| **b == b'\n').count();
30
31    let column = std::str::from_utf8(&input[line_start..=index])
32        .map(|s| s.chars().count() - 1)
33        .unwrap_or_else(|_| index - line_start);
34    let column = column + column_offset;
35
36    (line, column)
37}
38*/
39
40#[derive(Error, Debug, Clone, PartialEq, Serialize, From)]
41pub enum OMLCodeReason {
42    #[error("{0}")]
43    Syntax(String),
44    #[from(skip)]
45    #[error("{0}")]
46    NotFound(String),
47    #[error("{0}")]
48    Uvs(UvsReason),
49}
50impl ErrorCode for OMLCodeReason {
51    fn error_code(&self) -> i32 {
52        crate::codes::SysErrorCode::sys_code(self) as i32
53    }
54}
55
56pub type OMLCodeError = StructError<OMLCodeReason>;
57
58pub type OMLCodeResult<T> = Result<T, OMLCodeError>;
59
60#[derive(Error, Debug, PartialEq)]
61pub enum DataErrKind {
62    #[error("format error : {0}\n{1:?} ")]
63    FormatError(String, Option<String>),
64    #[error("not complete")]
65    NotComplete,
66    #[error("no parse data: {0}")]
67    UnParse(String),
68
69    #[error("less data")]
70    LessData,
71    #[error("empty data")]
72    EmptyData,
73    #[error("struct less : {0}")]
74    LessStc(String),
75    #[error("define less : {0}")]
76    LessDef(String),
77}
78impl From<DataErrKind> for OMLCodeReason {
79    fn from(_: DataErrKind) -> Self {
80        OMLCodeReason::from_data()
81    }
82}
83pub type OmlCodeResult<T> = Result<T, OMLCodeError>;
84
85// ParseError<&str, ContextError<StrContext>>
86
87impl From<OMLCodeReason> for UvsReason {
88    fn from(_: OMLCodeReason) -> Self {
89        UvsReason::from_res()
90    }
91}