Skip to content

Commit

Permalink
fix: handle name in tft augment container (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
kade-robertson authored Apr 5, 2024
1 parent f040b8d commit bf46732
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions src/models/tft/augments.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::{Deserialize, Serialize};
use serde::{de::Visitor, Deserialize, Deserializer, Serialize};
use std::collections::HashMap;

use crate::models::shared::{has_image, BasicDatum, Image};
Expand All @@ -11,10 +11,75 @@ pub struct Augments {
pub data: HashMap<String, BasicDatum>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[derive(Serialize, Debug, Clone, PartialEq, Eq)]
pub struct AugmentContainer {
pub name: String,
pub image: Image,
}

has_image!(AugmentContainer);

#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
enum AugmentContainerFields {
Name,
Image,
}

impl<'de> Deserialize<'de> for AugmentContainer {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct AugmentContainerVisitor;

impl<'de> Visitor<'de> for AugmentContainerVisitor {
type Value = AugmentContainer;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("struct AugmentContainer")
}

fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>
where
V: serde::de::SeqAccess<'de>,
{
let name = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
let image = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;

Ok(AugmentContainer { name, image })
}

fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
where
V: serde::de::MapAccess<'de>,
{
let mut name = None;
let mut image = None;

while let Some(key) = map.next_key()? {
match key {
AugmentContainerFields::Name => {
name = Some(map.next_value()?);
}
AugmentContainerFields::Image => {
image = Some(map.next_value()?);
}
}
}

let name = name.ok_or_else(|| serde::de::Error::missing_field("name"))?;
let image = image.ok_or_else(|| serde::de::Error::missing_field("image"))?;

Ok(AugmentContainer { name, image })
}
}

const FIELDS: &[&str] = &["name", "image"];
deserializer.deserialize_struct("AugmentContainer", FIELDS, AugmentContainerVisitor)
}
}

0 comments on commit bf46732

Please sign in to comment.