|
14 | 14 |
|
15 | 15 | use std::any; |
16 | 16 | use std::borrow::Cow; |
| 17 | +use std::collections::HashMap; |
17 | 18 | use std::error::Error; |
18 | 19 | use std::fmt::Display; |
19 | 20 | use std::future::Future; |
@@ -146,6 +147,7 @@ struct VisitPath { |
146 | 147 | /// GraphQL type to provide path data for the next scan for a given visit |
147 | 148 | struct ScanPaths { |
148 | 149 | visit: VisitPath, |
| 150 | + extra_templates: HashMap<String, PathTemplate<ScanField>>, |
149 | 151 | subdirectory: Subdirectory, |
150 | 152 | } |
151 | 153 |
|
@@ -233,6 +235,12 @@ impl ScanPaths { |
233 | 235 | self.visit.info.scan_number() |
234 | 236 | } |
235 | 237 |
|
| 238 | + async fn template(&self, name: String) -> async_graphql::Result<String> { |
| 239 | + Ok(path_to_string( |
| 240 | + self.extra_templates.get(&name).unwrap().render(self), |
| 241 | + )?) |
| 242 | + } |
| 243 | + |
236 | 244 | /// The paths where the given detectors should write their files. |
237 | 245 | /// |
238 | 246 | /// Detector names are normalised before being used in file names by replacing any |
@@ -457,11 +465,38 @@ impl Mutation { |
457 | 465 | warn!("Failed to increment tracker file: {e}"); |
458 | 466 | } |
459 | 467 |
|
| 468 | + let required_templates = ctx |
| 469 | + .field() |
| 470 | + .selection_set() |
| 471 | + .filter(|slct| slct.name() == "template") |
| 472 | + .flat_map(|slct| slct.arguments()) |
| 473 | + .filter_map(|args| { |
| 474 | + args.get(0).map(|arg| { |
| 475 | + let Value::String(name) = &arg.1 else { |
| 476 | + panic!("name isn't a string") |
| 477 | + }; |
| 478 | + name.into() |
| 479 | + }) |
| 480 | + }) |
| 481 | + .collect::<Vec<_>>(); |
| 482 | + let extra_templates = db |
| 483 | + .additional_templates(&beamline, required_templates) |
| 484 | + .await? |
| 485 | + .into_iter() |
| 486 | + .map(|template| { |
| 487 | + ( |
| 488 | + template.name, |
| 489 | + ScanTemplate::new_checked(&template.template).unwrap(), |
| 490 | + ) |
| 491 | + }) |
| 492 | + .collect(); |
| 493 | + |
460 | 494 | Ok(ScanPaths { |
461 | 495 | visit: VisitPath { |
462 | 496 | visit, |
463 | 497 | info: next_scan, |
464 | 498 | }, |
| 499 | + extra_templates, |
465 | 500 | subdirectory: sub.unwrap_or_default(), |
466 | 501 | }) |
467 | 502 | } |
|
0 commit comments