Skip to content

Commit 1efa71c

Browse files
committed
Support custom placeholders in templates
1 parent 6bfa8c2 commit 1efa71c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/graphql/mod.rs

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

1515
use std::any;
1616
use std::borrow::Cow;
17+
use std::collections::HashMap;
18+
use std::error::Error;
1719
use std::future::Future;
1820
use std::io::Write;
1921
use std::path::{Component, PathBuf};
@@ -145,6 +147,7 @@ struct DirectoryPath {
145147
struct ScanPaths {
146148
directory: DirectoryPath,
147149
subdirectory: Subdirectory,
150+
metadata: HashMap<String, String>,
148151
}
149152

150153
/// GraphQL type to provide current configuration for an instrument
@@ -153,6 +156,12 @@ struct CurrentConfiguration {
153156
high_file: Option<u32>,
154157
}
155158

159+
#[derive(Debug, InputObject)]
160+
struct MetaKeyValue {
161+
key: String,
162+
value: String,
163+
}
164+
156165
/// Error to be returned when a path contains non-unicode characters
157166
#[derive(Debug, Display, Error)]
158167
#[display("Path contains non-unicode characters")]
@@ -308,6 +317,12 @@ impl FieldSource<ScanField> for ScanPaths {
308317
ScanField::Subdirectory => self.subdirectory.to_string().into(),
309318
ScanField::ScanNumber => self.directory.info.scan_number().to_string().into(),
310319
ScanField::Directory(dir) => self.directory.resolve(dir),
320+
ScanField::Custom(key) => self
321+
.metadata
322+
.get(key)
323+
.map(|s| s.as_str())
324+
.unwrap_or("")
325+
.into(),
311326
}
312327
}
313328
}
@@ -397,6 +412,7 @@ impl Mutation {
397412
instrument: String,
398413
instrument_session: String,
399414
sub: Option<Subdirectory>,
415+
meta: Option<Vec<MetaKeyValue>>,
400416
) -> async_graphql::Result<ScanPaths> {
401417
check_auth(ctx, |policy, token| {
402418
policy.check_access(token, &instrument, &instrument_session)
@@ -420,11 +436,18 @@ impl Mutation {
420436
warn!("Failed to increment tracker file: {e}");
421437
}
422438

439+
let metadata = meta
440+
.into_iter()
441+
.flatten()
442+
.map(|kv| (kv.key, kv.value))
443+
.collect();
444+
423445
Ok(ScanPaths {
424446
directory: DirectoryPath {
425447
instrument_session,
426448
info: next_scan,
427449
},
450+
metadata,
428451
subdirectory: sub.unwrap_or_default(),
429452
})
430453
}

src/paths.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,19 @@ pub enum DirectoryField {
3232
Instrument,
3333
}
3434

35-
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash)]
35+
#[derive(Debug, Display, Clone, PartialEq, Eq, Hash)]
3636
pub enum ScanField {
3737
#[display("subdirectory")]
3838
Subdirectory,
3939
#[display("scan_number")]
4040
ScanNumber,
4141
#[display("{_0}")]
4242
Directory(DirectoryField),
43+
#[display("{_0}")]
44+
Custom(String),
4345
}
4446

45-
#[derive(Debug, Display, Clone, Copy, PartialEq, Eq, Hash)]
47+
#[derive(Debug, Display, Clone, PartialEq, Eq, Hash)]
4648
pub enum DetectorField {
4749
#[display("detector")]
4850
Detector,
@@ -73,7 +75,10 @@ impl TryFrom<String> for ScanField {
7375
match value.as_str() {
7476
"scan_number" => Ok(ScanField::ScanNumber),
7577
"subdirectory" => Ok(ScanField::Subdirectory),
76-
_ => Ok(ScanField::Directory(DirectoryField::try_from(value)?)),
78+
_ => match DirectoryField::try_from(value) {
79+
Ok(bf) => Ok(ScanField::Directory(bf)),
80+
Err(InvalidKey(value)) => Ok(ScanField::Custom(value)),
81+
},
7782
}
7883
}
7984
}

0 commit comments

Comments
 (0)