Add serde serialization suppor to proto types.
This commit is contained in:
parent
d6a80a7996
commit
93ed679946
7
build.rs
7
build.rs
|
@ -1,3 +1,8 @@
|
||||||
fn main() {
|
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();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,9 @@ pub enum Error {
|
||||||
|
|
||||||
#[error("serialization error: {0}")]
|
#[error("serialization error: {0}")]
|
||||||
SerializationError(#[from] prost::EncodeError),
|
SerializationError(#[from] prost::EncodeError),
|
||||||
|
|
||||||
|
#[error("deserialization error: {0}")]
|
||||||
|
DeserializationError(#[from] prost::DecodeError),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
|
|
|
@ -9,6 +9,11 @@ extern crate rocket_contrib;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate diesel;
|
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;
|
use rocket_contrib::templates::Template;
|
||||||
|
|
||||||
pub mod catchers;
|
pub mod catchers;
|
||||||
|
|
|
@ -22,6 +22,13 @@ struct NewCharacterForm {
|
||||||
name: String, //TODO add game system
|
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>")]
|
#[get("/<username>/<character_id>")]
|
||||||
fn view_character(
|
fn view_character(
|
||||||
character_id: i32,
|
character_id: i32,
|
||||||
|
@ -36,9 +43,17 @@ fn view_character(
|
||||||
.and_then(|c| c.as_visible_for(logged_in_user))
|
.and_then(|c| c.as_visible_for(logged_in_user))
|
||||||
.ok_or(Error::NotFound)?;
|
.ok_or(Error::NotFound)?;
|
||||||
|
|
||||||
let mut context = HashMap::new();
|
//TODO determine sheet type and deserialize based on that.
|
||||||
context.insert("name", character.character_name);
|
use crate::models::proto::cofd::CofdSheet;
|
||||||
context.insert("username", user.username);
|
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))
|
Ok(Template::render("view_character", context))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,5 +4,6 @@
|
||||||
<div>
|
<div>
|
||||||
<h1>Character {{name}}</h1>
|
<h1>Character {{name}}</h1>
|
||||||
<h3>User: {{username}}</h3>
|
<h3>User: {{username}}</h3>
|
||||||
|
<p>Strength: {{sheet.strength}}</p>
|
||||||
</div>
|
</div>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
Loading…
Reference in New Issue