file_fetcher/
lib.rs

1//! The 'file_fetcher' crate can get you file either locally or over http(s).
2
3// @Author: Lashermes Ronan <ronan>
4// @Date:   25-07-2017
5// @Email:  ronan.lashermes@inria.fr
6// @Last modified by:   ronan
7// @Last modified time: 25-07-2017
8// @License: MIT
9
10// `error_chain!` can recurse deeply
11#![recursion_limit = "1024"]
12
13#[macro_use] extern crate error_chain;
14extern crate reqwest;
15extern crate serde;
16extern crate serde_json;
17
18pub mod errors;
19pub mod local;
20pub mod http;
21
22use errors::*;
23pub use reqwest::{Url, UrlError};
24use serde::de::DeserializeOwned;
25
26use std::io::Read;
27
28
29/// Open a string url to get bytes
30///
31/// # Arguments
32/// * 'url' - the url to open
33///
34/// # Examples
35///
36/// To load a local file (here ./src/lib.rs)
37///
38/// ```
39/// use std::fs;
40///
41/// let path = fs::canonicalize("./src/lib.rs").unwrap();
42/// let local_bytes = file_fetcher::open_bytes_str(&format!("file://{}", path.display())).unwrap();
43/// ```
44///
45/// To load a remote file
46///
47/// ```
48/// let remote_bytes = file_fetcher::open_bytes_str("https://gitlab.com/Artefaritaj/file-fetcher/raw/master/src/lib.rs").unwrap();
49/// ```
50pub fn open_bytes_str(url: &str) -> Result<Vec<u8>> {
51    let url = Url::parse(url)?;
52    open_bytes(url)
53}
54
55/// Open an url to get bytes
56pub fn open_bytes(url: Url) -> Result<Vec<u8>> {
57    let scheme = url.scheme().to_string();
58    match scheme.as_str() {
59        "http"|"https" => http::open_bytes(url),
60        "file" => local::open_bytes(url.path()),
61        prot => Err(format!("Protocol {} is not supported.", prot).into())
62    }
63}
64
65///Open a string url to get a reader
66///
67/// # Arguments
68/// * 'url' - the url to open
69///
70/// # Examples
71///
72/// To load a local file (here ./src/lib.rs)
73///
74/// ```
75/// use std::fs;
76///
77/// let path = fs::canonicalize("./src/lib.rs").unwrap();
78/// let local_reader = file_fetcher::open_str(&format!("file://{}", path.display())).unwrap();
79/// ```
80///
81/// To load a remote file
82///
83/// ```
84/// let remote_reader = file_fetcher::open_str("https://gitlab.com/Artefaritaj/file-fetcher/raw/master/src/lib.rs").unwrap();
85/// ```
86pub fn open_str(url: &str) -> Result<Box<dyn Read>> {
87    let url = Url::parse(url)?;
88    open(url)
89}
90
91/// Open an url to get a reader
92pub fn open(url: Url) -> Result<Box<dyn Read>> {
93    let scheme = url.scheme().to_string();
94    match scheme.as_str() {
95        "http"|"https" => http::open(url),
96        "file" => local::open(url.path()),
97        prot => Err(format!("Protocol {} is not supported.", prot).into())
98    }
99}
100
101/// Open an url to get a json struct
102pub fn open_json<T: DeserializeOwned>(url: Url) -> Result<T> {
103    let scheme = url.scheme().to_string();
104    match scheme.as_str() {
105        "http"|"https" => http::open_json(url),
106        "file" => local::open_json(url.path()),
107        prot => Err(format!("Protocol {} is not supported.", prot).into())
108    }
109}
110
111/// Open a string url to get a json struct
112pub fn open_json_str<T: DeserializeOwned>(url: &str) -> Result<T> {
113    let url = Url::parse(url)?;
114    open_json(url)
115}
116
117#[test]
118fn test_local() {
119    use std::fs;
120    let path = fs::canonicalize("./src/lib.rs").unwrap();
121    let local_bytes = open_bytes(Url::parse(&format!("file://{}", path.display())).unwrap()).unwrap();
122    assert!(local_bytes.len() > 0);
123
124    let _ = open(Url::parse(&format!("file://{}", path.display())).unwrap()).unwrap();
125}
126
127#[test]
128fn test_remote() {
129    let remote_bytes = open_bytes(Url::parse("https://gitlab.com/Artefaritaj/file-fetcher/raw/master/src/lib.rs").unwrap()).unwrap();
130    assert!(remote_bytes.len() > 0);
131
132    let _ = open(Url::parse("https://gitlab.com/Artefaritaj/file-fetcher/raw/master/src/lib.rs").unwrap()).unwrap();
133}