Skip to content

Commit b78c16d

Browse files
committed
Working named templating
Assuming everything is used correctly. No error handling yet.
1 parent c0b9ce1 commit b78c16d

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

src/graphql/mod.rs

Lines changed: 37 additions & 2 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::future::Future;
1819
use std::io::Write;
1920
use std::path::{Component, PathBuf};
@@ -144,6 +145,7 @@ struct DirectoryPath {
144145
/// GraphQL type to provide path data for the next scan for a given instrument session
145146
struct ScanPaths {
146147
directory: DirectoryPath,
148+
extra_templates: HashMap<String, PathTemplate<ScanField>>,
147149
subdirectory: Subdirectory,
148150
}
149151

@@ -224,6 +226,12 @@ impl ScanPaths {
224226
self.directory.info.scan_number()
225227
}
226228

229+
async fn template(&self, name: String) -> async_graphql::Result<String> {
230+
Ok(path_to_string(
231+
self.extra_templates.get(&name).unwrap().render(self),
232+
)?)
233+
}
234+
227235
/// The paths where the given detectors should write their files.
228236
///
229237
/// Detector names are normalised before being used in file names by replacing any
@@ -410,7 +418,7 @@ impl Query {
410418
names: Option<Vec<String>>,
411419
) -> async_graphql::Result<Vec<NamedTemplate>> {
412420
check_auth(ctx, |policy, token| {
413-
policy.check_beamline_admin(token, &beamline)
421+
policy.check_instrument_admin(token, &beamline)
414422
})
415423
.await?;
416424
let db = ctx.data::<SqliteScanPathService>()?;
@@ -455,11 +463,38 @@ impl Mutation {
455463
warn!("Failed to increment tracker file: {e}");
456464
}
457465

466+
let required_templates = ctx
467+
.field()
468+
.selection_set()
469+
.filter(|slct| slct.name() == "template")
470+
.flat_map(|slct| slct.arguments())
471+
.filter_map(|args| {
472+
args.get(0).map(|arg| {
473+
let Value::String(name) = &arg.1 else {
474+
panic!("name isn't a string")
475+
};
476+
name.into()
477+
})
478+
})
479+
.collect::<Vec<_>>();
480+
let extra_templates = db
481+
.additional_templates(&instrument, required_templates)
482+
.await?
483+
.into_iter()
484+
.map(|template| {
485+
(
486+
template.name,
487+
ScanTemplate::new_checked(&template.template).unwrap(),
488+
)
489+
})
490+
.collect();
491+
458492
Ok(ScanPaths {
459493
directory: DirectoryPath {
460494
instrument_session,
461495
info: next_scan,
462496
},
497+
extra_templates,
463498
subdirectory: sub.unwrap_or_default(),
464499
})
465500
}
@@ -494,7 +529,7 @@ impl Mutation {
494529
beamline: String,
495530
template: TemplateInput,
496531
) -> async_graphql::Result<NamedTemplate> {
497-
check_auth(ctx, |pc, token| pc.check_beamline_admin(token, &beamline)).await?;
532+
check_auth(ctx, |pc, token| pc.check_instrument_admin(token, &beamline)).await?;
498533
let db = ctx.data::<SqliteScanPathService>()?;
499534
Ok(db
500535
.register_template(&beamline, template.name, template.template)

0 commit comments

Comments
 (0)