oparl-types 0.1.0

Type definitions for the OParl protocol
Documentation
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

#[derive(
    Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum OrganizationType {
    #[serde(rename = "Gremium")]
    Council,

    #[serde(rename = "Partei")]
    Party,

    #[serde(rename = "Fraktion")]
    ParliamentaryGroup,

    #[serde(rename = "Verwaltungsbereich")]
    AdministrativeAmbit,

    #[serde(rename = "externes Gremium")]
    ExternalCouncil,

    #[serde(rename = "Institution")]
    Institution,

    #[serde(rename = "Sonstiges")]
    Other,
}

#[cfg(test)]
mod serde_tests {
    use super::OrganizationType;
    use pretty_assertions::assert_eq;
    use serde_json::json;

    #[test]
    fn serialize() {
        assert_eq!(json!(OrganizationType::Council), json!("Gremium"));
        assert_eq!(json!(OrganizationType::Party), json!("Partei"));
        assert_eq!(
            json!(OrganizationType::ParliamentaryGroup),
            json!("Fraktion")
        );
        assert_eq!(
            json!(OrganizationType::AdministrativeAmbit),
            json!("Verwaltungsbereich")
        );
        assert_eq!(
            json!(OrganizationType::ExternalCouncil),
            json!("externes Gremium")
        );
        assert_eq!(json!(OrganizationType::Institution), json!("Institution"));
        assert_eq!(json!(OrganizationType::Other), json!("Sonstiges"));
    }

    #[test]
    fn deserialize_good() {
        let deserialized: OrganizationType = serde_json::from_value(json!("Gremium"))
            .expect("value must be deserializable as organization type");
        assert_eq!(deserialized, OrganizationType::Council);

        let deserialized: OrganizationType = serde_json::from_value(json!("Partei"))
            .expect("value must be deserializable as organization type");
        assert_eq!(deserialized, OrganizationType::Party);

        let deserialized: OrganizationType = serde_json::from_value(json!("Fraktion"))
            .expect("value must be deserializable as organization type");
        assert_eq!(deserialized, OrganizationType::ParliamentaryGroup);

        let deserialized: OrganizationType = serde_json::from_value(json!("Institution"))
            .expect("value must be deserializable as organization type");
        assert_eq!(deserialized, OrganizationType::Institution);

        let deserialized: OrganizationType = serde_json::from_value(json!("externes Gremium"))
            .expect("value must be deserializable as organization type");
        assert_eq!(deserialized, OrganizationType::ExternalCouncil);

        let deserialized: OrganizationType = serde_json::from_value(json!("Sonstiges"))
            .expect("value must be deserializable as organization type");
        assert_eq!(deserialized, OrganizationType::Other);
    }

    #[test]
    fn deserialize_bad() {
        assert!(serde_json::from_value::<OrganizationType>(json!("gremium")).is_err());
        assert!(serde_json::from_value::<OrganizationType>(json!("GREMIUM")).is_err());
        assert!(serde_json::from_value::<OrganizationType>(json!({})).is_err());
        assert!(serde_json::from_value::<OrganizationType>(json!([])).is_err());
        assert!(serde_json::from_value::<OrganizationType>(json!(null)).is_err());
    }
}