Skip to content

Commit 39dea54

Browse files
committed
Support custom placeholders in templates
1 parent a7a28cc commit 39dea54

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/graphql.rs

Lines changed: 22 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;
@@ -147,6 +148,7 @@ struct VisitPath {
147148
struct ScanPaths {
148149
visit: VisitPath,
149150
subdirectory: Subdirectory,
151+
metadata: HashMap<String, String>,
150152
}
151153

152154
/// GraphQL type to provide current configuration for a beamline
@@ -155,6 +157,12 @@ struct CurrentConfiguration {
155157
high_file: Option<u32>,
156158
}
157159

160+
#[derive(Debug, InputObject)]
161+
struct MetaKeyValue {
162+
key: String,
163+
value: String,
164+
}
165+
158166
/// Error to be returned when a path contains non-unicode characters
159167
#[derive(Debug)]
160168
struct NonUnicodePath;
@@ -301,6 +309,12 @@ impl FieldSource<ScanField> for ScanPaths {
301309
ScanField::Subdirectory => self.subdirectory.to_string().into(),
302310
ScanField::ScanNumber => self.visit.info.scan_number().to_string().into(),
303311
ScanField::Beamline(bl) => self.visit.resolve(bl),
312+
ScanField::Custom(key) => self
313+
.metadata
314+
.get(key)
315+
.map(|s| s.as_str())
316+
.unwrap_or("")
317+
.into(),
304318
}
305319
}
306320
}
@@ -399,6 +413,7 @@ impl Mutation {
399413
beamline: String,
400414
visit: String,
401415
sub: Option<Subdirectory>,
416+
meta: Option<Vec<MetaKeyValue>>,
402417
) -> async_graphql::Result<ScanPaths> {
403418
check_auth(ctx, |policy, token| {
404419
policy.check_access(token, &beamline, &visit)
@@ -422,11 +437,18 @@ impl Mutation {
422437
warn!("Failed to increment tracker file: {e}");
423438
}
424439

440+
let metadata = meta
441+
.into_iter()
442+
.flatten()
443+
.map(|kv| (kv.key, kv.value))
444+
.collect();
445+
425446
Ok(ScanPaths {
426447
visit: VisitPath {
427448
visit,
428449
info: next_scan,
429450
},
451+
metadata,
430452
subdirectory: sub.unwrap_or_default(),
431453
})
432454
}

src/paths.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ pub enum BeamlineField {
2727
Instrument,
2828
}
2929

30-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3131
pub enum ScanField {
3232
Subdirectory,
3333
ScanNumber,
3434
Beamline(BeamlineField),
35+
Custom(String),
3536
}
3637

37-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
3839
pub enum DetectorField {
3940
Detector,
4041
Scan(ScanField),
@@ -57,6 +58,7 @@ impl Display for ScanField {
5758
ScanField::Subdirectory => f.write_str("subdirectory"),
5859
ScanField::ScanNumber => f.write_str("scan_number"),
5960
ScanField::Beamline(bl) => write!(f, "{bl}"),
61+
ScanField::Custom(field) => write!(f, "{field}"),
6062
}
6163
}
6264
}
@@ -100,7 +102,10 @@ impl TryFrom<String> for ScanField {
100102
match value.as_str() {
101103
"scan_number" => Ok(ScanField::ScanNumber),
102104
"subdirectory" => Ok(ScanField::Subdirectory),
103-
_ => Ok(ScanField::Beamline(BeamlineField::try_from(value)?)),
105+
_ => match BeamlineField::try_from(value) {
106+
Ok(bf) => Ok(ScanField::Beamline(bf)),
107+
Err(InvalidKey(value)) => Ok(ScanField::Custom(value)),
108+
},
104109
}
105110
}
106111
}

0 commit comments

Comments
 (0)