Creating a Phoronix CLI Front-End Library

Now we can go a step further and separate the open_phoronix() and open_testing() functions from into it's own library, The open_phoronix() function shall be renamed as online() while open_testing() will be named offline(). That way, the code may be called conveniently with homepage::online() or homepage::offline(). At the same time, to remove any compiler warnings about our online() or offline() functions being unused, tag the functions with #[allow(dead_code)].

use hyper::Client;
use hyper::header::Connection;
use std::io::Read;

pub fn online() -> String {
    let client = Client::new();
    let mut response = client.get("").
    let mut body = String::new();
    response.read_to_string(&mut body).unwrap();
    return body;

pub fn offline() -> &'static str { include_str!("phoronix.html") }

Now it might be a good idea to separate the printing code from into our new front-end. Let's call this new function print() so that we can later call this in main() with phoronix_cli::print(). Because we now need to get our homepage string from the file, we need to make that change too.

use article::Article;
use homepage;

pub fn print() {
    let phoronix_articles = Article::get_articles(homepage::offline());
    for article in phoronix_articles.iter().rev() {
        println!("Title:   {}", article.title);
        println!("Details: {}", article.details);
        println!("Summary: {}\n", article.summary);

When you are finished with the program, make sure to make the correct changes in to reflect the newly-created libs. We will simply add mod homepage and mod phoronix_cli to the mod list and call our print() function in phoronix_cli with phoronix_cli::print().

Now our file should look like such:

extern crate hyper;
extern crate select;
mod article;
mod homepage;
mod phoronix_cli;

fn main() {

Run cargo run again to make sure it still builds and runs.