readap 0.2.0

A parser for the OpenDAP DAP2 protocol
Documentation
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! URL Builder for OpenDAP endpoints
//!
//! This module provides a builder pattern for constructing OpenDAP URLs with support for
//! DAS (Dataset Attribute Structure), DDS (Dataset Descriptor Structure), and DODS
//! (Dataset Data Structure) endpoints, including constraint expressions for subsetting data.
//!
//! # Examples
//!
//! ## Basic URL construction
//! ```
//! use readap::UrlBuilder;
//!
//! let builder = UrlBuilder::new("https://example.com/data/dataset");
//! assert_eq!(builder.das_url(), "https://example.com/data/dataset.das");
//! assert_eq!(builder.dds_url(), "https://example.com/data/dataset.dds");
//! assert_eq!(builder.dods_url().unwrap(), "https://example.com/data/dataset.dods");
//! ```
//!
//! ## Variable selection and constraints
//! ```
//! use readap::{UrlBuilder, IndexRange};
//!
//! let url = UrlBuilder::new("https://example.com/data/ocean")
//!     .add_variable("temperature")
//!     .add_multidimensional_constraint("temperature", vec![
//!         IndexRange::Range { start: 0, end: 10, stride: None },      // time
//!         IndexRange::Single(5),                                      // depth
//!         IndexRange::Range { start: 20, end: 50, stride: Some(2) },  // latitude
//!         IndexRange::Range { start: -180, end: 180, stride: None },  // longitude
//!     ])
//!     .dods_url()
//!     .unwrap();
//!
//! assert_eq!(url, "https://example.com/data/ocean.dods?temperature[0:10][5][20:2:50][-180:180]");
//! ```

use crate::errors::Error;
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct UrlBuilder {
    base_url: String,
    variables: Vec<String>,
    constraints: HashMap<String, Vec<Constraint>>,
}

#[derive(Debug, Clone)]
pub struct Constraint {
    pub variable: String,
    pub indices: Vec<IndexRange>,
}

#[derive(Debug, Clone)]
pub enum IndexRange {
    Single(isize),
    Range {
        start: isize,
        end: isize,
        stride: Option<isize>,
    },
}

impl UrlBuilder {
    pub fn new<S: Into<String>>(base_url: S) -> Self {
        let mut url = base_url.into();
        if url.ends_with('/') {
            url.pop();
        }

        Self {
            base_url: url,
            variables: Vec::new(),
            constraints: HashMap::new(),
        }
    }

    pub fn das_url(&self) -> String {
        format!("{}.das", self.base_url)
    }

    pub fn dds_url(&self) -> String {
        format!("{}.dds", self.base_url)
    }

    pub fn dods_url(&self) -> Result<String, Error> {
        let mut url = format!("{}.dods", self.base_url);

        if !self.variables.is_empty() || !self.constraints.is_empty() {
            url.push('?');

            let mut parts = Vec::new();

            // Process variables that were explicitly added
            for variable in &self.variables {
                if let Some(constraints) = self.constraints.get(variable) {
                    // Combine all constraints for this variable into a single expression
                    let mut combined_constraint = variable.clone();
                    for constraint in constraints {
                        for index_range in &constraint.indices {
                            combined_constraint.push_str(&format!("[{index_range}]"));
                        }
                    }
                    parts.push(combined_constraint);
                } else {
                    parts.push(variable.clone());
                }
            }

            // Process constraints for variables that weren't explicitly added
            for (variable, constraints) in &self.constraints {
                if !self.variables.contains(variable) {
                    for constraint in constraints {
                        parts.push(constraint.to_string());
                    }
                }
            }

            url.push_str(&parts.join(","));
        }

        Ok(url)
    }

    pub fn add_variable<S: Into<String>>(mut self, variable: S) -> Self {
        self.variables.push(variable.into());
        self
    }

    pub fn add_variables<S: Into<String>>(mut self, variables: Vec<S>) -> Self {
        for var in variables {
            self.variables.push(var.into());
        }
        self
    }

    pub fn add_constraint(mut self, constraint: Constraint) -> Self {
        self.constraints
            .entry(constraint.variable.clone())
            .or_default()
            .push(constraint);
        self
    }

    pub fn add_index_constraint<S: Into<String>>(
        self,
        variable: S,
        indices: Vec<IndexRange>,
    ) -> Self {
        let constraint = Constraint {
            variable: variable.into(),
            indices,
        };
        self.add_constraint(constraint)
    }

    pub fn add_single_index<S: Into<String>>(self, variable: S, index: isize) -> Self {
        self.add_index_constraint(variable, vec![IndexRange::Single(index)])
    }

    pub fn add_range<S: Into<String>>(
        self,
        variable: S,
        start: isize,
        end: isize,
        stride: Option<isize>,
    ) -> Self {
        self.add_index_constraint(variable, vec![IndexRange::Range { start, end, stride }])
    }

    pub fn add_multidimensional_constraint<S: Into<String>>(
        self,
        variable: S,
        indices: Vec<IndexRange>,
    ) -> Self {
        self.add_index_constraint(variable, indices)
    }

    pub fn clear_variables(mut self) -> Self {
        self.variables.clear();
        self
    }

    pub fn clear_constraints(mut self) -> Self {
        self.constraints.clear();
        self
    }

    pub fn clear_all(mut self) -> Self {
        self.variables.clear();
        self.constraints.clear();
        self
    }
}

