use std::fs::File;
use std::io::prelude::*;
use std::path::{Path};
use std::error::Error;
use url::Url;
pub async fn get_external_scripts_text(
script_urls: Vec<&str>
) -> Result<Vec<String>, Box<dyn Error>> {
let mut script_strings: Vec<String> = vec![];
for url in script_urls {
script_strings.push(reqwest::get(url).await?.text().await?);
}
Ok(script_strings)
}
pub fn get_local_scripts_text(
script_path_literals: Vec<&str>
) -> Result<Vec<String>, Box<dyn Error>> {
let mut script_strings: Vec<String> = vec![];
for path in script_path_literals {
script_strings.push(
get_local_script(Path::new(path)).unwrap()
)
}
Ok(script_strings)
}
pub fn get_local_script(path: &Path) -> Result<String, Box<dyn Error>> {
let mut file = File::open(path)?;
let mut text = String::new();
file.read_to_string(&mut text)?;
Ok(text)
}
pub fn get_local_file(path: &Path) -> Result<String, Box<dyn Error>> {
let mut file = File::open(path)?;
let mut text = String::new();
file.read_to_string(&mut text)?;
Ok(text)
}
pub async fn get_remote_file(url: Url) -> Result<String, Box<dyn Error>> {
let text = reqwest::get(url).await?.text().await?;
Ok(text)
}
pub async fn get_css_string(
css_urls: Vec<&str>
) -> Result<String, Box<dyn Error>> {
let mut css_string = String::from("");
for url in css_urls {
css_string += &reqwest::get(url).await?.text().await?;
}
Ok(css_string)
}