Skip to content

Commit 1dec836

Browse files
committed
Working named templating
Assuming everything is used correctly. No error handling yet.
1 parent 53de8b2 commit 1dec836

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/graphql.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use std::any;
1616
use std::borrow::Cow;
17+
use std::collections::HashMap;
1718
use std::error::Error;
1819
use std::fmt::Display;
1920
use std::future::Future;
@@ -146,6 +147,7 @@ struct VisitPath {
146147
/// GraphQL type to provide path data for the next scan for a given visit
147148
struct ScanPaths {
148149
visit: VisitPath,
150+
extra_templates: HashMap<String, PathTemplate<ScanField>>,
149151
subdirectory: Subdirectory,
150152
}
151153

@@ -233,6 +235,12 @@ impl ScanPaths {
233235
self.visit.info.scan_number()
234236
}
235237

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+
236244
/// The paths where the given detectors should write their files.
237245
///
238246
/// Detector names are normalised before being used in file names by replacing any
@@ -457,11 +465,38 @@ impl Mutation {
457465
warn!("Failed to increment tracker file: {e}");
458466
}
459467

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+
460494
Ok(ScanPaths {
461495
visit: VisitPath {
462496
visit,
463497
info: next_scan,
464498
},
499+
extra_templates,
465500
subdirectory: sub.unwrap_or_default(),
466501
})
467502
}

0 commit comments

Comments
 (0)