instapi/
lib.rs

1// Copyright © 2022 Nikita Dudko. All rights reserved.
2// Contacts: <nikita.dudko.95@gmail.com>
3// Licensed under the MIT License.
4
5//! Provides abstractions over the
6//! [Instagram Basic Display API](https://developers.facebook.com/docs/instagram-basic-display-api/).
7
8pub mod auth;
9pub mod user;
10
11use std::{error::Error, result, str::FromStr};
12
13const BASE_URL: &str = "https://graph.instagram.com";
14/// Used in requests related to the short-lived token retrieving.
15const AUTH_BASE_URL: &str = "https://api.instagram.com";
16const API_VERSION: &str = "v13.0";
17
18type Result<T> = result::Result<T, Box<dyn Error>>;
19
20/// Converts `Option<String>` to `Option<T>` using the [parse][str::parse] method.
21fn parse_opt<T, E>(opt: Option<String>) -> result::Result<Option<T>, E>
22where
23    T: FromStr<Err = E>,
24    E: Error,
25{
26    Ok(match opt {
27        Some(str) => Some(str.parse()?),
28        None => None,
29    })
30}
31
32#[cfg(test)]
33mod tests {
34    use std::num::ParseIntError;
35    use url::Url;
36
37    #[test]
38    fn parse_opt() {
39        let opt_str = Some("test:".to_string());
40        let opt_url: Option<Url> = super::parse_opt(opt_str).unwrap();
41        assert!(opt_url.is_some());
42
43        assert_eq!(super::parse_opt::<i32, ParseIntError>(None).unwrap(), None);
44    }
45}