1#![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
29pub fn open_bytes_str(url: &str) -> Result<Vec<u8>> {
51 let url = Url::parse(url)?;
52 open_bytes(url)
53}
54
55pub 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
65pub fn open_str(url: &str) -> Result<Box<dyn Read>> {
87 let url = Url::parse(url)?;
88 open(url)
89}
90
91pub 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
101pub 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
111pub 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}