From f2eaa815a6d19e66cca93c942447b1a0e3f31c60 Mon Sep 17 00:00:00 2001 From: Peter Holloway Date: Wed, 12 Feb 2025 11:19:54 +0000 Subject: [PATCH] Extract creation of CurrentConfiguration to shared method --- src/graphql.rs | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/graphql.rs b/src/graphql.rs index 826c6f4a..a6ab5358 100644 --- a/src/graphql.rs +++ b/src/graphql.rs @@ -295,6 +295,22 @@ impl CurrentConfiguration { } } +impl CurrentConfiguration { + async fn for_config( + db_config: BeamlineConfiguration, + nt: &NumTracker, + ) -> async_graphql::Result { + let dir = nt + .for_beamline(db_config.name(), db_config.tracker_file_extension()) + .await?; + let high_file = dir.prev().await?; + Ok(CurrentConfiguration { + db_config, + high_file, + }) + } +} + impl FieldSource for ScanPaths { fn resolve(&self, field: &ScanField) -> Cow<'_, str> { match field { @@ -346,14 +362,7 @@ impl Query { let nt = ctx.data::()?; trace!("Getting config for {beamline:?}"); let conf = db.current_configuration(&beamline).await?; - let dir = nt - .for_beamline(&beamline, conf.tracker_file_extension()) - .await?; - let high_file = dir.prev().await?; - Ok(CurrentConfiguration { - db_config: conf, - high_file, - }) + CurrentConfiguration::for_config(conf, nt).await } /// Get the configurations for all available beamlines @@ -372,16 +381,11 @@ impl Query { None => db.all_configurations().await?, }; - futures::future::join_all(configurations.into_iter().map(|cnf| async { - let dir = nt - .for_beamline(cnf.name(), cnf.tracker_file_extension()) - .await?; - let high_file = dir.prev().await?; - Ok(CurrentConfiguration { - db_config: cnf, - high_file, - }) - })) + futures::future::join_all( + configurations + .into_iter() + .map(|cnf| CurrentConfiguration::for_config(cnf, nt)), + ) .await .into_iter() .collect() @@ -448,14 +452,7 @@ impl Mutation { Some(bc) => bc, None => upd.insert_new(db).await?, }; - let dir = nt - .for_beamline(&beamline, db_config.tracker_file_extension()) - .await?; - let high_file = dir.prev().await?; - Ok(CurrentConfiguration { - db_config, - high_file, - }) + CurrentConfiguration::for_config(db_config, nt).await } }