Skip to main content

qmt_parser/
error.rs

1//! 通用错误类型。
2//!
3//! 这些错误主要用于 tick、分钟线和日线三个历史行情解析模块。
4
5use std::path::PathBuf;
6
7use crate::dividend::DividendError;
8use crate::finance::FinanceError;
9use thiserror::Error;
10
11#[cfg(feature = "polars")]
12use polars::error::PolarsError;
13
14/// Tick 解析错误。
15#[derive(Error, Debug)]
16pub enum TickParseError {
17    /// 输入路径为空。
18    #[error("文件路径不能为空")]
19    EmptyPath,
20    /// 输入文件扩展名不是 `.dat` 或 `.DAT`。
21    #[error("文件必须是.dat或.DAT格式: {0}")]
22    InvalidExtension(String),
23    /// 无法从文件路径中提取合法的日期元数据。
24    #[error("无法从文件名解析日期")]
25    InvalidFileName,
26    /// 底层文件读取或字节解析失败。
27    #[error("IO错误: {0}")]
28    Io(#[from] std::io::Error),
29    /// Polars 侧构建 `DataFrame` 失败。
30    #[cfg(feature = "polars")]
31    #[error("Polars错误: {0}")]
32    Polars(#[from] PolarsError),
33}
34
35/// xtquant 本地资料文件解析错误。
36#[derive(Debug, Error)]
37pub enum MetadataParseError {
38    /// I/O 失败。
39    #[error("failed to read metadata file: {0}")]
40    Io(#[from] std::io::Error),
41    /// 文件中没有可用记录。
42    #[error("no records parsed from {0}")]
43    NoRecords(&'static str),
44}
45
46/// 1 分钟线解析错误。
47#[derive(Error, Debug)]
48pub enum MinParseError {
49    /// 输入路径为空。
50    #[error("文件路径不能为空")]
51    EmptyPath,
52    /// 输入文件扩展名不是 `.dat` 或 `.DAT`。
53    #[error("文件必须是.dat或.DAT格式: {0}")]
54    InvalidExtension(String),
55    /// 底层文件读取或字节解析失败。
56    #[error("IO错误: {0}")]
57    Io(#[from] std::io::Error),
58    /// Polars 侧构建 `DataFrame` 失败。
59    #[cfg(feature = "polars")]
60    #[error("Polars错误: {0}")]
61    Polars(#[from] PolarsError),
62}
63
64/// 日线解析错误。
65#[derive(Error, Debug)]
66pub enum DailyParseError {
67    /// 输入路径为空。
68    #[error("文件路径不能为空")]
69    EmptyPath,
70    /// 输入文件扩展名不是 `.dat` 或 `.DAT`。
71    #[error("文件必须是.dat或.DAT格式: {0}")]
72    InvalidExtension(String),
73    /// 起始日期字符串无法按 `YYYYMMDD` 解析。
74    #[error("开始日期格式错误: {0}")]
75    InvalidStartDate(String),
76    /// 结束日期字符串无法按 `YYYYMMDD` 解析。
77    #[error("结束日期格式错误: {0}")]
78    InvalidEndDate(String),
79    /// 文件中的时间戳值非法或超出预期范围。
80    #[error("无效的时间戳")]
81    InvalidTimestamp,
82    /// 底层文件读取或字节解析失败。
83    #[error("IO错误: {0}")]
84    Io(#[from] std::io::Error),
85    /// Polars 侧构建 `DataFrame` 失败。
86    #[cfg(feature = "polars")]
87    #[error("Polars错误: {0}")]
88    Polars(#[from] PolarsError),
89}
90
91/// datadir 自动发现与解析错误。
92#[derive(Debug, Error)]
93pub enum DataDirError {
94    /// datadir 根目录不存在或不是目录。
95    #[error("invalid datadir root: {}", .0.display())]
96    InvalidRoot(PathBuf),
97    /// 输入键值不合法。
98    #[error("invalid datadir input: {0}")]
99    InvalidInput(String),
100    /// 在候选路径中未发现目标文件。
101    #[error("unable to locate {kind}; tried: {tried:?}")]
102    PathNotFound {
103        /// 目标类型名称。
104        kind: &'static str,
105        /// 已尝试的候选路径。
106        tried: Vec<PathBuf>,
107    },
108    /// Tick 解析错误。
109    #[error(transparent)]
110    Tick(#[from] TickParseError),
111    /// 1 分钟线解析错误。
112    #[error(transparent)]
113    Min(#[from] MinParseError),
114    /// 日线解析错误。
115    #[error(transparent)]
116    Daily(#[from] DailyParseError),
117    /// Metadata 解析错误。
118    #[error(transparent)]
119    Metadata(#[from] MetadataParseError),
120    /// 财务解析错误。
121    #[error(transparent)]
122    Finance(#[from] FinanceError),
123    /// 分红数据库错误。
124    #[error(transparent)]
125    Dividend(#[from] DividendError),
126}