Add serde serialization suppor to proto types.

This commit is contained in:
projectmoon 2020-12-08 08:47:34 +00:00
parent 8b3e8c70ae
commit 0c09a3fdef
5 changed files with 33 additions and 4 deletions

View File

@ -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();
}

View File

@ -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)]

View File

@ -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;

View File

@ -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("/<username>/<character_id>")]
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))
}

View File

@ -4,5 +4,6 @@
<div>
<h1>Character {{name}}</h1>
<h3>User: {{username}}</h3>
<p>Strength: {{sheet.strength}}</p>
</div>
{% endblock content %}