i18n+filtering fork - fluent-templates v2
at main 86 lines 2.2 kB view raw
1use serde::{Deserialize, Serialize}; 2use thiserror::Error; 3 4use crate::{ 5 errors::expand_error, 6 i18n::Locales, 7 storage::{event::event_get_cid, StoragePool}, 8}; 9 10#[derive(Debug, Error)] 11pub enum BuildRSVPError { 12 #[error("error-rsvp-builder-1 Invalid Subject")] 13 InvalidSubject, 14 15 #[error("error-rsvp-builder-2 Invalid Status")] 16 InvalidStatus, 17} 18 19#[derive(Serialize, Deserialize, Debug, Default, PartialEq, Clone)] 20pub enum BuildRsvpContentState { 21 #[default] 22 Reset, 23 Selecting, 24 Selected, 25 Review, 26} 27 28#[derive(Serialize, Deserialize, Debug, Clone)] 29pub struct BuildRSVPForm { 30 pub build_state: Option<BuildRsvpContentState>, 31 32 pub subject_aturi: Option<String>, 33 pub subject_aturi_error: Option<String>, 34 35 pub subject_cid: Option<String>, 36 pub subject_cid_error: Option<String>, 37 38 pub status: Option<String>, 39 pub status_error: Option<String>, 40} 41 42impl BuildRSVPForm { 43 pub async fn hydrate( 44 &mut self, 45 database_pool: &StoragePool, 46 locales: &Locales, 47 language: &unic_langid::LanguageIdentifier, 48 ) { 49 // If we don't have an AT-URI, we can't do anything. 50 let subject_aturi = match self.subject_aturi.as_ref() { 51 Some(uri) => uri, 52 None => return, 53 }; 54 55 // If we already have a CID, we don't need to hydrate it. 56 if self.subject_cid.is_some() { 57 return; 58 } 59 60 let cid_result = event_get_cid(database_pool, subject_aturi).await; 61 match cid_result { 62 Ok(cid_option) => { 63 self.subject_cid = cid_option; 64 } 65 Err(err) => { 66 let (err_bare, err_partial) = expand_error(err); 67 let error_message = locales.format_error(language, &err_bare, &err_partial); 68 self.subject_cid_error = Some(error_message); 69 } 70 } 71 } 72 73 pub fn validate( 74 &mut self, 75 _locales: &Locales, 76 _language: &unic_langid::LanguageIdentifier, 77 ) -> bool { 78 // TODO: Ensure subject_aturi is set. 79 80 // TODO: Ensure subject_cid is set. 81 82 // TODO: Ensure status is a valid value. 83 84 false 85 } 86}