Cleans up the code a bit

This commit is contained in:
Dennis Brentjes 2025-07-12 20:00:44 +02:00
parent df1e3ff905
commit ad0e81fdc3
8 changed files with 21 additions and 21 deletions

View File

@ -25,11 +25,8 @@ pub async fn post_leave_gamenight(pool: web::Data<DbPool>, user: AuthUser, gamen
gamenight_id: Uuid::parse_str(&gamenight_id.gamenight_id)?, gamenight_id: Uuid::parse_str(&gamenight_id.gamenight_id)?,
user_id: user.0.id user_id: user.0.id
}; };
println!("{:?}", participant);
let x = delete_gamenight_participant(&mut conn, participant)?; let x = delete_gamenight_participant(&mut conn, participant)?;
println!("Amount of deleted rows: {:?}", x);
Ok(x) Ok(x)
}).await??; }).await??;

View File

@ -65,7 +65,7 @@ impl Config {
let config_path = Self::config_path(); let config_path = Self::config_path();
if !fs::exists(&config_path)? { if !fs::exists(&config_path)? {
let config_error = ConfigError(format!("Cannot create parent directory for config file: {}", &config_path.display()).to_string()); let config_error = ConfigError(format!("Cannot create parent directory for config file: {}", &config_path.display()).to_string());
let _ = fs::create_dir_all(&config_path.parent().ok_or(config_error)?)?; fs::create_dir_all(config_path.parent().ok_or(config_error)?)?;
let config = Config::new(); let config = Config::new();
fs::write(&config_path, serde_json::to_string_pretty(&config)?.as_bytes())?; fs::write(&config_path, serde_json::to_string_pretty(&config)?.as_bytes())?;
@ -83,3 +83,9 @@ impl Config {
Ok(()) Ok(())
} }
} }
impl Default for Config {
fn default() -> Self {
Self::new()
}
}

View File

@ -6,7 +6,7 @@ pub struct Participants<'a>(pub &'a Vec<User>);
impl<'a> Display for Participants<'a> { impl<'a> Display for Participants<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let string_list: Vec<String> = self.0.iter().map(|x| {format!("{}", x)}).collect(); let string_list: Vec<String> = self.0.iter().map(|x| {format!("{x}")}).collect();
write!(f, "{}", string_list.join(", ")) write!(f, "{}", string_list.join(", "))
} }
} }

View File

@ -30,7 +30,7 @@ impl Connect {
let instance_name: String; let instance_name: String;
loop { loop {
if let Some(name) = Text::new("Name this instance:").prompt_skippable()? { if let Some(name) = Text::new("Name this instance:").prompt_skippable()? {
if config.instances.iter().find(|x| x.name == name).is_none() { if config.instances.iter().any(|x| x.name == name) {
instance_name = name; instance_name = name;
break; break;
} else { } else {
@ -58,7 +58,7 @@ impl Connect {
let instance = config.instances.iter_mut().find(|x| x.name == *instance_name).unwrap(); let instance = config.instances.iter_mut().find(|x| x.name == *instance_name).unwrap();
instance.token = token.jwt_token.clone(); instance.token = token.jwt_token.clone();
api_configuration.bearer_access_token = token.jwt_token.clone(); api_configuration.bearer_access_token = token.jwt_token.clone();
Config::save(&config)?; Config::save(config)?;
Ok(true) Ok(true)
} }
else { else {
@ -80,7 +80,7 @@ impl Connect {
} }
config.last_instance = Some(instance_name.clone()); config.last_instance = Some(instance_name.clone());
Config::save(&config)?; Config::save(config)?;
Ok(()) Ok(())
} }
} }
@ -90,13 +90,11 @@ impl<'a> Flow<'a> for Connect {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> { async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
let mut instance = if let Some(instance) = self.instance.clone() { let mut instance = if let Some(instance) = self.instance.clone() {
instance instance
} else { } else if let Some(instance) = self.prompt_new_instance(&state.gamenight_configuration)? {
if let Some(instance) = self.prompt_new_instance(&state.gamenight_configuration)? {
instance instance
} }
else { else {
return Ok((FlowOutcome::Cancelled, state)); return Ok((FlowOutcome::Cancelled, state));
}
}; };
let instance_name = instance.name.clone(); let instance_name = instance.name.clone();

View File

@ -25,7 +25,7 @@ impl RenameGame {
impl<'a> Flow<'a> for RenameGame { impl<'a> Flow<'a> for RenameGame {
async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> { async fn run(&self, state: &'a mut GamenightState) -> FlowResult<'a> {
if let Some(name) = Text::new(&format!("Rename {} to:", self.game.name)) if let Some(name) = Text::new(&format!("Rename {} to:", self.game.name))
.with_initial_value(&format!("{}", self.game.name)) .with_initial_value(&self.game.name)
.prompt_skippable()? .prompt_skippable()?
{ {
let req = RenameGameRequestBody { let req = RenameGameRequestBody {

View File

@ -28,12 +28,12 @@ impl<'a> Flow<'a> for ViewGame {
println!("{}", game); println!("{}", game);
let owned_games: Vec<Uuid> = owned_games_get(&state.api_configuration).await?.iter().map(|x| -> Result<Uuid, FlowError> { Ok(Uuid::parse_str(&x)?) }).collect::<Result<Vec<Uuid>, FlowError>>()?; let owned_games: Vec<Uuid> = owned_games_get(&state.api_configuration).await?.iter().map(|x| -> Result<Uuid, FlowError> { Ok(Uuid::parse_str(x)?) }).collect::<Result<Vec<Uuid>, FlowError>>()?;
println!("game_id {:?}, owned_games {:?}", game.id, owned_games); println!("game_id {:?}, owned_games {:?}", game.id, owned_games);
let own_or_disown: Box<dyn Flow<'a> + Send> = let own_or_disown: Box<dyn Flow<'a> + Send> =
if owned_games.into_iter().find(|x| *x == game.id) != None { if owned_games.into_iter().any(|x| x == game.id) {
Box::new(Disown::new(self.game.id)) Box::new(Disown::new(self.game.id))
} }
else { else {

View File

@ -58,12 +58,12 @@ impl<'a> Flow<'a> for ViewGamenight {
let mut validation = Validation::default(); let mut validation = Validation::default();
validation.insecure_disable_signature_validation(); validation.insecure_disable_signature_validation();
let claims = decode::<Claims>( let claims = decode::<Claims>(
&state.api_configuration.bearer_access_token.as_ref().unwrap(), state.api_configuration.bearer_access_token.as_ref().unwrap(),
&decoding_key, &decoding_key,
&validation)?.claims; &validation)?.claims;
let join_or_leave: Box<dyn Flow<'a> + Send> = let join_or_leave: Box<dyn Flow<'a> + Send> =
if users.iter().map(|x| {x.id}).find(|x| *x == claims.uid) != None { if users.iter().map(|x| {x.id}).any(|x| x == claims.uid) {
Box::new(Leave::new(self.gamenight.id)) Box::new(Leave::new(self.gamenight.id))
} }
else { else {

View File

@ -2,7 +2,6 @@ pub mod flows;
pub mod domain; pub mod domain;
use flows::{main::Main, Flow, GamenightState}; use flows::{main::Main, Flow, GamenightState};
use clear_screen;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {