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
/// Re-export slog
///
/// Users of this library can, but don't have to use slog to build their own loggers
#[macro_use]
pub extern crate slog ;
extern crate slog_stdlog;

use slog::DrainExt;

#[macro_use]
extern crate lazy_static;

extern crate regex;
use regex::Regex;

extern crate reqwest;
use reqwest::Url;

extern crate select;
use select::document::Document;
use select::predicate::Name;

use std::io::Read;

#[derive(Debug,PartialEq)]
pub struct RecID<'a> {
    pub id: &'a str,
}

/// Create RecID from &str
///
/// Returns a `Result<Self, ()>` as this can fail.
/// In future I may also implement `std::convert::TryFrom`, currently a [nightly only
/// feature](https://github.com/rust-lang/rust/issues/33417).
///
/// # Examples
///
/// ```
/// extern crate libinspire;
/// libinspire::RecID::new("Bekenstein:1973ur");
/// ```
impl<'a> RecID<'a> {
    pub fn new(s: &'a str) -> Result<Self, ()> {
        match validate_recid(s) {
            true => Ok(RecID { id: s }),
            false => Err(()),
        }
    }
}

/// Test whether a string is a valid Inspire bibliographic code
///
/// # Examples
///
/// ```
/// assert!(libinspire::validate_recid("Nambu:1961tp"))
/// ```
pub fn validate_recid(code: &str) -> bool {
    // Use lazy_static to ensure that regexes are compiled only once
    lazy_static! {
        static ref REGEX: Regex = Regex::new(
            r"^[[:alpha:].]+:[[:digit:]]{4}[[:alpha:]]{2,3}$").unwrap();
    }

    REGEX.is_match(code)
}

pub struct Api {
    logger: slog::Logger,
}

impl Api {
    /// Initialize API
    ///
    /// Either provide a custom slog::Logger or default to the standard `log`
    /// crate.
    ///
    /// # Examples
    /// ```
    /// libinspire::Api::init(None);
    /// ```
    pub fn init(logger: Option<slog::Logger>) -> Self {
        Api {
            logger: logger.unwrap_or_else(|| slog::Logger::root(slog_stdlog::StdLog.fuse(), o!())),
        }
    }

    /// Fetches BibTeX entries from inspire.net.
    ///
    /// # Examples
    ///
    /// ```
    /// let inspire = libinspire::Api::init(None);
    ///
    /// println!("{}", inspire.fetch_bibtex_with_key(
    ///     libinspire::RecID::new("Abramovici:1992ah").unwrap()).expect("Error"));
    /// ```
    pub fn fetch_bibtex_with_key(&self, key: RecID) -> Option<String> {
        let mut api_url: Url = Url::parse("http://inspirehep.net")
            .expect("Unable to parse API URL")
            .join("search")
            .unwrap();
        api_url
            .query_pairs_mut()
            .append_pair("of", "hx")
            .append_pair("p", &key.id);

        debug!(self.logger, "Querying inspire API";
               "URL" => api_url.to_string());
        let mut response = reqwest::get(api_url).expect("Failed to send get request");
        debug!(self.logger, "GET request completed";
               "HTTP response status" => response.status().to_string());

        let mut html = String::new();
        response
            .read_to_string(&mut html)
            .expect("Failed to read response.");

        let document = Document::from(html.as_str());

        Some(document
                 .find(Name("pre"))
                 .first()
                 .expect("No text found.")
                 .text())
    }
}

#[cfg(test)]
mod tests {
    // #[test]
    // fn it_works() {}
}