earthmc 2.0.5

Async client for interacting with the EarthMC API
Documentation
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::model::{
    color::RgbaColor,
    location::PositionXYZ,
    named_uuid::{NamedUuid, NamedUuidOpt},
    timestamp::Timestamp,
};

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Quarter {
    pub name: String,
    pub uuid: Uuid,
    #[serde(rename = "type")]
    pub kind: QuarterKind,
    pub creator: Uuid,
    pub owner: NamedUuidOpt,
    pub town: NamedUuid,
    pub timestamps: QuarterTimestamps,
    pub status: QuarterStatus,
    pub stats: QuarterStats,
    pub colour: RgbaColor,
    pub trusted: Vec<NamedUuid>,
    pub cuboids: Vec<QuarterCuboid>,
}

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum QuarterKind {
    /// The default kind of Quarter.
    Apartment,
    /// Allows bed usage.
    Inn,
    /// Allows vehicle placing and usage.
    Station,
}

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QuarterTimestamps {
    #[cfg_attr(
        feature = "chrono",
        serde(with = "crate::model::timestamp::seconds")
    )]
    pub registered: Timestamp,
    #[cfg_attr(
        feature = "chrono",
        serde(with = "crate::model::timestamp::optional_seconds")
    )]
    pub claimed_at: Option<Timestamp>,
}

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QuarterStatus {
    pub is_embassy: bool,
    pub is_for_sale: bool,
}

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QuarterStats {
    pub price: Option<i32>,
    pub volume: i32,
    pub num_cuboids: u16,
    pub particle_size: Option<f32>,
}

#[derive(Clone, Deserialize, Serialize, PartialEq, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QuarterCuboid {
    pub corner_one: PositionXYZ,
    pub corner_two: PositionXYZ,
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::{
        endpoints::quarters::QuarterCuboid, model::location::PositionXYZ,
    };

    #[test]
    fn deserializes_cuboid_corners_from_coordinate_arrays() {
        let cuboid: QuarterCuboid = serde_json::from_value(json!({
            "cornerOne": [1, 2, 3],
            "cornerTwo": [4, 5, 6]
        }))
        .unwrap();

        assert_eq!(
            cuboid,
            QuarterCuboid {
                corner_one: PositionXYZ { x: 1, y: 2, z: 3 },
                corner_two: PositionXYZ { x: 4, y: 5, z: 6 },
            }
        );
    }

    #[test]
    fn serializes_cuboid_corners_as_coordinate_arrays() {
        let cuboid = QuarterCuboid {
            corner_one: PositionXYZ { x: 1, y: 2, z: 3 },
            corner_two: PositionXYZ { x: 4, y: 5, z: 6 },
        };

        assert_eq!(
            serde_json::to_value(cuboid).unwrap(),
            json!({
                "cornerOne": [1, 2, 3],
                "cornerTwo": [4, 5, 6]
            })
        );
    }
}