wp_error/
parse_error.rs

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