team-leader: Added bonus 1 exercise (in rust!) to repo (still to finish)

git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@21 a672b425-5310-4d7a-af5c-997e18724b81
This commit is contained in:
Claudio Maggioni 2018-10-31 19:37:45 +00:00
parent d3d35c6f81
commit a55855a02c
5 changed files with 1281 additions and 9 deletions

2
bonus1/.svnignore Normal file
View File

@ -0,0 +1,2 @@
target
**/*.rs.bk

1213
bonus1/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

9
bonus1/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "bonus1"
version = "0.1.0"
authors = ["Claudio Maggioni <claudio@maggioni.xyz>"]
[dependencies]
reqwest = "0.9.4"
regex = "1"
lazy_static = "1.1.0"

57
bonus1/src/main.rs Normal file
View File

@ -0,0 +1,57 @@
/// # Bonus 1: link linter
///
/// *Authors:* Claudio Maggioni, Joey Bevilacqua
///
/// Group 1
extern crate reqwest;
extern crate regex;
#[macro_use]
extern crate lazy_static;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use regex::Regex;
fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
fn find_urls(html: &str) -> Vec<String> {
let mut results: Vec<String> = Vec::new();
lazy_static! {
static ref RE: Regex = Regex::new("(?:(?:href)|(?:src))=\"([^\"]*)\"").unwrap();
}
for cap in RE.captures_iter(html) {
results.push(cap[1].to_string());
}
results
}
fn is_url_working(url: &str) -> bool {
let res: Result<reqwest::Response, reqwest::Error> = reqwest::get(url);
match res {
Ok(r) => r.status().is_success(),
Err(_) => false
}
}
#[test]
fn test_is_url_working() {
assert!(is_url_working("https://www.google.com"));
assert!(is_url_working("https://xkcd.com"));
assert!(!is_url_working("https://xkcd.com/404"));
assert!(!is_url_working("notaurl"));
}
fn main() {
}

View File

@ -1,9 +0,0 @@
---
---
@charset "utf-8";
@import "syntax-highlighting";
@import "footer";
@import "header";