From e59e9a5ebf3e805ac886faf54c43ab84975bd9ce Mon Sep 17 00:00:00 2001 From: jeff Date: Wed, 9 Dec 2020 08:58:49 +0000 Subject: [PATCH] Clearer function names in new character flow. --- src/models/characters.rs | 2 +- src/routes/characters.rs | 4 +-- src/routes/characters/new.rs | 63 ++++++++++++++++++------------------ 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/models/characters.rs b/src/models/characters.rs index 4cc72d6..716ed22 100644 --- a/src/models/characters.rs +++ b/src/models/characters.rs @@ -39,7 +39,7 @@ pub(crate) trait Visibility { } } -#[derive(DbEnum, Debug, Serialize, PartialEq)] +#[derive(DbEnum, Debug, Serialize, PartialEq, Clone, Copy)] pub enum CharacterDataType { ChroniclesOfDarknessV1, ChangelingV1, diff --git a/src/routes/characters.rs b/src/routes/characters.rs index 4956953..69e4610 100644 --- a/src/routes/characters.rs +++ b/src/routes/characters.rs @@ -10,8 +10,8 @@ mod new; pub(crate) fn routes() -> Vec { routes![ view_character, - new::new_character, - new::create_new_character, + new::new_character_page, + new::new_character_submit, new::new_character_not_logged_in, edit_character ] diff --git a/src/routes/characters/new.rs b/src/routes/characters/new.rs index d7908c9..7296621 100644 --- a/src/routes/characters/new.rs +++ b/src/routes/characters/new.rs @@ -37,36 +37,20 @@ impl<'v> FromFormValue<'v> for CharacterDataType { } } -#[get("/new")] -pub(super) fn new_character( - logged_in_user: &User, - conn: TenebrousDbConn, -) -> Result { - let mut context = HashMap::new(); - let form = NewCharacterForm { - name: "".to_string(), - system: CharacterDataType::ChroniclesOfDarknessV1, - }; - - context.insert("form", form); - Ok(Template::render("characters/new_character", context)) -} - -fn new_sheet(system: &CharacterDataType) -> Result { - let sheet = match system { - CharacterDataType::ChroniclesOfDarknessV1 => { - let mut new_character = CofdSheet::default(); - new_character.strength = 100; - let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&new_character)); - new_character.encode(&mut buf)?; +fn create_new_sheet(system: &CharacterDataType) -> Result { + use CharacterDataType::*; + let sheet: BytesMut = match system { + ChroniclesOfDarknessV1 => { + let sheet = CofdSheet::default(); + let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&sheet)); + sheet.encode(&mut buf)?; buf } - CharacterDataType::ChangelingV1 => { - let mut new_character = ChangelingSheet::default(); - new_character.base = Some(CofdSheet::default()); - new_character.base.as_mut().unwrap().strength = 100; - let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&new_character)); - new_character.encode(&mut buf)?; + ChangelingV1 => { + let mut sheet = ChangelingSheet::default(); + sheet.base = Some(CofdSheet::default()); + let mut buf = BytesMut::with_capacity(std::mem::size_of_val(&sheet)); + sheet.encode(&mut buf)?; buf } }; @@ -74,12 +58,12 @@ fn new_sheet(system: &CharacterDataType) -> Result { Ok(sheet) } -fn do_new_character( +fn create_new_character( form: Form, user_id: i32, conn: TenebrousDbConn, ) -> Result<(), Error> { - let sheet = new_sheet(&form.system)?; + let sheet = create_new_sheet(&form.system)?; let insert = NewCharacter { user_id: user_id, @@ -94,8 +78,23 @@ fn do_new_character( Ok(()) } +#[get("/new")] +pub(super) fn new_character_page( + logged_in_user: &User, + conn: TenebrousDbConn, +) -> Result { + let mut context = HashMap::new(); + let form = NewCharacterForm { + name: "".to_string(), + system: CharacterDataType::ChroniclesOfDarknessV1, + }; + + context.insert("form", form); + Ok(Template::render("characters/new_character", context)) +} + #[post("/new", data = "
")] -pub(super) fn create_new_character( +pub(super) fn new_character_submit( form: Result, FormError>, logged_in_user: &User, conn: TenebrousDbConn, @@ -113,7 +112,7 @@ pub(super) fn create_new_character( return Err(Template::render("characters/new_character", context)); } - match do_new_character(form.unwrap(), logged_in_user.id, conn) { + match create_new_character(form.unwrap(), logged_in_user.id, conn) { Ok(_) => Ok(crate::routes::common::redirect_to_index()), Err(e) => { let context = HashMap::::new();