diff --git a/build.rs b/build.rs index 5834bb4..b0bf7ab 100644 --- a/build.rs +++ b/build.rs @@ -1,3 +1,8 @@ fn main() { - prost_build::compile_protos(&["proto/cofd.proto"], &["proto/"]).unwrap(); + let mut config = prost_build::Config::new(); + config.type_attribute(".", "#[derive(Serialize)]"); + config.type_attribute(".", "#[serde(rename_all = \"camelCase\")]"); + config + .compile_protos(&["proto/cofd.proto"], &["src/", "proto/"]) + .unwrap(); } diff --git a/src/errors.rs b/src/errors.rs index 527ca25..2d740f6 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -22,6 +22,9 @@ pub enum Error { #[error("serialization error: {0}")] SerializationError(#[from] prost::EncodeError), + + #[error("deserialization error: {0}")] + DeserializationError(#[from] prost::DecodeError), } #[derive(Error, Debug)] diff --git a/src/main.rs b/src/main.rs index 5d3ca8a..a9b5a03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,11 @@ extern crate rocket_contrib; #[macro_use] extern crate diesel; +// Seemingly necessary to get serde::Serialize into scope for Prost +// code generation. +#[macro_use] +extern crate serde_derive; + use rocket_contrib::templates::Template; pub mod catchers; diff --git a/src/routes/characters.rs b/src/routes/characters.rs index 944ea65..10aa2de 100644 --- a/src/routes/characters.rs +++ b/src/routes/characters.rs @@ -22,6 +22,13 @@ struct NewCharacterForm { name: String, //TODO add game system } +#[derive(Serialize)] +struct ViewCharacterTemlate<'a, T> { + pub name: &'a str, + pub username: &'a str, + pub sheet: T, +} + #[get("//")] fn view_character( character_id: i32, @@ -36,9 +43,17 @@ fn view_character( .and_then(|c| c.as_visible_for(logged_in_user)) .ok_or(Error::NotFound)?; - let mut context = HashMap::new(); - context.insert("name", character.character_name); - context.insert("username", user.username); + //TODO determine sheet type and deserialize based on that. + use crate::models::proto::cofd::CofdSheet; + use prost::Message; + let sheet = CofdSheet::decode(character.data.as_ref())?; + + let context = ViewCharacterTemlate { + name: &character.character_name, + username: &user.username, + sheet: sheet, + }; + Ok(Template::render("view_character", context)) } diff --git a/templates/view_character.html.tera b/templates/view_character.html.tera index 4bbaef3..8a4a5c8 100644 --- a/templates/view_character.html.tera +++ b/templates/view_character.html.tera @@ -4,5 +4,6 @@

Character {{name}}

User: {{username}}

+

Strength: {{sheet.strength}}

{% endblock content %}