use crate::TushareError;
use crate::api::{Api, serialize_api_name};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::Infallible;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TushareRequest {
#[serde(serialize_with = "serialize_api_name")]
pub api_name: Api,
pub params: HashMap<String, String>,
pub fields: Vec<String>,
}
impl TryFrom<&TushareRequest> for TushareRequest {
type Error = Infallible;
fn try_from(value: &TushareRequest) -> Result<Self, Self::Error> {
Ok(value.clone())
}
}
impl TryFrom<&&str> for TushareRequest {
type Error = TushareError;
fn try_from(value: &&str) -> Result<Self, Self::Error> {
Self::try_from(*value)
}
}
impl TryFrom<&String> for TushareRequest {
type Error = TushareError;
fn try_from(value: &String) -> Result<Self, Self::Error> {
Self::try_from(value.as_str())
}
}
impl TushareRequest {
pub fn new<K, V, F, P, Fs>(api_name: Api, params: P, fields: Fs) -> Self
where
K: Into<String>,
V: Into<String>,
F: Into<String>,
P: IntoIterator<Item = (K, V)>,
Fs: IntoIterator<Item = F>,
{
let params = params
.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect();
let fields = fields.into_iter().map(|f| f.into()).collect();
Self {
api_name,
params,
fields,
}
}
pub fn with_str_params<const N: usize>(
api_name: Api,
params: [(&str, &str); N],
fields: &[&str],
) -> Self {
let params = params
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
let fields = fields.iter().map(|f| f.to_string()).collect();
Self {
api_name,
params,
fields,
}
}
pub fn add_param<K: Into<String>, V: Into<String>>(mut self, key: K, value: V) -> Self {
self.params.insert(key.into(), value.into());
self
}
pub fn add_field<F: Into<String>>(mut self, field: F) -> Self {
self.fields.push(field.into());
self
}
}
pub type TushareRequestString = TushareRequest;
#[macro_export]
macro_rules! params {
($($key:expr => $value:expr),* $(,)?) => {
{
let mut map = std::collections::HashMap::new();
$(
map.insert($key.to_string(), $value.to_string());
)*
map
}
};
}
#[macro_export]
macro_rules! fields {
($($field:expr),* $(,)?) => {
vec![$($field.to_string()),*]
};
}
#[macro_export]
macro_rules! request {
($api:expr, { $($key:expr => $value:expr),* $(,)? }, [ $($field:expr),* $(,)? ]) => {
TushareRequest {
api_name: $api,
params: params!($($key => $value),*),
fields: fields![$($field),*],
}
};
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TushareResponse {
pub request_id: String,
pub code: i32,
pub msg: Option<String>,
pub data: Option<TushareData>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TushareData {
pub fields: Vec<String>,
pub items: Vec<Vec<serde_json::Value>>,
pub has_more: bool,
pub count: i64,
}
#[derive(Debug, Clone)]
pub struct TushareEntityList<T> {
pub items: Vec<T>,
pub has_more: bool,
pub count: i64,
}
impl<T> TushareEntityList<T> {
pub fn new(items: Vec<T>, has_more: bool, count: i64) -> Self {
Self {
items,
has_more,
count,
}
}
pub fn len(&self) -> usize {
self.items.len()
}
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
pub fn items(&self) -> &[T] {
&self.items
}
pub fn items_mut(&mut self) -> &mut [T] {
&mut self.items
}
pub fn has_more(&self) -> bool {
self.has_more
}
pub fn count(&self) -> i64 {
self.count
}
pub fn iter(&self) -> std::slice::Iter<T> {
self.items.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<T> {
self.items.iter_mut()
}
pub fn into_items(self) -> Vec<T> {
self.items
}
}
impl<T> std::ops::Deref for TushareEntityList<T> {
type Target = Vec<T>;
fn deref(&self) -> &Self::Target {
&self.items
}
}
impl<T> std::ops::DerefMut for TushareEntityList<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.items
}
}
impl<T> IntoIterator for TushareEntityList<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<'a, T> IntoIterator for &'a TushareEntityList<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.items.iter()
}
}
impl<'a, T> IntoIterator for &'a mut TushareEntityList<T> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.items.iter_mut()
}
}
impl<T> From<Vec<T>> for TushareEntityList<T> {
fn from(items: Vec<T>) -> Self {
Self {
items,
has_more: false,
count: 0,
}
}
}
impl TryFrom<String> for TushareRequest {
type Error = TushareError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::try_from(value.as_str())
}
}
impl TryFrom<&str> for TushareRequest {
type Error = TushareError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
serde_json::from_str(value).map_err(TushareError::SerializationError)
}
}