NAC_Blockchain/nac-wallet-core/src/gnacs_parser.rs

90 lines
2.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! GNACS解析器模块
/// 资产类型
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AssetType {
/// 不动产收益权
RealEstateRevenue,
/// 股权
Equity,
/// 债权
Debt,
/// 商品
Commodity,
/// 其他
Other(String),
}
/// 风险等级
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(u8)]
pub enum RiskLevel {
/// 低风险
Low = 1,
/// 中风险
Medium = 2,
/// 高风险
High = 3,
}
/// GNACS解析结果
#[derive(Debug, Clone)]
pub struct GNACSInfo {
/// 资产类型
pub asset_type: AssetType,
/// 风险权重巴塞尔III
pub risk_weight: u8,
/// 合规等级
pub compliance_level: u8,
/// 实时状态
pub status: u8,
}
/// GNACS解析器
pub struct GNACSParser;
impl GNACSParser {
/// 解析GNACS编码
pub fn parse(gnacs: &[u8; 6]) -> GNACSInfo {
let asset_type_code = gnacs[0];
let risk_weight = gnacs[1];
let compliance_level = gnacs[2];
let status = gnacs[5];
let asset_type = match asset_type_code {
0x01 => AssetType::RealEstateRevenue,
0x02 => AssetType::Equity,
0x03 => AssetType::Debt,
0x04 => AssetType::Commodity,
_ => AssetType::Other(format!("Unknown({})", asset_type_code)),
};
GNACSInfo {
asset_type,
risk_weight,
compliance_level,
status,
}
}
/// 获取资产类型描述
pub fn asset_type_description(asset_type: &AssetType) -> String {
match asset_type {
AssetType::RealEstateRevenue => "不动产收益权".to_string(),
AssetType::Equity => "股权".to_string(),
AssetType::Debt => "债权".to_string(),
AssetType::Commodity => "商品".to_string(),
AssetType::Other(s) => s.clone(),
}
}
/// 获取风险等级
pub fn risk_level(risk_weight: u8) -> RiskLevel {
match risk_weight {
0..=50 => RiskLevel::Low,
51..=100 => RiskLevel::Medium,
_ => RiskLevel::High,
}
}
}