#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![doc= include_str!("../README.md")]
mod generated;
pub mod json;
pub use generated::ElectionListItem;
pub use generated::{Answer, Category, Comment, ElectionOverview, Opinion, Party, Statement};
use json::*;
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Election {
pub metadata: ElectionOverview,
pub possible_answers: Vec<Answer>,
pub categories: Vec<Category>,
pub comments: Vec<Comment>,
pub opinions: Vec<Opinion>,
pub parties: Vec<Party>,
pub statements: Vec<Statement>,
}
impl Election {
pub fn read_from_path(path: impl AsRef<Path>) -> Result<Self, ReadJsonError> {
let path = path.as_ref();
Ok(Self {
metadata: read_json_file(path.join("overview.json"))?,
possible_answers: read_json_file(path.join("answer.json"))?,
categories: read_json_file(path.join("answer.json")).unwrap_or_default(),
comments: read_json_file(path.join("comment.json")).unwrap_or_default(),
opinions: read_json_file(path.join("opinion.json"))?,
parties: read_json_file(path.join("party.json"))?,
statements: read_json_file(path.join("statement.json"))?,
})
}
pub fn read_all_from_path(path: impl AsRef<Path>) -> Result<Vec<Self>, ReadJsonError> {
let path = path.as_ref();
let index: Vec<ElectionListItem> = read_json_file(path.join("election.json"))?;
index
.iter()
.map(|election| Election::read_from_path(path.join(&election.path)))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn read_all() {
let elections = Election::read_all_from_path("qual-o-mat-data").unwrap();
dbg!(elections);
}
}