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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/// Error handling
pub mod err {

    use std::fmt::Display;

    /// The error enum
    #[derive(Debug)]
    pub enum Error {
        InvalidArgument(String),
        InvalidResponse(String),
        Internal(String),
        NotFound(String),
    }

    impl Display for Error {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            use Error::*;
            match self {
                InvalidArgument(ref s) => write!(f, "Invalid argument: '{}'", s),
                InvalidResponse(ref s) => write!(f, "Invalid response: '{}'", s),
                Internal(ref s) => write!(f, "Internal error: '{}'", s),
                NotFound(ref s) => write!(f, "Not found: '{}'", s),
            }
        }
    }

    impl std::error::Error for Error {}

    pub fn invalid_argument<T: Display>(t: T) -> Error {
        Error::InvalidArgument(t.to_string())
    }

    pub fn invalid_response<T: Display>(t: T) -> Error {
        Error::InvalidArgument(t.to_string())
    }

    pub fn internal<T: Display>(t: T) -> Error {
        Error::Internal(t.to_string())
    }

    pub fn not_found<T: Display>(t: T) -> Error {
        Error::NotFound(t.to_string())
    }


    /// Result wrapper: `Result<T, Error>`
    pub type Result<T> = std::result::Result<T, Error>;

}

use std::sync::Arc;

#[derive(Clone)]
pub struct Client {
    pub inner: Arc<reqwest::Client>,
    pub base_url: url::Url
}

impl Client {
    pub fn new<T: std::borrow::Borrow<str>>(url: T) -> err::Result<Self> {
        Ok(Client { 
            inner: Arc::new(reqwest::Client::new()), 
            base_url: url.borrow().parse().map_err(err::invalid_argument)?
        })
    }
}

pub mod metadata {

    use crate::err;
    use std::collections::HashMap;

    pub trait Format {
        type MetadataKind: std::fmt::Debug;

        const AS_STR: &'static str;

        fn parse_metadata(node: &roxmltree::Node) -> err::Result<Self::MetadataKind>;
    }

    pub enum OaiDc {}

    impl Format for OaiDc {
        type MetadataKind = HashMap<String, Vec<Option<String>>>;

        const AS_STR: &'static str = "oai_dc";

        fn parse_metadata(node: &roxmltree::Node) -> err::Result<Self::MetadataKind> {
            let mut map = HashMap::new();

            for dc_node in node
                .children()
                .filter(|x| x.has_tag_name("dc"))
                .flat_map(|x| x.children())
                .filter(|x| x.is_element())
            {
                map.entry(String::from(dc_node.tag_name().name()))
                    .or_insert_with(Vec::new)
                    .push(dc_node.text().map(String::from));
            }

            Ok(map)
        }
    }

    pub enum Xoai {}

    #[derive(Debug, serde::Serialize)]
    pub struct XoaiElements(pub Vec<XoaiElement>);

    #[derive(Debug, serde::Serialize)]
    pub struct XoaiElement {
        name: String,
        fields: Option<Vec<(Option<String>, Option<String>)>>,
        children: Option<Vec<Box<XoaiElement>>>
    }

    impl XoaiElement {
        fn from_node(node: &roxmltree::Node) -> err::Result<Self> {
            let name = node.attribute("name").ok_or_else(|| err::internal("No name"))?.to_string();
            let fields = node
                .children()
                .filter(|x| x.has_tag_name("field"))
                .map(|x| (x.attribute("name").map(String::from), x.text().map(String::from)))
                .collect::<Vec<_>>();
            let children = node
                .children()
                .filter(|x| x.has_tag_name("element"))
                .map(|x| XoaiElement::from_node(&x).map(Box::new))
                .collect::<err::Result<Vec<_>>>()?;
            Ok(XoaiElement {
                name,
                fields: if fields.is_empty() { None } else { Some(fields) },
                children: if children.is_empty() { None } else { Some(children) }
            })
        }
    }

    impl Format for Xoai {
        type MetadataKind = XoaiElements;

        const AS_STR: &'static str = "xoai";

        fn parse_metadata(node: &roxmltree::Node) -> err::Result<Self::MetadataKind> {
            let vec = node
                .children()
                .filter(|x| x.has_tag_name("metadata"))
                .flat_map(|x| x.children())
                .filter(|x| x.has_tag_name("element"))
                .map(|x| XoaiElement::from_node(&x))
                .collect::<err::Result<Vec<_>>>()?;

            Ok(XoaiElements(vec))
        }
    }

    #[derive(Debug, Clone, serde::Serialize)]
    pub struct Header {
        pub identifier: String,
        pub datestamp: chrono::DateTime<chrono::Utc>,
        pub set_spec: Vec<String>
    }

    #[derive(Debug, Clone, serde::Serialize)]
    pub struct Record<T> {
        pub header: Header,
        pub metadata: T
    }

}

