liftoff 0.1.1

Get your coding project off the ground fast. See repo
Documentation
use crate::{consts::*, error::KickError, psuccess};
use leg;
use reqwest;
use std::{fs, io, path::Path};

pub fn download<S: AsRef<str>, P: AsRef<Path>>(link: S, target: &P) -> Result<(), KickError> {
    let target = target.as_ref();
    let link = link.as_ref();

    let mut response = reqwest::get(link)?;
    if !response.status().is_success() {
        return Err(KickError::RequestStatusError((
            link.to_owned(),
            response.status(),
        )));
    }

    // TODO file name online and local not matching
    let mut dest = {
        let _fname = response
            .url()
            .path_segments()
            .and_then(|segments| segments.last())
            .and_then(|name| if name.is_empty() { None } else { Some(name) })
            .unwrap_or("tmp.bin");

        fs::File::create(target)?
    };

    io::copy(&mut response, &mut dest)?;
    psuccess!("Downloaded file   {}", link);
    Ok(())
}

pub fn download_gitignore<S: AsRef<str>, P: AsRef<Path>>(
    lang: &S,
    target: &P,
) -> Result<(), KickError> {
    let url = format!("{}/{}", LIFTOFF_GITIGNORE_API, lang.as_ref());
    download(&url, target)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::{fs, path::Path};

    #[test]
    fn test_download() {
        let target = Path::new(".");
        let target_file = target.join("rust-logo-512x512.png");
        download(
            "https://www.rust-lang.org/logos/rust-logo-512x512.png",
            &target_file,
        )
        .unwrap();
        assert!(Path::exists(&target_file));
        fs::remove_file(target_file).unwrap();

        let target = Path::new("./target");
        let target_file = target.join("README.md");

        download(
            "https://raw.githubusercontent.com/juliangaal/mpu6050/master/README.md",
            &target_file,
        )
        .unwrap();
        assert!(Path::exists(&target_file));
        fs::remove_file(target_file).unwrap();

        let target_file = target.join(".gitignore");
        download_gitignore(&"c++", &target_file).unwrap();
        assert!(Path::exists(&target_file));

        fs::remove_file(target_file).unwrap();
    }

    #[test]
    fn test_url_regex() {
        let reg = regex::Regex::new(LIFTOFF_URL_REGEX).unwrap();
        assert!(!reg.is_match("www"));
        assert!(!reg.is_match("www.screwgoogle."));
        assert!(reg.is_match("https://www.screwgoogle.de"));
        assert!(reg.is_match("http://screwgoogle.de"));
        assert!(
            reg.is_match("https://raw.githubusercontent.com/juliangaal/mpu6050/master/README.md")
        );
    }
}