impl Constraint {
    pub fn new<S: Into<String>>(variable: S, indices: Vec<IndexRange>) -> Self {
        Self {
            variable: variable.into(),
            indices,
        }
    }

    pub fn single<S: Into<String>>(variable: S, index: isize) -> Self {
        Self::new(variable, vec![IndexRange::Single(index)])
    }

    pub fn range<S: Into<String>>(
        variable: S,
        start: isize,
        end: isize,
        stride: Option<isize>,
    ) -> Self {
        Self::new(variable, vec![IndexRange::Range { start, end, stride }])
    }
}

impl std::fmt::Display for Constraint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.variable)?;
        for index_range in &self.indices {
            write!(f, "[{index_range}]")?;
        }
        Ok(())
    }
}

impl std::fmt::Display for IndexRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IndexRange::Single(index) => write!(f, "{index}"),
            IndexRange::Range { start, end, stride } => {
                if let Some(stride) = stride {
                    write!(f, "{start}:{stride}:{end}")
                } else {
                    write!(f, "{start}:{end}")
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic_urls() {
        let builder = UrlBuilder::new("http://example.com/data/dataset");

        assert_eq!(builder.das_url(), "http://example.com/data/dataset.das");
        assert_eq!(builder.dds_url(), "http://example.com/data/dataset.dds");
        assert_eq!(
            builder.dods_url().unwrap(),
            "http://example.com/data/dataset.dods"
        );
    }

    #[test]
    fn test_trailing_slash_removal() {
        let builder = UrlBuilder::new("http://example.com/data/dataset/");

        assert_eq!(builder.das_url(), "http://example.com/data/dataset.das");
        assert_eq!(builder.dds_url(), "http://example.com/data/dataset.dds");
        assert_eq!(
            builder.dods_url().unwrap(),
            "http://example.com/data/dataset.dods"
        );
    }

    #[test]
    fn test_simple_variable_selection() {
        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_variable("pressure");

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature,pressure"
        );
    }

    #[test]
    fn test_single_index_constraint() {
        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_single_index("temperature", 5);

        let url = builder.dods_url().unwrap();
        assert_eq!(url, "http://example.com/data/dataset.dods?temperature[5]");
    }

    #[test]
    fn test_range_constraint() {
        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_range("temperature", 0, 10, None);

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:10]"
        );
    }

    #[test]
    fn test_range_with_stride() {
        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_range("temperature", 0, 20, Some(2));

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:2:20]"
        );
    }

    #[test]
    fn test_multiple_dimensions() {
        let constraint = Constraint::new(
            "temperature",
            vec![
                IndexRange::Range {
                    start: 0,
                    end: 10,
                    stride: None,
                },
                IndexRange::Single(5),
                IndexRange::Range {
                    start: 20,
                    end: 30,
                    stride: Some(2),
                },
            ],
        );

        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_constraint(constraint);

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:10][5][20:2:30]"
        );
    }

    #[test]
    fn test_multiple_variables_with_constraints() {
        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_variable("pressure")
            .add_range("temperature", 0, 10, None)
            .add_single_index("pressure", 3);

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:10],pressure[3]"
        );
    }

    #[test]
    fn test_constraint_without_variable() {
        let builder = UrlBuilder::new("http://example.com/data/dataset").add_range(
            "temperature",
            0,
            10,
            None,
        );

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:10]"
        );
    }

    #[test]
    fn test_clear_operations() {
        let builder = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_variable("pressure")
            .add_range("temperature", 0, 10, None)
            .clear_variables();

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:10]"
        );

        let builder = builder.clear_constraints();
        let url = builder.dods_url().unwrap();
        assert_eq!(url, "http://example.com/data/dataset.dods");
    }

    #[test]
    fn test_constraint_display() {
        let constraint = Constraint::single("temp", 5);
        assert_eq!(constraint.to_string(), "temp[5]");

        let constraint = Constraint::range("temp", 0, 10, None);
        assert_eq!(constraint.to_string(), "temp[0:10]");

        let constraint = Constraint::range("temp", 0, 20, Some(2));
        assert_eq!(constraint.to_string(), "temp[0:2:20]");
    }

    #[test]
    fn test_index_range_display() {
        let single = IndexRange::Single(5);
        assert_eq!(single.to_string(), "5");

        let range = IndexRange::Range {
            start: 0,
            end: 10,
            stride: None,
        };
        assert_eq!(range.to_string(), "0:10");

        let range_with_stride = IndexRange::Range {
            start: 0,
            end: 20,
            stride: Some(2),
        };
        assert_eq!(range_with_stride.to_string(), "0:2:20");
    }

    #[test]
    fn test_builder_pattern_chaining() {
        let url = UrlBuilder::new("http://example.com/data/dataset")
            .add_variable("temperature")
            .add_variable("pressure")
            .add_range("temperature", 0, 10, None)
            .add_single_index("pressure", 5)
            .dods_url()
            .unwrap();

        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature[0:10],pressure[5]"
        );
    }

    #[test]
    fn test_add_variables_batch() {
        let builder = UrlBuilder::new("http://example.com/data/dataset").add_variables(vec![
            "temperature",
            "pressure",
            "humidity",
        ]);

        let url = builder.dods_url().unwrap();
        assert_eq!(
            url,
            "http://example.com/data/dataset.dods?temperature,pressure,humidity"
        );
    }
}