pub mod get_record {

    use chrono::prelude::*;
    use std::marker::PhantomData;

    use crate::{
        err,
        ext::NodeExt,
        metadata,
        Client
    };

    #[derive(Debug, serde::Serialize)]
    pub struct GetRecord<T, U> {
        pub response_date: DateTime<Utc>,
        pub record: metadata::Record<U>,
        #[serde(skip_serializing)]
        marker: PhantomData<T>
    }

    impl<T: metadata::Format> GetRecord<T, T::MetadataKind> {

        fn build(identifier: &str, text: String,) -> err::Result<Self> {
            let doc = roxmltree::Document::parse(&text).map_err(err::invalid_response)?;

            let root = doc.root_element();

            let response_date = root.find_first_child_parsed_text("responseDate")?;

            let get_record = root.find_first_child("GetRecord")?;

            let record = get_record
                .children()
                .filter(|x| x.has_tag_name("record"))
                .flat_map(|x| -> err::Result<metadata::Record<T::MetadataKind>> {
                    let header = x.find_first_child("header")?;

                    let header = metadata::Header {
                        identifier: header.find_first_child_parsed_text("identifier")?,
                        datestamp: header.find_first_child_parsed_text("datestamp")?,
                        set_spec: header
                            .children()
                            .filter(|y| y.has_tag_name("setSpec"))
                            .flat_map(|x| x.text().map(String::from))
                            .collect()
                    };

                    let metadata_node = x.find_first_child("metadata")?;

                    let metadata = T::parse_metadata(&metadata_node)?;

                    Ok(metadata::Record { header, metadata })
                })
                .next()
                .ok_or_else(|| err::not_found(identifier))?;

            Ok(GetRecord { response_date, record, marker: PhantomData })
        }
    }

    impl Client {
        pub async fn get_record<T: metadata::Format>(
            &self,
            identifier: &str
        ) -> err::Result<GetRecord<T, T::MetadataKind>>
        {
 
            #[derive(serde::Serialize)]
            struct VerbedParams<'b> {
                identifier: &'b str,
                #[serde(rename = "metadataPrefix")]
                metadata_prefix: &'static str,
                verb: &'static str
            }

            let mut url = self.base_url.clone();

            let params = VerbedParams { identifier, metadata_prefix: T::AS_STR, verb: "GetRecord" };

            url.set_query(Some(&serde_urlencoded::to_string(&params).map_err(err::internal)?));

            let body = self.inner.get(url).send().await.map_err(err::internal)?
                .text().await.map_err(err::internal)?;

            Ok(GetRecord::<T, T::MetadataKind>::build(identifier, body)?)
        }
    }
}

pub mod list_records {

    use chrono::prelude::*;
    use std::marker::PhantomData;

    use crate::{
        err,
        ext::NodeExt,
        metadata,
        Client
    };

    #[derive(Debug, serde::Serialize)]
    pub struct ResumptionToken {
        value: Option<String>,
        complete_list_size: u64,
        cursor: u64
    }

    #[derive(Debug, serde::Serialize)]
    pub struct ListRecords<T, U> {
        pub response_date: DateTime<Utc>,
        pub records: Vec<metadata::Record<U>>,
        pub resumption_token: Option<ResumptionToken>,
        #[serde(skip_serializing)]
        marker: PhantomData<T>
    }

    #[derive(Default, Debug, serde::Serialize)]
    pub struct Params {
        pub from: Option<DateTime<Utc>>,
        pub until: Option<DateTime<Utc>>,
        pub set: Option<String>
    }

    impl<T: metadata::Format> ListRecords<T, T::MetadataKind> {
        pub fn has_next(&self) -> bool {
            self.resumption_token.as_ref().and_then(|x| x.value.as_ref()).is_some()
        }

        pub async fn get_next(self, client: &Client) -> err::Result<ListRecords<T, T::MetadataKind>>
        {
            if let Some(rt) = self
                .resumption_token
                .as_ref()
                .and_then(|x| x.value.as_ref())
                .map(|x| String::from(x as &str))
            {
                Ok(client.list_records_from_resumption_token(rt).await?)
            } else {
                Err(err::internal("No more results"))
            }
        }

