diff --git a/Cargo.toml b/Cargo.toml index bbd9458..82ffb0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,5 +8,6 @@ edition = "2021" [dependencies] axum = "0.7.5" serde = "1.0.198" -tokio = { version = "1.2", features = ["full"] } +serde_json = "1.0.116" +tokio = { version = "1.2", features = ["full", "derive"] } warp = "0.3.7" diff --git a/src/question.rs b/src/question.rs index 17a5e9e..8ef47e8 100644 --- a/src/question.rs +++ b/src/question.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use serde::{Deserialize, Serialize}; struct Store { @@ -9,9 +10,14 @@ struct Store { impl Store { fn new() -> Self { Store { - questions: HashMap::new(), + questions: Self::init(), } } + fn init() -> HashMap { + let file = include_str!("./questions.json"); + serde_json::from_str(file).expect("can't read questions.json") + } + fn add(mut self, question: Question) -> Result { match self.questions.get(&question.id) { Some(_) => Err(format!("Question with id {} already exists", question.id)), @@ -46,7 +52,7 @@ impl Store { } } } -#[derive(Clone)] +#[derive(Deserialize, Serialize, Clone)] pub(crate) struct Question { id: u8, title: String, @@ -64,9 +70,3 @@ impl Question { } } } - -// impl IntoResponse for &Question { -// fn into_response(self) -> Response { -// (StatusCode::OK, Json(&self)).into_response() -// } -// } diff --git a/src/questions.json b/src/questions.json new file mode 100644 index 0000000..0345a5e --- /dev/null +++ b/src/questions.json @@ -0,0 +1,8 @@ +{ + "1" : { + "id": "1", + "title": "How?", + "content": "Please help!", + "tags": ["general"] + } +} \ No newline at end of file