use std::fmt;
#[derive(Debug, Clone)]
pub enum CtpError {
FfiError(String),
EncodingError(String),
ConnectionError(String),
AuthenticationError(String),
BusinessError(i32, String),
InitializationError(String),
TimeoutError(String),
InvalidParameterError(String),
MemoryError(String),
Other(String),
}
impl fmt::Display for CtpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CtpError::FfiError(msg) => write!(f, "FFI错误: {}", msg),
CtpError::EncodingError(msg) => write!(f, "编码转换错误: {}", msg),
CtpError::ConnectionError(msg) => write!(f, "网络连接错误: {}", msg),
CtpError::AuthenticationError(msg) => write!(f, "登录认证错误: {}", msg),
CtpError::BusinessError(code, msg) => write!(f, "业务错误 [{}]: {}", code, msg),
CtpError::InitializationError(msg) => write!(f, "初始化错误: {}", msg),
CtpError::TimeoutError(msg) => write!(f, "超时错误: {}", msg),
CtpError::InvalidParameterError(msg) => write!(f, "无效参数错误: {}", msg),
CtpError::MemoryError(msg) => write!(f, "内存错误: {}", msg),
CtpError::Other(msg) => write!(f, "其他错误: {}", msg),
}
}
}
impl std::error::Error for CtpError {}
pub type CtpResult<T> = Result<T, CtpError>;
pub fn c_string_error(msg: &str) -> CtpError {
CtpError::FfiError(format!("C字符串处理错误: {}", msg))
}
pub fn encoding_error(msg: &str) -> CtpError {
CtpError::EncodingError(msg.to_string())
}
pub fn connection_error(msg: &str) -> CtpError {
CtpError::ConnectionError(msg.to_string())
}
pub fn business_error(error_id: i32, error_msg: &str) -> CtpError {
CtpError::BusinessError(error_id, error_msg.to_string())
}