use serde::ser::{Serialize, SerializeSeq, Serializer};
use std::collections::HashMap;
pub type CropWeatherType = HashMap<String, HashMap<String, i8>>;
pub struct CropTypeState {
pub name: &'static str,
pub max_harvest: u8,
pub min_harvest: u8,
pub states: u8,
}
pub struct CropTypeStateBuilder {
pub max_harvest: u8,
pub min_harvest: u8,
pub name: String,
pub states: u8,
}
pub struct CropSeason {
pub name: &'static str,
pub min: i8,
pub max: i8,
}
#[derive(Clone)]
pub struct Crop {
pub name: &'static str,
pub growth_time: u8,
pub harvest_periods: [bool; 12],
pub plant_periods: [bool; 12],
}
#[derive(serde::Serialize, Debug, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct CropOutput {
pub growth_time: u8,
pub harvest_periods: Vec<u8>,
pub plant_periods: Vec<u8>,
}
impl CropOutput {
#[must_use]
pub fn new(growth_time: u8) -> Self {
CropOutput {
growth_time,
harvest_periods: vec![],
plant_periods: vec![],
}
}
}
#[derive(serde::Serialize)]
struct CropSerializerOutput {
pub name: String,
pub growth_time: u8,
pub harvest_periods: Vec<u8>,
pub plant_periods: Vec<u8>,
}
pub struct CropList {
list: HashMap<String, CropOutput>,
order: Vec<String>,
}
impl CropList {
#[must_use]
pub fn len(&self) -> usize {
self.list.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.list.is_empty()
}
pub fn insert(&mut self, key: String, value: CropOutput) {
self.list.insert(key.clone(), value);
self.order.push(key);
}
pub fn get(&mut self, key: &str) -> Option<&CropOutput> {
self.list.get(key)
}
#[must_use]
pub fn new() -> Self {
CropList {
list: HashMap::new(),
order: vec![],
}
}
}
impl Default for CropList {
fn default() -> Self {
Self::new()
}
}
impl Serialize for CropList {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.list.is_empty() {
return serializer.serialize_none();
}
let mut seq = serializer.serialize_seq(Some(self.list.len()))?;
for key in &self.order {
let item = &self.list[key];
let item_struct = CropSerializerOutput {
name: key.to_string().to_lowercase(),
growth_time: item.growth_time,
harvest_periods: item.harvest_periods.clone(),
plant_periods: item.plant_periods.clone(),
};
seq.serialize_element(&item_struct)?;
}
seq.end()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn empty_crop_list() {
let mine = CropList::default();
assert_eq!(String::from("null"), serde_json::to_string(&mine).unwrap())
}
}