Skip to main content

democracy_index/
lib.rs

1//! The Democracy Index published by the Economist Group is an index measuring the quality of democracy across the world.
2
3#![no_std]
4
5use core::fmt;
6
7/// The year the data is based on
8pub const REPORT_YEAR: u16 = 2024;
9
10/// Category based on score
11#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
12pub enum RegimeType {
13    /// Political pluralism is nonexistent or severely limited
14    Authoritarian,
15    /// Regular electoral fraud, preventing them from being fair and free democracies
16    HybridRegime,
17    /// Elections are fair and free and basic civil liberties are honoured, but may have issues
18    FlawedDemocracy,
19    /// Civil liberties and fundamental political freedoms are not only respected but also reinforced by a political culture conducive to the thriving of democratic principles
20    FullDemocracy,
21}
22
23impl RegimeType {
24    pub fn as_str(&self) -> &str {
25        match self {
26            Self::Authoritarian => "Authoritarian regime",
27            Self::HybridRegime => "Hybrid regime",
28            Self::FlawedDemocracy => "Flawed democracy",
29            Self::FullDemocracy => "Full democracy",
30        }
31    }
32}
33
34impl fmt::Display for RegimeType {
35    fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
36        write!(w, "{}", self.as_str())
37    }
38}
39
40/// The various scores for a country
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct DemocracyQualities {
43    pub overall_score: u16,
44    pub regime_type: RegimeType,
45    pub electoral_process_and_pluralism: u16,
46    pub functioning_of_government: u16,
47    pub political_participation: u16,
48    pub political_culture: u16,
49    pub civil_liberties: u16,
50}
51
52include!(concat!(env!("OUT_DIR"), "/gen.rs"));
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_number_of_entries() {
60        assert_eq!(RANKING.len(), 167);
61    }
62
63    #[test]
64    fn test_sort_regime_types() {
65        let list = &mut [
66            RegimeType::HybridRegime,
67            RegimeType::FlawedDemocracy,
68            RegimeType::Authoritarian,
69            RegimeType::FullDemocracy,
70        ];
71        list.sort();
72        assert_eq!(
73            list,
74            &[
75                RegimeType::Authoritarian,
76                RegimeType::HybridRegime,
77                RegimeType::FlawedDemocracy,
78                RegimeType::FullDemocracy,
79            ]
80        );
81        assert!(RegimeType::FullDemocracy > RegimeType::Authoritarian);
82    }
83
84    #[test]
85    fn test_sweden() {
86        assert_eq!(
87            countries::SWE,
88            DemocracyQualities {
89                overall_score: 939,
90                regime_type: RegimeType::FullDemocracy,
91                electoral_process_and_pluralism: 958,
92                functioning_of_government: 964,
93                political_participation: 833,
94                political_culture: 1000,
95                civil_liberties: 941,
96            }
97        );
98        assert_eq!(get("SWE"), Some(&countries::SWE));
99    }
100
101    #[test]
102    fn test_nepal() {
103        assert_eq!(
104            countries::NPL,
105            DemocracyQualities {
106                overall_score: 460,
107                regime_type: RegimeType::HybridRegime,
108                electoral_process_and_pluralism: 483,
109                functioning_of_government: 536,
110                political_participation: 500,
111                political_culture: 250,
112                civil_liberties: 529,
113            }
114        );
115        assert_eq!(get("NPL"), Some(&countries::NPL));
116    }
117
118    #[test]
119    fn test_cuba() {
120        assert_eq!(
121            countries::CUB,
122            DemocracyQualities {
123                overall_score: 258,
124                regime_type: RegimeType::Authoritarian,
125                electoral_process_and_pluralism: 0,
126                functioning_of_government: 286,
127                political_participation: 333,
128                political_culture: 375,
129                civil_liberties: 294,
130            }
131        );
132        assert_eq!(get("CUB"), Some(&countries::CUB));
133    }
134
135    #[test]
136    fn test_north_korea() {
137        assert_eq!(
138            countries::PRK,
139            DemocracyQualities {
140                overall_score: 108,
141                regime_type: RegimeType::Authoritarian,
142                electoral_process_and_pluralism: 0,
143                functioning_of_government: 250,
144                political_participation: 167,
145                political_culture: 125,
146                civil_liberties: 0,
147            }
148        );
149        assert_eq!(get("PRK"), Some(&countries::PRK));
150    }
151
152    #[test]
153    fn test_get_invalid() {
154        assert_eq!(get("X"), None);
155        assert_eq!(get(""), None);
156    }
157}