use crate::models::users::User; use crate::schema::characters; use serde_derive::Serialize; /// An entry that appears in a user's character list. Properties are /// in order of table columns. #[derive(Serialize, Debug, Queryable)] pub struct CharacterEntry { pub id: i32, pub user_id: i32, pub viewable: bool, pub name: String, //TODO don't need to carry around character data for this. pub data: Option>, } impl CharacterEntry { /// Transform to an Option that holds the character, if the /// character is viewable to a potentially existing user. A /// character is "visible" if the public viewable property is set /// to true, or the user is the owner of the character. Consumes /// self. pub fn as_visible_for(self, user: Option<&User>) -> Option { let character_is_visible = |c: CharacterEntry| { if c.viewable || user.map(|u| u.id) == Some(c.user_id) { Some(c) } else { None } }; Some(self).and_then(character_is_visible) } } /// Represents insert of a new character into the database. Property /// names correspond to columns. #[derive(Insertable)] #[table_name = "characters"] pub struct NewCharacter<'a> { pub user_id: i32, pub viewable: bool, pub character_name: &'a str, pub character_data: &'a [u8], }