        fn build(text: String) -> err::Result<ListRecords<T, T::MetadataKind>> {
            let doc = roxmltree::Document::parse(&text).map_err(err::invalid_response)?;

            let root = doc.root_element();

            let response_date = root.find_first_child_parsed_text("responseDate")?;

            let list_records = root.find_first_child("ListRecords")?;

            let resumption_token =
                list_records.find_first_child("resumptionToken").ok().and_then(|x| {
                    match (
                        x.text(),
                        x.attribute("completeListSize").and_then(|x| x.parse().ok()),
                        x.attribute("cursor").and_then(|x| x.parse().ok())
                    ) {
                        (value, Some(complete_list_size), Some(cursor)) => Some(ResumptionToken {
                            value: value.map(String::from),
                            complete_list_size,
                            cursor
                        }),
                        _ => None
                    }
                });

            let records = list_records
                .children()
                .filter(|x| x.has_tag_name("record"))
                .flat_map(|x| -> err::Result<metadata::Record<T::MetadataKind>> {
                    let header = x.find_first_child("header")?;

                    let header = metadata::Header {
                        identifier: header.find_first_child_parsed_text("identifier")?,
                        datestamp: header.find_first_child_parsed_text("datestamp")?,
                        set_spec: header
                            .children()
                            .filter(|y| y.has_tag_name("setSpec"))
                            .flat_map(|x| x.text().map(String::from))
                            .collect()
                    };

                    let metadata_node = x.find_first_child("metadata")?;

                    let metadata = T::parse_metadata(&metadata_node)?;

                    Ok(metadata::Record { header, metadata })
                })
                .collect();

            Ok(ListRecords { response_date, records, resumption_token, marker: PhantomData })
        }
    }

    impl Client {
        pub async fn list_records<T: metadata::Format>(
            &self,
            params: Option<Params>
        ) -> err::Result<ListRecords<T, T::MetadataKind>>
        {
            let params = params.unwrap_or_else(Params::default);

            #[derive(serde::Serialize)]
            struct VerbedParams {
                #[serde(flatten)]
                params: Params,
                #[serde(rename = "metadataPrefix")]
                metadata_prefix: &'static str,
                verb: &'static str
            }

            let mut url = self.base_url.clone();

            let params = VerbedParams { params, metadata_prefix: T::AS_STR, verb: "ListRecords" };

            url.set_query(Some(&serde_urlencoded::to_string(&params).map_err(err::internal)?));

            let body = self.inner.get(url).send().await.map_err(err::internal)?
                .text().await.map_err(err::internal)?;

            Ok(ListRecords::<T, T::MetadataKind>::build(body)?)
        }

        pub async fn list_records_from_resumption_token<T: metadata::Format>(
            &self,
            resumption_token: String
        ) -> err::Result<ListRecords<T, T::MetadataKind>>
        {
            #[derive(serde::Serialize)]
            struct VerbedParams {
                #[serde(rename = "resumptionToken")]
                resumption_token: String,
                verb: &'static str
            }

            let mut url = self.base_url.clone();

            let params = VerbedParams { resumption_token, verb: "ListRecords" };

            url.set_query(Some(&serde_urlencoded::to_string(&params).map_err(err::internal)?));

            let body = self.inner.get(url).send().await.map_err(err::internal)?
                .text().await.map_err(err::internal)?;

            Ok(ListRecords::<T, T::MetadataKind>::build(body)?)
        }

        pub async fn list_records_all<T: metadata::Format>(
            &self,
            params: Option<Params>
        ) -> err::Result<ListRecords<T, T::MetadataKind>>
        {
            let mut list_records = self.list_records::<T>(params).await?;
            while list_records.has_next() {
                let mut prev_records =
                    list_records.records.drain(..).collect::<Vec<_>>();

                list_records = list_records.get_next(self).await?;

                prev_records.extend(list_records.records.drain(..));

                list_records.records = prev_records;
            }

            Ok(list_records)
        }
    }
}

pub mod ext {

    pub use roxmltree;

    use roxmltree::{ExpandedName, Node};

    pub(crate) trait NodeExt<'a, 'd>: Sized {
        fn find_first_child<I: Into<ExpandedName<'a>>>(
            &self,
            tag_name: I
        ) -> crate::err::Result<Node<'a, 'd>>;
        fn find_first_child_parsed_text<P: std::str::FromStr, I: Into<ExpandedName<'a>>>(
            &'a self,
            tag_name: I
        ) -> crate::err::Result<P>;
    }

    impl<'a, 'd: 'a> NodeExt<'a, 'd> for Node<'a, 'd> {
        fn find_first_child<I: Into<ExpandedName<'a>>>(
            &self,
            tag_name: I
        ) -> crate::err::Result<Node<'a, 'd>>
        {
            let tag_name = tag_name.into();
            self.children()
                .filter(|x| x.has_tag_name(tag_name))
                .next()
                .ok_or_else(|| crate::err::internal(format!("No such tag: {}", tag_name.name())))
        }

        fn find_first_child_parsed_text<P: std::str::FromStr, I: Into<ExpandedName<'a>>>(
            &'a self,
            tag_name: I
        ) -> crate::err::Result<P>
        {
            let tag_name = tag_name.into();
            self.find_first_child(tag_name)?
                .text()
                .and_then(|x| x.parse().ok())
                .ok_or_else(|| crate::err::internal(format!("No text for: {}", tag_name.name())))
        }
    }

}