-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmod.rs
More file actions
213 lines (195 loc) · 5.59 KB
/
mod.rs
File metadata and controls
213 lines (195 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use std::collections::HashMap;
use std::fmt::Formatter;
use crate::AppState;
use axum::Router;
use dicom::core::dictionary::DataDictionaryEntry;
use dicom::core::ops::AttributeSelector;
use dicom::core::{DataDictionary, PrimitiveValue, Tag, VR};
use dicom::object::StandardDataDictionary;
use serde::de::{Error, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer};
mod aets;
mod home;
pub mod mwl;
pub mod qido;
pub mod stow;
pub mod wado;
pub fn routes(base_path: &str) -> Router<AppState> {
let router = Router::new()
.merge(home::routes())
.merge(aets::routes())
.nest(
"/aets/{aet}",
Router::new()
.merge(qido::routes())
.merge(wado::routes())
.merge(stow::routes())
.merge(mwl::routes()),
);
// axum no longer supports nesting at the root
match base_path {
"/" | "" => router,
base_path => Router::new().nest(base_path, router),
}
}
/// Match Query Parameters for QIDO and MWL requests.
#[derive(Debug, Deserialize, PartialEq)]
#[serde(try_from = "HashMap<String, String>")]
pub struct MatchCriteria(Vec<(AttributeSelector, PrimitiveValue)>);
impl MatchCriteria {
pub fn into_inner(self) -> Vec<(AttributeSelector, PrimitiveValue)> {
self.0
}
}
impl TryFrom<HashMap<String, String>> for MatchCriteria {
type Error = String;
fn try_from(value: HashMap<String, String>) -> Result<Self, Self::Error> {
let criteria: Vec<(AttributeSelector, PrimitiveValue)> = value
.into_iter()
.map(|(key, value)| {
StandardDataDictionary
.parse_selector(&key)
.map_err(|err| format!("invalid attribute selector {key}: {err}"))
.and_then(|selector| {
to_primitive_value(selector.last_tag(), &value)
.map(|primitive| (selector, primitive))
})
})
.collect::<Result<_, Self::Error>>()?;
Ok(Self(criteria))
}
}
/// helper function to convert a query parameter value to a `PrimitiveValue`
fn to_primitive_value(tag: Tag, raw_value: &str) -> Result<PrimitiveValue, String> {
if raw_value.is_empty() {
return Ok(PrimitiveValue::Empty);
}
let vr = StandardDataDictionary
.by_tag(tag)
.ok_or_else(|| format!("unknown tag {tag}"))?
.vr();
match vr.relaxed() {
// String-like VRs, no parsing required
VR::AE
| VR::AS
| VR::CS
| VR::DA
| VR::DS
| VR::DT
| VR::IS
| VR::LO
| VR::LT
| VR::PN
| VR::SH
| VR::ST
| VR::TM
| VR::UC
| VR::UR
| VR::UT => Ok(PrimitiveValue::from(raw_value)),
// uid-list-match: a comma-separated list of UIDs
// See https://dicom.nema.org/medical/dicom/current/output/html/part18.html#sect_8.3.4.1
VR::UI => {
let uids: Vec<String> = raw_value.split(',').map(|s| s.trim().to_owned()).collect();
Ok(PrimitiveValue::Strs(uids.into()))
}
// Numeric VRs, parsing required
VR::SS => {
let value = raw_value.parse::<i16>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::US => {
let value = raw_value.parse::<u16>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::SL => {
let value = raw_value.parse::<i32>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::UL => {
let value = raw_value.parse::<u32>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::SV => {
let value = raw_value.parse::<i64>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::UV => {
let value = raw_value.parse::<u64>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::FL => {
let value = raw_value.parse::<f32>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
VR::FD => {
let value = raw_value.parse::<f64>().map_err(|err| err.to_string())?;
Ok(PrimitiveValue::from(value))
}
_ => Err(format!(
"Attribute {tag} cannot be used for matching due to unsupported VR {vr:?}",
)),
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum IncludeField {
All,
List(Vec<Tag>),
}
impl Default for IncludeField {
fn default() -> Self {
Self::List(Vec::new())
}
}
/// Custom deserialization visitor for repeated `includefield` query parameters.
/// It collects all `includefield` parameters in [`crate::dicomweb::qido::IncludeField::List`].
/// If at least one `includefield` parameter has the value `all`,
/// [`crate::dicomweb::qido::IncludeField::All`] is returned instead.
struct IncludeFieldVisitor;
impl<'a> Visitor<'a> for IncludeFieldVisitor {
type Value = IncludeField;
fn expecting(&self, formatter: &mut Formatter) -> std::fmt::Result {
write!(formatter, "a value of <{{attribute}}* | all>")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
if v.to_lowercase() == "all" {
Ok(IncludeField::All)
} else {
v.split(',')
.map(|v| {
let entry = StandardDataDictionary
.by_expr(v)
.ok_or_else(|| E::custom(format!("unknown tag {v}")))?;
Ok(entry.tag())
})
.collect::<Result<Vec<_>, _>>()
.map(IncludeField::List)
}
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'a>,
{
let mut items = Vec::new();
while let Some(item) = seq.next_element::<String>()? {
// If includefield=all, then all other includefield parameters are ignored
if &item.to_lowercase() == "all" {
return Ok(IncludeField::All);
}
let entry = StandardDataDictionary
.by_expr(&item)
.ok_or_else(|| Error::custom(format!("unknown tag {item}")))?;
items.push(entry.tag());
}
Ok(IncludeField::List(items))
}
}
/// See [`IncludeFieldVisitor`].
fn deserialize_includefield<'de, D>(deserializer: D) -> Result<IncludeField, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_any(IncludeFieldVisitor)
}