Skip to content

Commit

Permalink
#5
Browse files Browse the repository at this point in the history
  • Loading branch information
zjp-CN committed Oct 15, 2021
1 parent 1303fd7 commit 992b421
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 65 deletions.
30 changes: 4 additions & 26 deletions src/file/day/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ pub mod fq;
///
/// 注意:这个类型只对 `*.day` 文件进行了初步解析,
/// 所以日期 `date` 和股票代码 `code` 都是 `u32` 类型,
/// 如果的确需要这两个字段为字符串类型,考虑使用 [`serde_type::Day`] 类型。
///
/// [`serde_type::Day`]: crate::serde_type::Day
/// ## 注意
/// 开启 `serde` feature 时,此结构体的序列化 (serialize) 时:
/// 1. `date` 为 `年-月-日` 格式
/// 2. `code` 为 6 位字符串的股票代码
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Day {
Expand Down Expand Up @@ -76,30 +78,6 @@ impl Day {
Ok(std::fs::read(p)?.chunks_exact(32).map(|b| Self::from_bytes(code, b)).collect())
}

// #[cfg(feature="tokio")]
// pub async fn from_file_into_vec<P: AsRef<Path>>(code: u32, p: P) -> Result<Vec<Day>, Error>
// { Ok(tokio::fs::read(p).await.map_err(|_| Error::FileNotFound)?
// .chunks_exact(32)
// .map(|b| Self::from_bytes(code, b))
// .collect())
// }

// /// 转化成用于(反)序列化的数据类型:
// /// 6 位字符串的股票代码;%Y-%m-%d 字符串格式的日期;f64 类型的成交额;u64 类型的 vol 。
// #[cfg(feature = "serde")]
// pub fn into_serde_type(self) -> crate::serde_type::Day {
// crate::serde_type::Day { code: format!("{:06}", self.code),
// date: self.date_string(),
// open: self.open,
// high: self.high,
// low: self.low,
// close: self.close,
// // 单位:元
// amount: self.amount,
// // 转换成手:方便与其他数据源汇合
// vol: self.vol as f32 / 100., }
// }

/// `%Y-%m-%d` 格式的日期
pub fn date_string(&self) -> String { crate::bytes_helper::date_string(self.date) }

Expand Down
6 changes: 3 additions & 3 deletions src/file/gbbq/fq.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use super::Gbbq;
use crate::file::day::Day;

#[cfg(feature = "serde")]
#[derive(Debug, Clone, serde::Deserialize)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
#[derive(Debug, Clone)]
pub struct Factor {
pub date: String,
pub code: String,
#[serde(rename(deserialize = "close"))]
#[cfg_attr(feature = "serde", serde(rename(deserialize = "close")))]
pub preclose: f64,
pub factor: f64,
}
Expand Down
41 changes: 5 additions & 36 deletions src/file/gbbq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use crate::{bytes_helper::*, Result};
use std::collections::HashMap;
pub type StockGbbq<'a> = HashMap<u32, Vec<Gbbq<'a>>>;

/// 股本变迁 (gbbq) 文件。
///
/// ## 注意
/// 开启 `serde` feature 时,此结构体的序列化 (serialize) 时:
/// `date` 为 `年-月-日` 格式。
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Gbbq<'a> {
Expand Down Expand Up @@ -65,18 +70,6 @@ impl<'a> Gbbq<'a> {
pg_hzgb: f32_from_le_bytes(chunk, 25), }
}

// #[inline]
// pub fn from_chunk_mut(chunk: &'a mut [u8]) -> Gbbq {
// Self { sh: u8_from_le_bytes(chunk, 0),
// code: unsafe { std::str::from_utf8_unchecked(chunk.get_unchecked(1..7)) },
// date: u32_from_le_bytes(chunk, 8),
// category: u8_from_le_bytes(chunk, 12),
// fh_qltp: f32_from_le_bytes(chunk, 13),
// pgj_qzgb: f32_from_le_bytes(chunk, 17),
// sg_hltp: f32_from_le_bytes(chunk, 21),
// pg_hzgb: f32_from_le_bytes(chunk, 25), }
// }

// 未解密二进制数据转化成 [`Gbbq`]
pub fn iter(bytes: &mut [u8]) -> impl Iterator<Item = Gbbq> {
bytes.chunks_exact_mut(29).map(parse).map(Gbbq::from_chunk)
Expand All @@ -88,30 +81,6 @@ impl<'a> Gbbq<'a> {
bytes.chunks_exact(29).map(Self::from_chunk)
}

// /// 读取,解密并写入股本变迁的数据到 csv 。
// /// 如果写入成功,则返回解密后二进制数据(包括表示长度的前 4 字节)
// #[cfg(feature = "csv")]
// #[cfg(feature = "tokio")]
// pub async fn from_file_into_csv(src: impl AsRef<Path>, dst: impl AsRef<Path>)
// -> io::Result<Vec<u8>, Box<dyn std::error::Error>> {
// let mut bytes = std::fs::read(src)?;
// let mut wtr = csv::Writer::from_path(dst)?;
// Self::iter(&mut bytes[4..]).try_for_each(|gbbq| wtr.serialize(gbbq.into_serde_type()))?;
// Ok(bytes)
// }

// #[cfg(feature = "serde")]
// pub fn into_serde_type(self) -> crate::serde_type::Gbbq {
// crate::serde_type::Gbbq { sh: self.market,
// code: self.code.into(),
// date: date_string(self.date),
// category: self.category,
// fh_qltp: self.fh_qltp,
// pgj_qzgb: self.pgj_qzgb,
// sg_hltp: self.sg_hltp,
// pg_hzgb: self.pg_hzgb, }
// }

#[inline]
pub fn compute_pre_pct(&self, close: f32, mut preclose: f64, flag: bool) -> [f64; 3] {
if flag {
Expand Down

0 comments on commit 992b421

Please sign in to comment.