1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! Module for sector
//!
//! This API returns the realtime and historical sector performances calculated
//! from S&P500 incumbents.
//!
//! You can read about [Sector][sector] API and what it returns
//! on alphavantage documentation
//!
//! [sector]: https://www.alphavantage.co/documentation/#sector

use crate::user::APIKey;
use serde_derive::Deserialize;
use std::collections::HashMap;

/// Stores Metadata
#[derive(Deserialize, Clone)]
struct MetaData {
    #[serde(rename = "Information")]
    information: String,
    #[serde(rename = "Last Refreshed")]
    last_refreshed: String,
}

/// Store Sector data
#[derive(Default, Clone)]
pub struct Data {
    rank: String,
    utilites: String,
    health_care: String,
    information_technology: String,
    industrials: String,
    real_estate: String,
    consumer_staples: String,
    consumer_discretionary: String,
    financials: String,
    communication_services: String,
    materials: String,
    energy: String,
}

impl Data {
    /// Return rank
    pub fn rank(&self) -> String {
        self.rank.to_string()
    }

    /// Return utilites score
    pub fn utilites(&self) -> String {
        self.utilites.to_string()
    }

    /// Return health care score
    pub fn health_care(&self) -> String {
        self.health_care.to_string()
    }

    /// Return out information technology
    pub fn information_technology(&self) -> String {
        self.information_technology.to_string()
    }

    /// Return industrials scores
    pub fn industrials(&self) -> String {
        self.industrials.to_string()
    }

    /// Return out real estate value
    pub fn real_estate(&self) -> String {
        self.real_estate.to_string()
    }

    /// Return consumer staples value
    pub fn consumer_staples(&self) -> String {
        self.consumer_staples.to_string()
    }

    /// Return out value for consumer discretionary
    pub fn consumer_discretionary(&self) -> String {
        self.consumer_discretionary.to_string()
    }

    /// Return out for financials
    pub fn financials(&self) -> String {
        self.financials.to_string()
    }

    /// Gives value of communication services
    pub fn communication_services(&self) -> String {
        self.communication_services.to_string()
    }

    /// Gives materials value
    pub fn materials(&self) -> String {
        self.materials.to_string()
    }

    /// Gives out energy data
    pub fn energy(&self) -> String {
        self.energy.to_string()
    }
}

/// Stores sector data
#[derive(Default)]
pub struct Sector {
    error_message: Option<String>,
    information: Option<String>,
    meta_data: Option<MetaData>,
    data: Option<Vec<Data>>,
}

impl Sector {
    /// Return sector information
    ///
    /// ```
    /// let api = alpha_vantage::set_api("demo");
    /// let sector = api.sector();
    /// let information = sector.information();
    /// assert_eq!(
    ///     information.unwrap(),
    ///     "US Sector Performance (realtime & historical)"
    /// );
    /// ```
    pub fn information(&self) -> Result<String, String> {
        self.check_meta_data("information")
    }

    /// Return last refreshed time
    pub fn last_refreshed(&self) -> Result<String, String> {
        self.check_meta_data("last refreshed")
    }

    /// Return vector of data in Result
    pub fn data(&self) -> Result<Vec<Data>, String> {
        if let Some(data) = &self.data {
            Ok(data.to_vec())
        } else if let Some(error) = &self.error_message {
            Err(format!("Error Message : {}", error))
        } else {
            Err(format!(
                "Information : {}",
                self.information.clone().unwrap()
            ))
        }
    }

    /// Check a meta data is present or not
    fn check_meta_data(&self, name: &str) -> Result<String, String> {
        if let Some(meta_data) = &self.meta_data {
            let value = match name {
                "information" => &meta_data.information,
                "last refreshed" => &meta_data.last_refreshed,
                _ => "",
            };
            Ok(value.to_string())
        } else if let Some(error) = &self.error_message {
            Err(format!("Error Message : {}", error))
        } else {
            Err(format!(
                "Information : {}",
                self.information.clone().unwrap()
            ))
        }
    }
}

/// struct for helping out sector
#[derive(Deserialize)]
pub(crate) struct SectorHelper {
    #[serde(rename = "Information")]
    information: Option<String>,
    #[serde(rename = "Error Message")]
    error_message: Option<String>,
    #[serde(rename = "Meta Data")]
    meta_data: Option<MetaData>,
    #[serde(flatten)]
    data: Option<HashMap<String, HashMap<String, String>>>,
}

impl SectorHelper {
    /// Convert out sectorhelper to sector
    pub(crate) fn convert(self) -> Sector {
        let mut sector = Sector::default();
        sector.information = self.information;
        sector.error_message = self.error_message;
        sector.meta_data = self.meta_data;
        if let Some(temp_data) = self.data {
            let mut final_data = Vec::new();
            for (key, val) in &temp_data {
                let mut data = Data::default();
                match key.as_str() {
                    "Rank A: Real-Time Performance" => data.rank = "real-time".to_string(),
                    "Rank B: 1 Day Performance" => data.rank = "1-day".to_string(),
                    "Rank C: 5 Day Performance" => data.rank = "5-day".to_string(),
                    "Rank D: 1 Month Performance" => data.rank = "1-month".to_string(),
                    "Rank E: 3 Month Performance" => data.rank = "3-month".to_string(),
                    "Rank F: Year-to-Date (YTD) Performance" => {
                        data.rank = "year-to-date".to_string()
                    }
                    "Rank G: 1 Year Performance" => data.rank = "1-year".to_string(),
                    "Rank H: 3 Year Performance" => data.rank = "3-year".to_string(),
                    "Rank I: 5 Year Performance" => data.rank = "5-year".to_string(),
                    "Rank J: 10 Year Performance" => data.rank = "10-year".to_string(),
                    _ => data.rank = "".to_string(),
                }
                for (key, val) in val.iter() {
                    match key.as_str() {
                        "Utilities" => data.utilites = val.to_string(),
                        "Health Care" => data.health_care = val.to_string(),
                        "Information Technology" => data.information_technology = val.to_string(),
                        "Industrials" => data.industrials = val.to_string(),
                        "Real Estate" => data.real_estate = val.to_string(),
                        "Consumer Staples" => data.consumer_staples = val.to_string(),
                        "Consumer Discretionary" => data.consumer_discretionary = val.to_string(),
                        "Financials" => data.financials = val.to_string(),
                        "Communication Services" => data.communication_services = val.to_string(),
                        "Materials" => data.materials = val.to_string(),
                        "Energy" => data.energy = val.to_string(),
                        _ => {}
                    }
                }
                final_data.push(data);
            }
            sector.data = Some(final_data);
        }
        sector
    }
}

/// Function used to create a [Sector][Sector] struct.
///
/// Instead of using this function directly calling through [APIKey][APIKey]
/// method is recommended
pub fn sector(api_data: (&str, Option<u64>)) -> Sector {
    let api;
    if let Some(timeout) = api_data.1 {
        api = APIKey::set_with_timeout(api_data.0, timeout);
    } else {
        api = APIKey::set_api(api_data.0);
    }
    api.sector()
}