<commit_before>use std::env;
use std::fs::File;
use std::io::{self, Read};
use handlebars::{Handlebars, to_json};

extern crate handlebars;

#[macro_use]
extern crate serde_derive;
extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct ButtonConfig {
    value: String,
    trigger: String
}

#[derive(Serialize, Deserialize, Debug)]
struct ManyConfig {
    value: Vec<String>
}

#[derive(Serialize, Deserialize, Debug)]
struct GeneralConfig {
    value: String
}

struct Col {
    pub buffers: Vec<String>
}
impl Col {
    fn new() -> Col {
        Col { buffers: Vec::new() }
    }
    fn add_buf(&mut self) {
        self.buffers.push(String::new());
    }
    fn append(&mut self, chr: char) {
        let mut len = self.buffers.len();

        if len == 0 {
            self.add_buf();
            len = 1;
        }

        let ref mut buffer = self.buffers[len-1];
        buffer.push(chr);
    }
    fn append_str(&mut self, str: &str) {
        for chr in str.chars() {
            self.append(chr);
        }
    }
    fn out(&self, template: &Template) -> String {
        let mut out: String = String::new();

        out.push_str(template.col_start.as_str());
        for ref buf in self.buffers.iter() {
            out.push_str(format!("{}{}{}",
                    template.segment_start.as_str(),
                    buf,
                    template.segment_end.as_str()
                ).as_str());
        }
        out.push_str(template.col_end.as_str());

        out
    }
}

struct Row {
    cols: Vec<Col>
}
impl Row {
    fn add_col(&mut self, col: Col) {
        self.cols.push(col);
    }
    fn get_col(&mut self, index: u8) -> &mut Col {
        self.cols.get_mut(index as usize).unwrap()
    }
    fn col_count(&self) -> usize {
        self.cols.len()
    }
    fn new() -> Row {
        let mut row: Row = Row { cols: Vec::new() };
        row.add_col(Col::new());
        row
    }
    fn out(&self, template: &Template) -> String {
        let mut out: String = String::new();

        out.push_str("\n");
        out.push_str(template.row_start.as_str());
        for ref col in self.cols.iter() {
            let formatted = format!("{}", col.out(template));
            out.push_str(formatted.as_str());
        }
        out.push_str(template.row_end.as_str());

        out
    }
}

struct Page {
    name: String,
    rows: Vec<Row>
}
impl Page {
    fn new(name: String) -> Page {
        let mut page: Page = Page { name: name, rows: Vec::new() };
        page.add_row(Row::new());
        page
    }
    fn add_row(&mut self, row: Row) {
        self.rows.push(row);
    }
    fn get_current_row(&mut self) -> &mut Row {
        let len = self.rows.len();
        self.rows.get_mut(len-1).unwrap()
    }
    fn out(&self, template: &Template) -> String {
        let mut out: String = String::new();

        let config = to_json(&GeneralConfig {value: self.name.to_string()});
        out.push_str(render_template(template.container_start.as_str(), &config).as_str());

        for ref row in self.rows.iter() {
            let formatted = format!("{}", row.out(template));
            out.push_str(formatted.as_str());
        }

        out.push_str(render_template(template.container_end.as_str(), &config).as_str());

        out
    }
}

struct Template {
    head: String,
    foot: String,
    container_start: String,
    container_end: String,
    row_start: String,
    row_end: String,
    col_start: String,
    col_end: String,
    segment_start: String,
    segment_end: String,
    label: String,
    text: String,
    checkbox: String,
    radio: String,
    textarea: String,
    hr: String,
    button: String,
    select: String
}
impl Template {
    fn from_vec(vec: &Vec<&str>) -> Template {
        Template {
            head: vec[0].to_string().trim().to_string(),
            foot: vec[1].to_string().trim().to_string(),
            container_start: vec[2].to_string().trim().to_string(),
            container_end: vec[3].to_string().trim().to_string(),
            row_start: vec[4].to_string().trim().to_string(),
            row_end: vec[5].to_string().trim().to_string(),
            col_start: vec[6].to_string().trim().to_string(),
            col_end: vec[7].to_string().trim().to_string(),
            segment_start: vec[8].to_string().trim().to_string(),
            segment_end: vec[9].to_string().trim().to_string(),
            label: vec[10].to_string().trim().to_string(),
            text: vec[11].to_string().trim().to_string(),
            checkbox: vec[12].to_string().trim().to_string(),
            radio: vec[13].to_string().trim().to_string(),
            textarea: vec[14].to_string().trim().to_string(),
            button: vec[15].to_string().trim().to_string(),
            select: vec[16].to_string().trim().to_string(),
            hr: vec[17].to_string().trim().to_string()
        }
    }
}

#[derive(PartialEq)]
enum InputType {
    Text,
    Radio,
    Checkbox,
    Textarea
}

#[derive(PartialEq)]
enum Type {
    Unknown,
    Input(InputType),
    Button,
    Label,
    Hr,
    Select
}

struct Working {
    str: String,
    until: char,
    work_type: Type
}
impl Working {
    fn new(until: char, work_type: Type) -> Working {
        Working {
            str: String::new(),
            until: until,
            work_type: work_type
        }
    }
    fn is_working(&self) -> bool {
        self.until != '\0'
    }
    fn append(&mut self, chr: char) {
        self.str.push(chr);
    }
    fn compile(&mut self, template: &Template) -> String {
        // Convert type to template
        let ref template = match self.work_type {
            Type::Button => template.button.as_str(),
            Type::Label => template.label.as_str(),
            Type::Hr => template.hr.as_str(),
            Type::Select => template.select.as_str(),
            Type::Input(ref t) => match t {
                &InputType::Text => template.text.as_str(),
                &InputType::Radio => template.radio.as_str(),
                &InputType::Checkbox => template.checkbox.as_str(),
                &InputType::Textarea => template.textarea.as_str()
            },
            _ => ""
        };

        // No actual template available
        if template.is_empty() {
            return String::new();
        }

        let config = match self.work_type {
            Type::Button => {
                let split: Vec<String> = self.str.split("->").map(|s| s.to_string()).collect();
                to_json(&ButtonConfig {
                    value: split[0].trim().to_owned(),
                    trigger: if split.len() > 1 { split[1].trim().to_owned() } else { "".to_string() }
                })
            },
            Type::Select => to_json(&ManyConfig {value: self.str.split(",").map(|s| s.to_owned()).collect()}),
            _ => to_json(&GeneralConfig {value: self.str.to_string()})
        };

        render_template(template, &config)
    }
}

fn render_template(template: &str, val: &serde_json::value::Value) -> String {
    // Compile template
    let mut handlebars = Handlebars::new();
    assert!(handlebars.register_template_string("tmpl", template)
        .is_ok());

    handlebars.render("tmpl", &val).unwrap()
}

/**
 * An empty line implies a new "row"
 * A line starting with a | implies a columnar form section
 * [value] is an input text field
 * {value} is an input textarea field
 * @ is a radio
 * * is a checkbox
 * --- is an hr
 *
 */
fn main() {
    // Read in standard input
    let mut buffer = String::new();
    io::stdin().read_to_string(&mut buffer)
        .expect("Error reading from stdin");

    // Split input by line
    let v: Vec<&str> = buffer.split("\n").collect();

    // Keep track of rows in output

    let mut pages: Vec<Page> = Vec::new();

    // Check arg for template
    let template = match env::args().nth(1) {
        Some(file) => {
            let mut in_file = match File::open(&file) {
                Err(_) => panic!("Unable to open template file"),
                Ok(file) => file,
            };

            let mut content = String::new();
            in_file.read_to_string(&mut content)
                .expect("Unable to process template file");

            let sp: Vec<&str> = content.split("\n").collect();

            Template::from_vec(&sp)
        },
        _ => match env::var("template") {
            Ok(val) => {
                let tv: Vec<&str> = val.split(",").collect();
                Template::from_vec(&tv)
            },
            _ => Template {
                head: "".to_string(),
                foot: "".to_string(),
                container_start: "
".to_string(),
                container_end: "
".to_string(),
                row_start: "
".to_string(),
                row_end: "
".to_string(),
                col_start: "".to_string(),
                col_end: "".to_string(),
                segment_start: "".to_string(),
                segment_end: "
".to_string(),
                label: "{[value}}".to_string(),
                text: "<input type=\"text\" value=\"{{value}}\"/>".to_string(),
                checkbox: "<input type=\"checkbox\"/>".to_string(),
                radio: "<input type=\"radio\"/>".to_string(),
                textarea: "<textarea>{{value}}</textarea>".to_string(),
                hr: "
".to_string(),
                button: "<button data-trigger=\"{{trigger}}\">{{value}}</button>".to_string(),
                select: "<select>{{#each value}}<option>{{this}}</option>{{/each}}</select>".to_string()
            }
        }
    };

    let mut page: Page = Page::new("default".to_string());

    // Iterate over lines in input
    for line in &v {
        // New row by way of empty line
        if line.chars().count() == 0 {
            page.add_row(Row::new());
            continue;
        }

        let mut form_mode = false; // If user toggled form mode
        let mut col_index: u8 = 1; // Keeping tracking of column index
        let mut working = Working::new('\0', Type::Unknown);

        // Iterate over characters in string
        for (i, c) in line.chars().enumerate() {
            // Match c against known tokens
            match c {
                '-' if i == 0 => {
                    page.get_current_row().get_col(col_index-1).append_str(
                        Working::new('\0', Type::Hr)
                            .compile(&template).as_str()
                        );
                    break;
                },

                // New page
                '#' if i == 0 => {
                    pages.push(page);
                    page = Page::new(line[1..].to_string());
                    break;
                },
                //
                // Controlling form modes
                '|' => {
                    let mut row = page.get_current_row();
                    if i == 0 {
                        form_mode = true;
                        row.get_col(col_index-1).add_buf()
                    } else if form_mode {
                        // Check if we have enough columns in row
                        // if not add a new column
                        if (row.col_count() as u8) < col_index + 1 {
                            row.add_col(Col::new());
                        }

                        // Increment index
                        col_index += 1;

                        row.get_col(col_index-1).add_buf();
                    } else {
                        row.get_col(col_index-1).append(c);
                    }
                },

                // Handle items that have a counterpart
                c if c == '[' && form_mode => working = Working::new(']', Type::Unknown),
                c if c == '{' && form_mode => working = Working::new('}', Type::Label),
                c if c == '(' && form_mode => working = Working::new(')', Type::Button),
                c if c == '<' && form_mode => working = Working::new('>', Type::Select),

                // Handle input type and we are waiting for mode qualifier
                c if c != working.until
                    && working.until != '\0'
                    && working.work_type == Type::Unknown =>
                        match c {
                            'o' => working.work_type = Type::Input(InputType::Radio),
                            '/' => working.work_type = Type::Input(InputType::Checkbox),
                            '+' => working.work_type = Type::Input(InputType::Textarea),
                            c => {
                                working.work_type = Type::Input(
                                    InputType::Text);
                                working.append(c);
                            },
                        },

                // Handle the case we are working towards the end goal of an
                // incoming char
                c if c == working.until => {
                    page.get_current_row().get_col(col_index-1)
                        .append_str(working.compile(&template).as_str());
                    working = Working::new('\0', Type::Unknown);
                },

                // In any other scenario we are building strings
                _ => {
                    if working.is_working() {
                        working.append(c);
                    } else {
                        page.get_current_row().get_col(col_index-1).append(c)
                    }
                }
            }
        }
    }

    pages.push(page);

    print!("{}", &template.head);

    for page in pages {
        print!("{}", page.out(&template));
    }

    println!("{}", &template.foot);
}
<commit_msg>Adding a link support<commit_after>use std::env;
use std::fs::File;
use std::io::{self, Read};
use handlebars::{Handlebars, to_json};

extern crate handlebars;

#[macro_use]
extern crate serde_derive;
extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct ButtonConfig {
    value: String,
    trigger: String
}

#[derive(Serialize, Deserialize, Debug)]
struct ManyConfig {
    value: Vec<String>
}

#[derive(Serialize, Deserialize, Debug)]
struct GeneralConfig {
    value: String
}

struct Col {
    pub buffers: Vec<String>
}
impl Col {
    fn new() -> Col {
        Col { buffers: Vec::new() }
    }
    fn add_buf(&mut self) {
        self.buffers.push(String::new());
    }
    fn append(&mut self, chr: char) {
        let mut len = self.buffers.len();

        if len == 0 {
            self.add_buf();
            len = 1;
        }

        let ref mut buffer = self.buffers[len-1];
        buffer.push(chr);
    }
    fn append_str(&mut self, str: &str) {
        for chr in str.chars() {
            self.append(chr);
        }
    }
    fn out(&self, template: &Template) -> String {
        let mut out: String = String::new();

        out.push_str(template.col_start.as_str());
        for ref buf in self.buffers.iter() {
            out.push_str(format!("{}{}{}",
                    template.segment_start.as_str(),
                    buf,
                    template.segment_end.as_str()
                ).as_str());
        }
        out.push_str(template.col_end.as_str());

        out
    }
}

struct Row {
    cols: Vec<Col>
}
impl Row {
    fn add_col(&mut self, col: Col) {
        self.cols.push(col);
    }
    fn get_col(&mut self, index: u8) -> &mut Col {
        self.cols.get_mut(index as usize).unwrap()
    }
    fn col_count(&self) -> usize {
        self.cols.len()
    }
    fn new() -> Row {
        let mut row: Row = Row { cols: Vec::new() };
        row.add_col(Col::new());
        row
    }
    fn out(&self, template: &Template) -> String {
        let mut out: String = String::new();

        out.push_str("\n");
        out.push_str(template.row_start.as_str());
        for ref col in self.cols.iter() {
            let formatted = format!("{}", col.out(template));
            out.push_str(formatted.as_str());
        }
        out.push_str(template.row_end.as_str());

        out
    }
}

struct Page {
    name: String,
    rows: Vec<Row>
}
impl Page {
    fn new(name: String) -> Page {
        let mut page: Page = Page { name: name, rows: Vec::new() };
        page.add_row(Row::new());
        page
    }
    fn add_row(&mut self, row: Row) {
        self.rows.push(row);
    }
    fn get_current_row(&mut self) -> &mut Row {
        let len = self.rows.len();
        self.rows.get_mut(len-1).unwrap()
    }
    fn out(&self, template: &Template) -> String {
        let mut out: String = String::new();

        let config = to_json(&GeneralConfig {value: self.name.to_string()});
        out.push_str(render_template(template.container_start.as_str(), &config).as_str());

        for ref row in self.rows.iter() {
            let formatted = format!("{}", row.out(template));
            out.push_str(formatted.as_str());
        }

        out.push_str(render_template(template.container_end.as_str(), &config).as_str());

        out
    }
}

struct Template {
    head: String,
    foot: String,
    container_start: String,
    container_end: String,
    row_start: String,
    row_end: String,
    col_start: String,
    col_end: String,
    segment_start: String,
    segment_end: String,
    label: String,
    text: String,
    checkbox: String,
    radio: String,
    textarea: String,
    hr: String,
    button: String,
    a: String,
    select: String
}
impl Template {
    fn from_vec(vec: &Vec<&str>) -> Template {
        Template {
            head: vec[0].to_string().trim().to_string(),
            foot: vec[1].to_string().trim().to_string(),
            container_start: vec[2].to_string().trim().to_string(),
            container_end: vec[3].to_string().trim().to_string(),
            row_start: vec[4].to_string().trim().to_string(),
            row_end: vec[5].to_string().trim().to_string(),
            col_start: vec[6].to_string().trim().to_string(),
            col_end: vec[7].to_string().trim().to_string(),
            segment_start: vec[8].to_string().trim().to_string(),
            segment_end: vec[9].to_string().trim().to_string(),
            label: vec[10].to_string().trim().to_string(),
            text: vec[11].to_string().trim().to_string(),
            checkbox: vec[12].to_string().trim().to_string(),
            radio: vec[13].to_string().trim().to_string(),
            textarea: vec[14].to_string().trim().to_string(),
            button: vec[15].to_string().trim().to_string(),
            a: vec[16].to_string().trim().to_string(),
            select: vec[17].to_string().trim().to_string(),
            hr: vec[18].to_string().trim().to_string()
        }
    }
}

#[derive(PartialEq)]
enum InputType {
    Text,
    Radio,
    Checkbox,
    Textarea
}

#[derive(PartialEq)]
enum ButtonType {
    Unknown,
    Button,
    A
}

#[derive(PartialEq)]
enum Type {
    Unknown,
    Input(InputType),
    Button(ButtonType),
    Label,
    Hr,
    Select
}

struct Working {
    str: String,
    until: char,
    work_type: Type
}
impl Working {
    fn new(until: char, work_type: Type) -> Working {
        Working {
            str: String::new(),
            until: until,
            work_type: work_type
        }
    }
    fn is_working(&self) -> bool {
        self.until != '\0'
    }
    fn append(&mut self, chr: char) {
        self.str.push(chr);
    }
    fn compile(&mut self, template: &Template) -> String {
        // Convert type to template
        let ref template = match self.work_type {
            Type::Button(ref t) => match t {
                &ButtonType::Button => template.button.as_str(),
                &ButtonType::A => template.a.as_str(),
                _ => ""
            },
            Type::Label => template.label.as_str(),
            Type::Hr => template.hr.as_str(),
            Type::Select => template.select.as_str(),
            Type::Input(ref t) => match t {
                &InputType::Text => template.text.as_str(),
                &InputType::Radio => template.radio.as_str(),
                &InputType::Checkbox => template.checkbox.as_str(),
                &InputType::Textarea => template.textarea.as_str()
            },
            _ => ""
        };

        // No actual template available
        if template.is_empty() {
            return String::new();
        }

        let config = match self.work_type {
            Type::Button(_) => {
                let split: Vec<String> = self.str.split("->").map(|s| s.to_string()).collect();
                to_json(&ButtonConfig {
                    value: split[0].trim().to_owned(),
                    trigger: if split.len() > 1 { split[1].trim().to_owned() } else { "".to_string() }
                })
            },
            Type::Select => to_json(&ManyConfig {value: self.str.split(",").map(|s| s.to_owned()).collect()}),
            _ => to_json(&GeneralConfig {value: self.str.to_string()})
        };

        render_template(template, &config)
    }
}

fn render_template(template: &str, val: &serde_json::value::Value) -> String {
    // Compile template
    let mut handlebars = Handlebars::new();
    assert!(handlebars.register_template_string("tmpl", template)
        .is_ok());

    handlebars.render("tmpl", &val).unwrap()
}

/**
 * An empty line implies a new "row"
 * A line starting with a | implies a columnar form section
 * [value] is an input text field
 * {value} is an input textarea field
 * @ is a radio
 * * is a checkbox
 * --- is an hr
 *
 */
fn main() {
    // Read in standard input
    let mut buffer = String::new();
    io::stdin().read_to_string(&mut buffer)
        .expect("Error reading from stdin");

    // Split input by line
    let v: Vec<&str> = buffer.split("\n").collect();

    // Keep track of rows in output

    let mut pages: Vec<Page> = Vec::new();

    // Check arg for template
    let template = match env::args().nth(1) {
        Some(file) => {
            let mut in_file = match File::open(&file) {
                Err(_) => panic!("Unable to open template file"),
                Ok(file) => file,
            };

            let mut content = String::new();
            in_file.read_to_string(&mut content)
                .expect("Unable to process template file");

            let sp: Vec<&str> = content.split("\n").collect();

            Template::from_vec(&sp)
        },
        _ => match env::var("template") {
            Ok(val) => {
                let tv: Vec<&str> = val.split(",").collect();
                Template::from_vec(&tv)
            },
            _ => Template {
                head: "".to_string(),
                foot: "".to_string(),
                container_start: "
".to_string(),
                container_end: "
".to_string(),
                row_start: "
".to_string(),
                row_end: "
".to_string(),
                col_start: "".to_string(),
                col_end: "".to_string(),
                segment_start: "".to_string(),
                segment_end: "
".to_string(),
                label: "{[value}}".to_string(),
                text: "<input type=\"text\" value=\"{{value}}\"/>".to_string(),
                checkbox: "<input type=\"checkbox\"/>".to_string(),
                radio: "<input type=\"radio\"/>".to_string(),
                textarea: "<textarea>{{value}}</textarea>".to_string(),
                hr: "
".to_string(),
                button: "<button data-trigger=\"{{trigger}}\">{{value}}</button>".to_string(),
                a: "{{value}}".to_string(),
                select: "<select>{{#each value}}<option>{{this}}</option>{{/each}}</select>".to_string()
            }
        }
    };

    let mut page: Page = Page::new("default".to_string());

    // Iterate over lines in input
    for line in &v {
        // New row by way of empty line
        if line.chars().count() == 0 {
            page.add_row(Row::new());
            continue;
        }

        let mut form_mode = false; // If user toggled form mode
        let mut col_index: u8 = 1; // Keeping tracking of column index
        let mut working = Working::new('\0', Type::Unknown);

        // Iterate over characters in string
        for (i, c) in line.chars().enumerate() {
            // Match c against known tokens
            match c {
                '-' if i == 0 => {
                    page.get_current_row().get_col(col_index-1).append_str(
                        Working::new('\0', Type::Hr)
                            .compile(&template).as_str()
                        );
                    break;
                },

                // New page
                '#' if i == 0 => {
                    pages.push(page);
                    page = Page::new(line[1..].to_string());
                    break;
                },
                //
                // Controlling form modes
                '|' => {
                    let mut row = page.get_current_row();
                    if i == 0 {
                        form_mode = true;
                        row.get_col(col_index-1).add_buf()
                    } else if form_mode {
                        // Check if we have enough columns in row
                        // if not add a new column
                        if (row.col_count() as u8) < col_index + 1 {
                            row.add_col(Col::new());
                        }

                        // Increment index
                        col_index += 1;

                        row.get_col(col_index-1).add_buf();
                    } else {
                        row.get_col(col_index-1).append(c);
                    }
                },

                // Handle items that have a counterpart
                c if c == '[' && form_mode => working = Working::new(']', Type::Unknown),
                c if c == '{' && form_mode => working = Working::new('}', Type::Label),
                c if c == '(' && form_mode => working = Working::new(')', Type::Button(ButtonType::Unknown)),
                c if c == '<' && form_mode => working = Working::new('>', Type::Select),

                // Handle button type and we are waiting for mode qualifier
                c if c != working.until
                    && working.until != '\0'
                    && working.work_type == Type::Button(ButtonType::Unknown) =>
                        match c {
                            '#' => working.work_type = Type::Button(ButtonType::A),
                            c => {
                                working.work_type = Type::Button(ButtonType::Button);
                                working.append(c);
                            },
                        },

                // Handle input type and we are waiting for mode qualifier
                c if c != working.until
                    && working.until != '\0'
                    && working.work_type == Type::Unknown =>
                        match c {
                            'o' => working.work_type = Type::Input(InputType::Radio),
                            '/' => working.work_type = Type::Input(InputType::Checkbox),
                            '+' => working.work_type = Type::Input(InputType::Textarea),
                            c => {
                                working.work_type = Type::Input(
                                    InputType::Text);
                                working.append(c);
                            },
                        },

                // Handle the case we are working towards the end goal of an
                // incoming char
                c if c == working.until => {
                    page.get_current_row().get_col(col_index-1)
                        .append_str(working.compile(&template).as_str());
                    working = Working::new('\0', Type::Unknown);
                },

                // In any other scenario we are building strings
                _ => {
                    if working.is_working() {
                        working.append(c);
                    } else {
                        page.get_current_row().get_col(col_index-1).append(c)
                    }
                }
            }
        }
    }

    pages.push(page);

    print!("{}", &template.head);

    for page in pages {
        print!("{}", page.out(&template));
    }

    println!("{}", &template.foot);
}
<|endoftext|>


<commit_before>#![feature(box_syntax)]
#![feature(unsafe_destructor)]
#![feature(collections)]
#![feature(core)]
#![feature(path)]
#![feature(env)]
#![feature(io)]
#![feature(libc)]
#![feature(std_misc)]
#![feature(unicode)]
#![feature(rustc_private)]
#![feature(plugin)]
#![feature(hash)]
#![plugin(regex_macros)]
extern crate sodiumoxide;
extern crate libc;
extern crate serialize;
extern crate unicode;
extern crate regex;
extern crate core;
#[no_link]
extern crate regex_macros;

use reader::*;
use constants::*;
use builtins::*;
use types::*;
use ast::*;
use handlers::*;

use types::InputValue::*;

mod constants;
#[macro_use]
mod util;
mod termios;
mod signal;
mod controls;
mod input;
mod reader;
mod script;
mod builtins;
mod command;
mod types;
mod env;
mod ioctl;
mod ast;
mod handlers;

// public so no warnings when we run tests
pub fn main() {
    let mut reader = LineReader::new();
    let mut ast = AST::new();
    let mut cleaned_jobs;
    match load_builtins(&mut ast.env) {
        Err(e) => ast.env.errf(format_args!("Could not load builtings: {}\n", e)),
        _ => {}
    }
    load_handlers(&mut ast);
    ast.env.update_terminal();
    loop {
        ast.env.flush();
        cleaned_jobs = ast.env.clean_jobs();
        match cleaned_jobs {
            WashArgs::Long(v) => {
                for status in v.iter() {
                    ast.env.outf(format_args!("{}\n", status.flatten()));
                }
            },
            _ => {/* nothing */}
        }
        if ast.in_block() {
            match ast.env.runf(&format!("subprompt"), &WashArgs::Empty) {
                Err(_) => reader.controls.outs("prompt failed => run("),
                Ok(v) => reader.controls.outs(v.flatten().as_slice())
            }
        } else {
            match ast.env.runf(&format!("prompt"), &WashArgs::Empty) {
                Err(_) => reader.controls.outs("prompt failed => run("),
                Ok(v) => reader.controls.outs(v.flatten().as_slice())
            }
        }
        match reader.read_line() {
            None => {
                if reader.eof {
                    break;
                } else if !reader.line.is_empty() {
                    ast.env.outc(BEL);
                    reader.restart();
                } else {
                    ast.env.outc(NL);
                    reader.clear();
                }
            },
            Some(mut line) => {
                ast.env.outc(NL);
                match ast.add_line(&mut line) {
                    Err(ref e) if *e == STOP => {
                        // the silent error
                        ast.clear();
                    },
                    Err(e) => {
                        println!("Error: {}", e);
                        ast.clear();
                    },
                    Ok(_) => {
                        if !ast.in_block() {
                            match ast.evaluate() {
                                Err(ref e) if *e == STOP => {
                                    // the silent error
                                }
                                Err(e) => {
                                    println!("Error: {}", e);
                                },
                                Ok(v) => {
                                    println!("{}", v.flatten());
                                }
                            };
                            ast.clear();
                        }
                    }
                }
                reader.clear();
            }
        }
    }
    ast.env.outs("\nExiting\n");
    ast.env.flush();
}
<commit_msg>Don't print newline on evaluate return Empty<commit_after>#![feature(box_syntax)]
#![feature(unsafe_destructor)]
#![feature(collections)]
#![feature(core)]
#![feature(path)]
#![feature(env)]
#![feature(io)]
#![feature(libc)]
#![feature(std_misc)]
#![feature(unicode)]
#![feature(rustc_private)]
#![feature(plugin)]
#![feature(hash)]
#![plugin(regex_macros)]
extern crate sodiumoxide;
extern crate libc;
extern crate serialize;
extern crate unicode;
extern crate regex;
extern crate core;
#[no_link]
extern crate regex_macros;

use reader::*;
use constants::*;
use builtins::*;
use types::*;
use ast::*;
use handlers::*;

use types::InputValue::*;

mod constants;
#[macro_use]
mod util;
mod termios;
mod signal;
mod controls;
mod input;
mod reader;
mod script;
mod builtins;
mod command;
mod types;
mod env;
mod ioctl;
mod ast;
mod handlers;

// public so no warnings when we run tests
pub fn main() {
    let mut reader = LineReader::new();
    let mut ast = AST::new();
    let mut cleaned_jobs;
    match load_builtins(&mut ast.env) {
        Err(e) => ast.env.errf(format_args!("Could not load builtings: {}\n", e)),
        _ => {}
    }
    load_handlers(&mut ast);
    ast.env.update_terminal();
    loop {
        ast.env.flush();
        cleaned_jobs = ast.env.clean_jobs();
        match cleaned_jobs {
            WashArgs::Long(v) => {
                for status in v.iter() {
                    ast.env.outf(format_args!("{}\n", status.flatten()));
                }
            },
            _ => {/* nothing */}
        }
        if ast.in_block() {
            match ast.env.runf(&format!("subprompt"), &WashArgs::Empty) {
                Err(_) => reader.controls.outs("prompt failed => run("),
                Ok(v) => reader.controls.outs(v.flatten().as_slice())
            }
        } else {
            match ast.env.runf(&format!("prompt"), &WashArgs::Empty) {
                Err(_) => reader.controls.outs("prompt failed => run("),
                Ok(v) => reader.controls.outs(v.flatten().as_slice())
            }
        }
        match reader.read_line() {
            None => {
                if reader.eof {
                    break;
                } else if !reader.line.is_empty() {
                    ast.env.outc(BEL);
                    reader.restart();
                } else {
                    ast.env.outc(NL);
                    reader.clear();
                }
            },
            Some(mut line) => {
                ast.env.outc(NL);
                match ast.add_line(&mut line) {
                    Err(ref e) if *e == STOP => {
                        // the silent error
                        ast.clear();
                    },
                    Err(e) => {
                        println!("Error: {}", e);
                        ast.clear();
                    },
                    Ok(_) => {
                        if !ast.in_block() {
                            match ast.evaluate() {
                                Err(ref e) if *e == STOP => {
                                    // the silent error
                                }
                                Err(e) => {
                                    println!("Error: {}", e);
                                },
                                Ok(WashArgs::Empty) => {
                                    // print nothing
                                },
                                Ok(v) => {
                                    println!("{}", v.flatten());
                                }
                            };
                            ast.clear();
                        }
                    }
                }
                reader.clear();
            }
        }
    }
    ast.env.outs("\nExiting\n");
    ast.env.flush();
}
<|endoftext|>


<commit_before>extern crate rand;

use std::string::String;
use std::collections::HashMap;
use std::vec::Vec;
use std::fs::{File};
use std::env;
use std::path::{Path};
use std::io::{self, BufReader, BufRead};
use std::iter;

use rand::{Closed01, Rng};
use rand::distributions::{IndependentSample, Sample, Gamma, LogNormal, RandSample, Range};

type Bag = HashMap<usize, usize>;

fn load_bags<P: AsRef<Path>>(path: P) -> io::Result<(Vec<usize>, Vec<Bag>, usize)> {
    let mut bags = Vec::new();
    let mut labels = Vec::new();
    let mut vocab_size = 0;
    let file = try!(File::open(path));
    let file = BufReader::new(file);
    for line in file.lines() {
        let line = line.unwrap();
        let mut iter = line.split_whitespace();
        let label = iter.next().unwrap().parse::<usize>().unwrap();
        let mut bag = Bag::new();
        for elm in iter {
            let mut iter = elm.split(':');
            let index = iter.next().unwrap().parse::<usize>().unwrap();
            let value = iter.next().unwrap().parse::<usize>().unwrap();
            bag.insert(index - 1, value);    // We assume one-based indexing
            if vocab_size < index {
                vocab_size = index;
            }
        }
        labels.push(label);
        bags.push(bag);
    }
    Ok((labels, bags, vocab_size))
}

fn load_text_vocabulary<P: AsRef<Path>>(path: P) -> io::Result<Vec<String>> {
    let mut vocab = Vec::new();
    let file = try!(File::open(path));
    let file = BufReader::new(file);
    for line in file.lines() {
        let line = line.unwrap();
        vocab.push(line);
    }
    Ok(vocab)
}

#[derive(Clone)]
struct Categorical {
    prop: Vec<f64>,
}

impl Categorical {
    fn new(weights: Vec<f64>) -> Categorical {
        let sum: f64 = weights.iter().sum();
        let prop: Vec<f64> = weights.iter().map(|w| w / sum).collect();
        Categorical {
            prop: prop,
        }
    }
}

impl Sample<usize> for Categorical {
    fn sample<R: Rng>(&mut self, rng: &mut R) -> usize {
        self.ind_sample(rng)
    }
}

impl IndependentSample<usize> for Categorical {
    fn ind_sample<R: Rng>(&self, rng: &mut R) -> usize {
        let closed01 = RandSample::<Closed01<f64>>::new();
        let Closed01(x) = closed01.ind_sample(rng);

        let mut sum = 0.0;
        let mut ret = self.prop.len() - 1;
        for (k, &p) in self.prop.iter().enumerate() {
            sum += p;
            if x <= sum {
                ret = k;
                break;
            }
        }
        ret
    }
}

#[derive(Clone)]
struct Dirichlet {
    alpha: Vec<f64>,
}

impl Dirichlet {
    fn new(alpha: Vec<f64>) -> Dirichlet {
        Dirichlet {
            alpha: alpha,
        }
    }
}

impl Sample<Vec<f64>> for Dirichlet {
    fn sample<R: Rng>(&mut self, rng: &mut R) -> Vec<f64> {
        self.ind_sample(rng)
    }
}

impl IndependentSample<Vec<f64>> for Dirichlet {
    fn ind_sample<R: Rng>(&self, rng: &mut R) -> Vec<f64> {
        let mut sum = 0.0;
        let mut xs = Vec::new();
        for &a in &self.alpha {
            let gamma = Gamma::new(a, 1.0);
            let y = gamma.ind_sample(rng);
            xs.push(y);
            sum += y;
        }
        for x in xs.iter_mut() {
            *x /= sum;
        }
        xs
    }
}

fn lda(dataset: Vec<Bag>, alpha: Vec<f64>, beta: Vec<f64>, burn_in: usize, num_samples: usize) {
    let num_docs: usize = dataset.len();
    let num_topics: usize = alpha.len();
    let vocab_size: usize = beta.len();
    let mut rng = rand::thread_rng();
    println!("K = {}", num_topics);
    println!("M = {}", num_docs);
    println!("V = {}", vocab_size);

    // Construct zero-filled nested arrays
    let mut w:     Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut z:     Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut theta: Vec<Vec<f64>>   = Vec::with_capacity(num_docs);
    let mut phi:   Vec<Vec<f64>>   = Vec::with_capacity(num_topics);
    let mut ndk:   Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut nkv:   Vec<Vec<usize>> = Vec::with_capacity(num_topics);
    let mut z_samples: Vec<Vec<Vec<usize>>> = Vec::with_capacity(num_docs);
    for bag in &dataset {
        // n_d
        let mut n_d = 0;
        for &count in bag.values() {
            n_d += count;
        }
        w.push(vec![0; n_d]);
        z.push(vec![0; n_d]);
        theta.push(vec![0.0; num_topics]);
        ndk.push(vec![0; num_topics]);
        z_samples.push(vec![vec![0; num_topics]; n_d]);
    }
    for k in 0..num_topics {
        phi.push(vec![0.0; vocab_size]);
        nkv.push(vec![0; vocab_size]);
    }

    // Initialize w, z, ndk and nkv
    let among_topics = Range::new(0, num_topics);
    for (d, bag) in dataset.iter().enumerate() {
        let mut i = 0;
        for (&v, &count) in bag {
            for _ in 0..count {
                w[d][i] = v;
                let k = among_topics.ind_sample(&mut rng);
                z[d][i] = k;
                ndk[d][k] += 1;
                nkv[k][v] += 1;
                i += 1;
            }
        }
    }
    // Initialize theta, phi
    for d in 0..num_docs {
        let dir = Dirichlet::new(alpha.clone());
        theta[d] = dir.ind_sample(&mut rng);
    }
    for k in 0..num_topics {
        let dir = Dirichlet::new(beta.clone());
        phi[k] = dir.ind_sample(&mut rng);
    }

    println!("z = {:?}", z);
    println!("theta = {:?}", theta);
    println!("ndk = {:?}", ndk);
    println!("phi = {:?}", phi);
    println!("nkv = {:?}", nkv);

    // Sampling
    for s in 0..(burn_in + num_samples) {
        for (d, w_d) in w.iter().enumerate() {
            for (i, &w_di) in w_d.iter().enumerate() {
                let v = w_di;
                // Sample z_di
                let mut weights: Vec<f64> = Vec::with_capacity(num_topics);
                for k in 0..num_topics {
                    weights.push(theta[d][k] * phi[k][v]);
                }
                let cat = Categorical::new(weights);
                let old_z_di = z[d][i];
                let new_z_di = cat.ind_sample(&mut rng);
                z[d][i] = new_z_di;
                if s >= burn_in {
                    z_samples[d][i][new_z_di] += 1;
                }
                // Update ndk and nkv
                ndk[d][old_z_di] -= 1;
                ndk[d][new_z_di] += 1;
                nkv[old_z_di][v] -= 1;
                nkv[new_z_di][v] += 1;
            }
            // Sample theta_d
            let mut alpha_d: Vec<f64> = Vec::with_capacity(num_topics);
            for k in 0..num_topics {
                alpha_d.push(ndk[d][k] as f64 + alpha[k]);
            }
            let dir = Dirichlet::new(alpha_d);
            theta[d] = dir.ind_sample(&mut rng);
        }
        for k in 0..num_topics {
            // Sample phi_k
            let mut beta_k: Vec<f64> = Vec::with_capacity(vocab_size);
            for v in 0..vocab_size {
                beta_k.push(nkv[k][v] as f64 + beta[v]);
            }
            let dir = Dirichlet::new(beta_k);
            phi[k] = dir.ind_sample(&mut rng);
        }
        // Evaluate the log-likelihood value for the current parameters
        let mut log_likelihood = 0.0;
        for k in 0..num_topics {
            for v in 0..vocab_size {
                log_likelihood += nkv[k][v] as f64 * f64::ln(phi[k][v]);
            }
        }
        println!("log_likelihood = {}", log_likelihood);
    }
    // Distribution of z
    for (d, z_samples_d) in z_samples.iter().enumerate() {
        for (i, samples) in z_samples_d.iter().enumerate() {
            let mut sum = 0.0;
            for k in 0..num_topics {
                sum += samples[k] as f64;
            }
            print!("z[{:3}][{:3}] = ", d, i);
            for k in 0..num_topics {
                print!("{:3}:{:.2} ", k, samples[k] as f64 / sum);
            }
            println!("");
        }
    }
}

fn make_dataset(num_docs: usize, mean_nd: f64, std_dev_nd: f64, alpha: Vec<f64>, beta: Vec<f64>) -> Vec<Bag> {
    let num_topics: usize = alpha.len();
    let vocab_size: usize = beta.len();
    let mut rng = rand::thread_rng();
    // phi
    let dir_beta = Dirichlet::new(beta);
    let mut phi: Vec<Vec<f64>> = Vec::with_capacity(num_topics);
    for k in 0..num_topics {
        // Sample phi_k
        phi.push(dir_beta.ind_sample(&mut rng));
    }
    // theta, nd, z, w
    let dir_alpha = Dirichlet::new(alpha);
    let lognorm = LogNormal::new(mean_nd, std_dev_nd);
    let mut theta: Vec<Vec<f64>> = Vec::with_capacity(num_docs);
    let mut nd: Vec<usize> = Vec::with_capacity(num_docs);
    let mut z: Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut w: Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    for d in 0..num_docs {
        theta.push(dir_alpha.ind_sample(&mut rng));
        nd.push(f64::floor(lognorm.ind_sample(&mut rng)) as usize);
        let cat_theta = Categorical::new(theta[d].clone());
        let mut z_d = Vec::with_capacity(nd[d]);
        let mut w_d = Vec::with_capacity(nd[d]);
        for i in 0..nd[d] {
            z_d.push(cat_theta.ind_sample(&mut rng));
            let cat_phi = Categorical::new(phi[z_d[i]].clone());
            w_d.push(cat_phi.ind_sample(&mut rng));
        }
        z.push(z_d);
        w.push(w_d);
    }
    // Make bags
    let mut bags: Vec<Bag> = Vec::new();
    for w_d in w {
        let mut bag: Bag = Bag::new();
        for v in w_d {
            let counter = bag.entry(v).or_insert(0);
            *counter += 1;
        }
        bags.push(bag);
    }
    bags
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let (labels, dataset, vocab_size) = load_bags(&args[1]).unwrap();
    let vocab: Option<Vec<String>> = if args.len() < 3 {
        None
    }
    else {
        Some(load_text_vocabulary(&args[2]).unwrap())
    };
    println!("{:?}", labels);
    println!("{:?}", dataset);
    println!("{:?}", vocab);

    let num_topics = 3;
    let alpha: Vec<f64> = iter::repeat(1.0).take(num_topics).collect();
    let beta: Vec<f64> = iter::repeat(1.0).take(vocab_size).collect();

    lda(dataset, alpha, beta, 1000, 10000);
}
<commit_msg>Print progress information to stderr<commit_after>extern crate rand;

use std::string::String;
use std::collections::HashMap;
use std::vec::Vec;
use std::fs::{File};
use std::env;
use std::path::{Path};
use std::io::{self, BufReader, BufRead, Write};
use std::iter;

use rand::{Closed01, Rng};
use rand::distributions::{IndependentSample, Sample, Gamma, LogNormal, RandSample, Range};

type Bag = HashMap<usize, usize>;

fn load_bags<P: AsRef<Path>>(path: P) -> io::Result<(Vec<usize>, Vec<Bag>, usize)> {
    let mut bags = Vec::new();
    let mut labels = Vec::new();
    let mut vocab_size = 0;
    let file = try!(File::open(path));
    let file = BufReader::new(file);
    for line in file.lines() {
        let line = line.unwrap();
        let mut iter = line.split_whitespace();
        let label = iter.next().unwrap().parse::<usize>().unwrap();
        let mut bag = Bag::new();
        for elm in iter {
            let mut iter = elm.split(':');
            let index = iter.next().unwrap().parse::<usize>().unwrap();
            let value = iter.next().unwrap().parse::<usize>().unwrap();
            bag.insert(index - 1, value);    // We assume one-based indexing
            if vocab_size < index {
                vocab_size = index;
            }
        }
        labels.push(label);
        bags.push(bag);
    }
    Ok((labels, bags, vocab_size))
}

fn load_text_vocabulary<P: AsRef<Path>>(path: P) -> io::Result<Vec<String>> {
    let mut vocab = Vec::new();
    let file = try!(File::open(path));
    let file = BufReader::new(file);
    for line in file.lines() {
        let line = line.unwrap();
        vocab.push(line);
    }
    Ok(vocab)
}

#[derive(Clone)]
struct Categorical {
    prop: Vec<f64>,
}

impl Categorical {
    fn new(weights: Vec<f64>) -> Categorical {
        let sum: f64 = weights.iter().sum();
        let prop: Vec<f64> = weights.iter().map(|w| w / sum).collect();
        Categorical {
            prop: prop,
        }
    }
}

impl Sample<usize> for Categorical {
    fn sample<R: Rng>(&mut self, rng: &mut R) -> usize {
        self.ind_sample(rng)
    }
}

impl IndependentSample<usize> for Categorical {
    fn ind_sample<R: Rng>(&self, rng: &mut R) -> usize {
        let closed01 = RandSample::<Closed01<f64>>::new();
        let Closed01(x) = closed01.ind_sample(rng);

        let mut sum = 0.0;
        let mut ret = self.prop.len() - 1;
        for (k, &p) in self.prop.iter().enumerate() {
            sum += p;
            if x <= sum {
                ret = k;
                break;
            }
        }
        ret
    }
}

#[derive(Clone)]
struct Dirichlet {
    alpha: Vec<f64>,
}

impl Dirichlet {
    fn new(alpha: Vec<f64>) -> Dirichlet {
        Dirichlet {
            alpha: alpha,
        }
    }
}

impl Sample<Vec<f64>> for Dirichlet {
    fn sample<R: Rng>(&mut self, rng: &mut R) -> Vec<f64> {
        self.ind_sample(rng)
    }
}

impl IndependentSample<Vec<f64>> for Dirichlet {
    fn ind_sample<R: Rng>(&self, rng: &mut R) -> Vec<f64> {
        let mut sum = 0.0;
        let mut xs = Vec::new();
        for &a in &self.alpha {
            let gamma = Gamma::new(a, 1.0);
            let y = gamma.ind_sample(rng);
            xs.push(y);
            sum += y;
        }
        for x in xs.iter_mut() {
            *x /= sum;
        }
        xs
    }
}

fn lda(dataset: Vec<Bag>, alpha: Vec<f64>, beta: Vec<f64>, burn_in: usize, num_samples: usize) {
    let num_docs: usize = dataset.len();
    let num_topics: usize = alpha.len();
    let vocab_size: usize = beta.len();
    let mut rng = rand::thread_rng();
    println!("K = {}", num_topics);
    println!("M = {}", num_docs);
    println!("V = {}", vocab_size);

    write!(&mut std::io::stderr(), "Initializing...");

    // Construct zero-filled nested arrays
    let mut w:     Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut z:     Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut theta: Vec<Vec<f64>>   = Vec::with_capacity(num_docs);
    let mut phi:   Vec<Vec<f64>>   = Vec::with_capacity(num_topics);
    let mut ndk:   Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut nkv:   Vec<Vec<usize>> = Vec::with_capacity(num_topics);
    let mut z_samples: Vec<Vec<Vec<usize>>> = Vec::with_capacity(num_docs);
    for bag in &dataset {
        // n_d
        let mut n_d = 0;
        for &count in bag.values() {
            n_d += count;
        }
        w.push(vec![0; n_d]);
        z.push(vec![0; n_d]);
        theta.push(vec![0.0; num_topics]);
        ndk.push(vec![0; num_topics]);
        z_samples.push(vec![vec![0; num_topics]; n_d]);
    }
    for k in 0..num_topics {
        phi.push(vec![0.0; vocab_size]);
        nkv.push(vec![0; vocab_size]);
    }

    // Initialize w, z, ndk and nkv
    let among_topics = Range::new(0, num_topics);
    for (d, bag) in dataset.iter().enumerate() {
        let mut i = 0;
        for (&v, &count) in bag {
            for _ in 0..count {
                w[d][i] = v;
                let k = among_topics.ind_sample(&mut rng);
                z[d][i] = k;
                ndk[d][k] += 1;
                nkv[k][v] += 1;
                i += 1;
            }
        }
    }
    // Initialize theta, phi
    for d in 0..num_docs {
        let dir = Dirichlet::new(alpha.clone());
        theta[d] = dir.ind_sample(&mut rng);
    }
    for k in 0..num_topics {
        let dir = Dirichlet::new(beta.clone());
        phi[k] = dir.ind_sample(&mut rng);
    }

    println!("z = {:?}", z);
    println!("theta = {:?}", theta);
    println!("ndk = {:?}", ndk);
    println!("phi = {:?}", phi);
    println!("nkv = {:?}", nkv);

    writeln!(&mut std::io::stderr(), "\rInitialized.");

    // Sampling
    write!(&mut std::io::stderr(), "Sampling...");
    for s in 0..(burn_in + num_samples) {
        for (d, w_d) in w.iter().enumerate() {
            for (i, &w_di) in w_d.iter().enumerate() {
                let v = w_di;
                // Sample z_di
                let mut weights: Vec<f64> = Vec::with_capacity(num_topics);
                for k in 0..num_topics {
                    weights.push(theta[d][k] * phi[k][v]);
                }
                let cat = Categorical::new(weights);
                let old_z_di = z[d][i];
                let new_z_di = cat.ind_sample(&mut rng);
                z[d][i] = new_z_di;
                if s >= burn_in {
                    z_samples[d][i][new_z_di] += 1;
                }
                // Update ndk and nkv
                ndk[d][old_z_di] -= 1;
                ndk[d][new_z_di] += 1;
                nkv[old_z_di][v] -= 1;
                nkv[new_z_di][v] += 1;
            }
            // Sample theta_d
            let mut alpha_d: Vec<f64> = Vec::with_capacity(num_topics);
            for k in 0..num_topics {
                alpha_d.push(ndk[d][k] as f64 + alpha[k]);
            }
            let dir = Dirichlet::new(alpha_d);
            theta[d] = dir.ind_sample(&mut rng);
        }
        for k in 0..num_topics {
            // Sample phi_k
            let mut beta_k: Vec<f64> = Vec::with_capacity(vocab_size);
            for v in 0..vocab_size {
                beta_k.push(nkv[k][v] as f64 + beta[v]);
            }
            let dir = Dirichlet::new(beta_k);
            phi[k] = dir.ind_sample(&mut rng);
        }
        // Evaluate the log-likelihood value for the current parameters
        let mut log_likelihood = 0.0;
        for k in 0..num_topics {
            for v in 0..vocab_size {
                log_likelihood += nkv[k][v] as f64 * f64::ln(phi[k][v]);
            }
        }
        println!("log_likelihood = {}", log_likelihood);
        write!(&mut std::io::stderr(), "\rSampling... {}/{}", s, num_samples);
    }
    writeln!(&mut std::io::stderr(), "\rSampled.");

    // Distribution of z
    for (d, z_samples_d) in z_samples.iter().enumerate() {
        for (i, samples) in z_samples_d.iter().enumerate() {
            let mut sum = 0.0;
            for k in 0..num_topics {
                sum += samples[k] as f64;
            }
            print!("z[{:3}][{:3}] = ", d, i);
            for k in 0..num_topics {
                print!("{:3}:{:.2} ", k, samples[k] as f64 / sum);
            }
            println!("");
        }
    }
}

fn make_dataset(num_docs: usize, mean_nd: f64, std_dev_nd: f64, alpha: Vec<f64>, beta: Vec<f64>) -> Vec<Bag> {
    let num_topics: usize = alpha.len();
    let vocab_size: usize = beta.len();
    let mut rng = rand::thread_rng();
    // phi
    let dir_beta = Dirichlet::new(beta);
    let mut phi: Vec<Vec<f64>> = Vec::with_capacity(num_topics);
    for k in 0..num_topics {
        // Sample phi_k
        phi.push(dir_beta.ind_sample(&mut rng));
    }
    // theta, nd, z, w
    let dir_alpha = Dirichlet::new(alpha);
    let lognorm = LogNormal::new(mean_nd, std_dev_nd);
    let mut theta: Vec<Vec<f64>> = Vec::with_capacity(num_docs);
    let mut nd: Vec<usize> = Vec::with_capacity(num_docs);
    let mut z: Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    let mut w: Vec<Vec<usize>> = Vec::with_capacity(num_docs);
    for d in 0..num_docs {
        theta.push(dir_alpha.ind_sample(&mut rng));
        nd.push(f64::floor(lognorm.ind_sample(&mut rng)) as usize);
        let cat_theta = Categorical::new(theta[d].clone());
        let mut z_d = Vec::with_capacity(nd[d]);
        let mut w_d = Vec::with_capacity(nd[d]);
        for i in 0..nd[d] {
            z_d.push(cat_theta.ind_sample(&mut rng));
            let cat_phi = Categorical::new(phi[z_d[i]].clone());
            w_d.push(cat_phi.ind_sample(&mut rng));
        }
        z.push(z_d);
        w.push(w_d);
    }
    // Make bags
    let mut bags: Vec<Bag> = Vec::new();
    for w_d in w {
        let mut bag: Bag = Bag::new();
        for v in w_d {
            let counter = bag.entry(v).or_insert(0);
            *counter += 1;
        }
        bags.push(bag);
    }
    bags
}

fn main() {
    let args: Vec<String> = env::args().collect();
    let (labels, dataset, vocab_size) = load_bags(&args[1]).unwrap();
    let vocab: Option<Vec<String>> = if args.len() < 3 {
        None
    }
    else {
        Some(load_text_vocabulary(&args[2]).unwrap())
    };
    println!("{:?}", labels);
    println!("{:?}", dataset);
    println!("{:?}", vocab);

    let num_topics = 3;
    let alpha: Vec<f64> = iter::repeat(1.0).take(num_topics).collect();
    let beta: Vec<f64> = iter::repeat(1.0).take(vocab_size).collect();

    lda(dataset, alpha, beta, 1000, 10000);
}
<|endoftext|>


<commit_before>#[macro_use]
extern crate bmp;
extern crate num;

use num::complex::*;
use bmp::{Image, Pixel};

const SIZE: u32 = 256;
const ESCAPE_LIMIT: f64 = 1000000.0;
const ITER_LIMIT: u32 = 200;
const WIDTH: f64 = 0.005;
const HEIGHT: f64 = 0.010;
const X_CENTER: f64 = 1.941;
const Y_CENTER: f64 = 0.004;

// Burning ship
#[inline]
fn burning_ship() -> Box<Fn(Complex64, Complex64) -> Complex64>
{
    return Box::new(move |z: Complex64, c: Complex64| {
        let temp = Complex{re: z.re.abs(), im: z.im.abs()};
        return (temp * temp) + c;
    });
}

// Mendelbrot
fn mandelbrot() -> Box<Fn(Complex64, Complex64) -> Complex64>
{
    return Box::new(move |z: Complex64, c: Complex64| z*z + c);
}

fn run_until_escape(gen_fn: Box<Fn(Complex64, Complex64) -> Complex64>,
    c: Complex64, escape_limit: f64, iter_limit: u32) -> u32
{
    let mut z: Complex64 = Complex64{ re: 0.0, im: 0.0};
    let mut n: u32 = 0;
    while z.norm_sqr() < escape_limit && n < iter_limit {
        n += 1;
        z = (*gen_fn)(z, c);
    }

    return n;
}

fn main() {
    let mut pixels = vec![0; (SIZE*SIZE) as usize];

    let coordinates: Vec<(u32, u32)> = (0..SIZE).flat_map(move |x| (0..SIZE).map(move |y| (x,y))).collect();
    for &(x, y) in &coordinates {
        let float_x: f64 = WIDTH*((x as f64)/(SIZE as f64) - 0.5) - X_CENTER;
        let float_y: f64 = HEIGHT*((y as f64)/(SIZE as f64) - 0.5) - Y_CENTER;
        let iterations = run_until_escape(burning_ship(), Complex{re: float_x, im: float_y},
                                          ESCAPE_LIMIT, ITER_LIMIT);
        pixels[(SIZE*x + y) as usize] = iterations;
    }

    let max = pixels.iter().cloned().filter(|&x| x < ITER_LIMIT).max().unwrap();
    println!("max pixel: {}", max);

    for &(x, y) in &coordinates {
            let current = pixels[(SIZE*x + y) as usize];
            if current < ITER_LIMIT {
                pixels[(SIZE*x + y) as usize] *= 256;
                pixels[(SIZE*x + y) as usize] /= max;
            }
            else {
                pixels[(SIZE*x + y) as usize] = 256;
            }
    }

    let mut img = Image::new(SIZE, SIZE);
    for (x,y) in img.coordinates()
    {
        let val = pixels[(SIZE*x + y) as usize];
        img.set_pixel(x, y, px!(val, val, val));
    }

    let _ = img.save("test.bmp");
}
<commit_msg>Started to break code out into functions<commit_after>#[macro_use]
extern crate bmp;
extern crate num;

use num::complex::*;
use bmp::{Image, Pixel};

const SIZE: u32 = 1024;
const ESCAPE_LIMIT: f64 = 1000000.0;
const ITER_LIMIT: u32 = 200;
const WIDTH: f64 = 0.005;
const HEIGHT: f64 = 0.010;
const X_CENTER: f64 = 1.941;
const Y_CENTER: f64 = 0.004;

// Burning ship
#[inline]
fn burning_ship() -> Box<Fn(Complex64, Complex64) -> Complex64>
{
    return Box::new(move |z: Complex64, c: Complex64| {
        let temp = Complex{re: z.re.abs(), im: z.im.abs()};
        return (temp * temp) + c;
    });
}

// Mendelbrot
fn mandelbrot() -> Box<Fn(Complex64, Complex64) -> Complex64>
{
    return Box::new(move |z: Complex64, c: Complex64| z*z + c);
}

fn run_until_escape(gen_fn: &Box<Fn(Complex64, Complex64) -> Complex64>,
    c: Complex64, escape_limit: f64, iter_limit: u32) -> u32
{
    let mut z: Complex64 = Complex64{ re: 0.0, im: 0.0};
    let mut n: u32 = 0;
    while z.norm_sqr() < escape_limit && n < iter_limit {
        n += 1;
        z = (**gen_fn)(z, c);
    }

    return n;
}
fn create_bmp(pixels: Vec<u32>) {
    let mut img = Image::new(SIZE, SIZE);
    for (x,y) in img.coordinates()
    {
        let val = pixels[(SIZE*x + y) as usize];
        img.set_pixel(x, y, px!(val, val, val));
    }

    let _ = img.save("test.bmp");
}

fn run_fractal(gen_fn: Box<Fn(Complex64, Complex64) -> Complex64>) {
    let mut pixels = vec![0; (SIZE*SIZE) as usize];

    // Run fractal generation
    let coordinates: Vec<(u32, u32)> = (0..SIZE).flat_map(move |x| (0..SIZE).map(move |y| (x,y))).collect();
    for &(x, y) in &coordinates {
        let float_x: f64 = WIDTH*((x as f64)/(SIZE as f64) - 0.5) - X_CENTER;
        let float_y: f64 = HEIGHT*((y as f64)/(SIZE as f64) - 0.5) - Y_CENTER;
        let iterations = run_until_escape(&gen_fn, Complex{re: float_x, im: float_y},
                                          ESCAPE_LIMIT, ITER_LIMIT);
        pixels[(SIZE*x + y) as usize] = iterations;
    }

    // Normalize to 256 scale
    let max = pixels.iter().cloned().filter(|&x| x < ITER_LIMIT).max().unwrap();
    println!("max pixel: {}", max);
    for &(x, y) in &coordinates {
            let current = pixels[(SIZE*x + y) as usize];
            if current < ITER_LIMIT {
                pixels[(SIZE*x + y) as usize] *= 256;
                pixels[(SIZE*x + y) as usize] /= max;
            }
            else {
                pixels[(SIZE*x + y) as usize] = 256;
            }
    }

    // Create bmp
    create_bmp(pixels);
}

fn main() {
    run_fractal(burning_ship());
}
<|endoftext|>


<commit_before>extern crate clap;
extern crate kafka;

use clap::{Arg, App};
use kafka::client::{KafkaClient, ProduceMessage, RequiredAcks};
use std::time::Duration;
use std::io;
use std::io::prelude::*;

fn main() {
    let matches = App::new("ktee")
                      .version("0.1.0")
                      .about("tee for kafka")
                      .arg(Arg::with_name("broker")
                               .short("b")
                               .long("broker")
                               .value_name("BROKER")
                               .help("Kafka broker")
                               .takes_value(true)
                               .required(true))
                      .arg(Arg::with_name("topic")
                               .short("t")
                               .long("topic")
                               .value_name("TOPIC")
                               .help("Kafka topic")
                               .takes_value(true)
                               .required(true))
                      .get_matches();

    let broker = matches.value_of("broker").unwrap();
    let topic = matches.value_of("topic").unwrap();

    let mut client = KafkaClient::new(vec![broker.to_owned()]);
    let meta_res = client.load_metadata_all();
    if let Some(err) = meta_res.err() {
        println!("Error fetching metadata: {}", err);
        return;
    }

    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        match line {
            Ok(line) => {
                let req = vec![ProduceMessage::new(topic, 0, None, Some(&line.as_bytes()))];
                let res = client.produce_messages(RequiredAcks::One, Duration::from_millis(0), req);
                if let Some(err) = res.err() {
                    writeln!(&mut io::stderr(), "Error sending message: {}", err).unwrap();
                }

                let write_res = write!(&mut io::stdout(), "{}\n", &line);
                if let Some(err) = write_res.err() {
                    writeln!(&mut io::stderr(), "Error writing to stdout: {}", err).unwrap();
                }
            }
            Err(err) => writeln!(&mut io::stderr(), "Error reading from stdin: {}", err).unwrap(),
        }
    }
}
<commit_msg>Minor fixup<commit_after>extern crate clap;
extern crate kafka;

use clap::{Arg, App};
use kafka::client::{KafkaClient, ProduceMessage, RequiredAcks};
use std::time::Duration;
use std::io;
use std::io::prelude::*;

fn main() {
    let args = App::new("ktee")
                   .version("0.1.0")
                   .about("tee for kafka")
                   .arg(Arg::with_name("broker")
                            .short("b")
                            .long("broker")
                            .value_name("BROKER")
                            .help("Kafka broker")
                            .takes_value(true)
                            .required(true))
                   .arg(Arg::with_name("topic")
                            .short("t")
                            .long("topic")
                            .value_name("TOPIC")
                            .help("Kafka topic")
                            .takes_value(true)
                            .required(true))
                   .get_matches();
    let broker = args.value_of("broker").unwrap();
    let topic = args.value_of("topic").unwrap();

    let mut client = KafkaClient::new(vec![broker.to_owned()]);
    let meta_res = client.load_metadata_all();
    if let Some(err) = meta_res.err() {
        println!("Error fetching metadata: {}", err);
        return;
    }

    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        match line {
            Ok(line) => {
                let req = vec![ProduceMessage::new(topic, 0, None, Some(&line.as_bytes()))];
                let res = client.produce_messages(RequiredAcks::One, Duration::from_millis(0), req);
                if let Some(err) = res.err() {
                    writeln!(&mut io::stderr(), "Error sending message: {}", err).unwrap();
                }

                let write_res = write!(&mut io::stdout(), "{}\n", &line);
                if let Some(err) = write_res.err() {
                    writeln!(&mut io::stderr(), "Error writing to stdout: {}", err).unwrap();
                }
            }
            Err(err) => writeln!(&mut io::stderr(), "Error reading from stdin: {}", err).unwrap(),
        }
    }
}
<|endoftext|>


<commit_before>extern crate iron;
extern crate ladaemon;
extern crate router;

use iron::Iron;
use router::Router;
use std::env;
use std::io::{self, Write};

/// The `main()` method. Will loop forever to serve HTTP requests.
fn main() {

    // Make sure we get a file name where we can find configuration.
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        io::stderr().write(b"no configuration file specified\n").unwrap();
        return;
    }

    // Read the configuration from the provided file.
    let app = ladaemon::AppConfig::from_json_file(&args[1]);

    // Register all handlers with a router object. TODO: cloning the
    // configuration object is kind of ugly, but is apparently needed with
    // the way the Iron `Handler` trait is defined. Also, it would be cleaner
    // if the handlers could just be a function, instead of single-method
    // impls.
    let mut router = Router::new();
    router.get("/", ladaemon::WelcomeHandler { app: app.clone() });
    router.get("/.well-known/openid-configuration",
               ladaemon::OIDConfigHandler { app: app.clone() });
    router.get("/keys.json", ladaemon::KeysHandler { app: app.clone() });
    router.post("/auth", ladaemon::AuthHandler { app: app.clone() });
    router.get("/confirm", ladaemon::ConfirmHandler { app: app.clone() });
    router.get("/callback", ladaemon::CallbackHandler { app: app.clone() });

    // Iron will take care of stuff from here. It should spin up a number of
    // threads according to the number of cores available. TODO: make the
    // interface on which we listen configurable.
    Iron::new(router).http("0.0.0.0:3333").unwrap();

}
<commit_msg>Use router!() to set up routes<commit_after>extern crate iron;
extern crate ladaemon;

#[macro_use(router)]
extern crate router;

use iron::Iron;
use std::env;
use std::io::{self, Write};

/// The `main()` method. Will loop forever to serve HTTP requests.
fn main() {

    // Make sure we get a file name where we can find configuration.
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        io::stderr().write(b"no configuration file specified\n").unwrap();
        return;
    }

    // Read the configuration from the provided file.
    let app = ladaemon::AppConfig::from_json_file(&args[1]);

    // Register all handlers with a router object. TODO: cloning the
    // configuration object is kind of ugly, but is apparently needed with
    // the way the Iron `Handler` trait is defined. Also, it would be cleaner
    // if the handlers could just be a function, instead of single-method
    // impls.
    let router = router!{
        get "/" => ladaemon::WelcomeHandler { app: app.clone() },
        get "/.well-known/openid-configuration" =>
               ladaemon::OIDConfigHandler { app: app.clone() },
        get "/keys.json" => ladaemon::KeysHandler { app: app.clone() },
        post "/auth" => ladaemon::AuthHandler { app: app.clone() },
        get "/confirm" => ladaemon::ConfirmHandler { app: app.clone() },
        get "/callback" => ladaemon::CallbackHandler { app: app.clone() },
    };

    // Iron will take care of stuff from here. It should spin up a number of
    // threads according to the number of cores available. TODO: make the
    // interface on which we listen configurable.
    Iron::new(router).http("0.0.0.0:3333").unwrap();

}
<|endoftext|>


<commit_before>// Copyright (c) 2017, All Contributors (see CONTRIBUTORS file)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![feature(slice_patterns, advanced_slice_patterns)]
#![cfg_attr(test, feature(test))]

#![cfg_attr(not(target_os = "windows"), feature(alloc, heap_api))]
#![feature(alloc)]

include!("crates.rs");

extern crate num_cpus;
extern crate log4rs;

pub mod script;
pub mod server;
pub mod timestamp;
pub mod pubsub;
pub mod storage;

use std::fs;
use std::fs::{OpenOptions};
use std::path::PathBuf;
use std::thread;
use std::sync::Arc;
use memmap::{Mmap, Protection};

lazy_static! {
 static ref ENVIRONMENT: lmdb::Environment = {
    let _ = config::set_default("storage.path", "pumpkin.db");
    let storage_path = config::get_str("storage.path").unwrap().into_owned();
    fs::create_dir_all(storage_path.as_str()).expect("can't create directory");
    let map_size = config::get_int("storage.mapsize");
    storage::create_environment(storage_path, map_size)
 };
}

/// Accepts storage path, filename and length and prepares the file. It is important that the length
/// is the total length of the memory mapped file, otherwise the application _will segfault_ when
/// trying to read those sections later. There is no way to handle that.
fn prepare_mmap(storage_path: &str, filename: &str, length: u64) -> Mmap {
    let mut scratchpad_pathbuf = PathBuf::from(storage_path);
    scratchpad_pathbuf.push(filename);
    scratchpad_pathbuf.set_extension("dat");
    let scratchpad_path = scratchpad_pathbuf.as_path();
    let scratchpad_file = OpenOptions::new().create(true).write(true)
        .open(scratchpad_path).expect("Could not open or create scratchpad");
    let _ = scratchpad_file.set_len(length);
    Mmap::open_path(scratchpad_path, Protection::ReadWrite)
        .expect("Could not open scratchpad")
}

fn main() {
    let _ = config::merge(config::Environment::new("pumpkindb"));
    let _ = config::merge(config::File::new("pumpkindb.toml", config::FileFormat::Toml));
    let _ = config::set_default("server.port", 9981);
    let _ = config::set_default("storage.path", "pumpkin.db");
    let storage_path = config::get_str("storage.path").unwrap().into_owned();
    fs::create_dir_all(storage_path.as_str()).expect("can't create directory");

    // Initialize Mmap
    let scratchpad = prepare_mmap(storage_path.as_str(), "scratchpad", 20);
    let mut hlc_state = scratchpad.into_view_sync();
    hlc_state.restrict(0, 20).expect("Could not prepare HLC state");

    // Initialize logging
    let log_config = config::get_str("logging.config");
    let mut log_configured = false;
    if log_config.is_some() {
        let log_file_path = log_config.unwrap().into_owned();
        if fs::metadata(&log_file_path).is_ok() {
            log4rs::init_file(&log_file_path, Default::default()).unwrap();
            log_configured = true;
        } else {
            println!("{} not found", &log_file_path);
        }
    }

    if !log_configured {
        let appender = log4rs::config::Appender::builder()
            .build("console",
                   Box::new(log4rs::append::console::ConsoleAppender::builder().build()));
        let root = log4rs::config::Root::builder().appender("console").build(log::LogLevelFilter::Info);
        let _ = log4rs::init_config(log4rs::config::Config::builder().appender(appender).build(root).unwrap());
        warn!("No logging configuration specified, switching to console logging");
    }

    info!("Starting up");

    let mut senders = Vec::new();

    let mut publisher = pubsub::Publisher::new();
    let publisher_accessor = publisher.accessor();
    let _ = thread::spawn(move || publisher.run());
    let storage = Arc::new(storage::Storage::new(&ENVIRONMENT));
    let timestamp = Arc::new(timestamp::Timestamp::new(Some(hlc_state)));

    for i in 0..num_cpus::get() {
        info!("Starting scheduler on core {}.", i);
        let (sender, receiver) = script::Scheduler::create_sender();
        let publisher_clone = publisher_accessor.clone();
        let storage_clone = storage.clone();
        let timestamp_clone = timestamp.clone();
        thread::spawn(move || {
            let mut scheduler = script::Scheduler::new(
                &storage_clone,
                publisher_clone,
                timestamp_clone,
                receiver,
            );
            scheduler.run()
        });
        senders.push(sender)
    }

    server::run(config::get_int("server.port").unwrap(), senders, publisher_accessor.clone());

}
<commit_msg>Problem: The structure of the scratchpad is hidden<commit_after>// Copyright (c) 2017, All Contributors (see CONTRIBUTORS file)
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#![feature(slice_patterns, advanced_slice_patterns)]
#![cfg_attr(test, feature(test))]

#![cfg_attr(not(target_os = "windows"), feature(alloc, heap_api))]
#![feature(alloc)]

include!("crates.rs");

extern crate num_cpus;
extern crate log4rs;

pub mod script;
pub mod server;
pub mod timestamp;
pub mod pubsub;
pub mod storage;

use std::fs;
use std::fs::{OpenOptions};
use std::path::PathBuf;
use std::thread;
use std::sync::Arc;
use memmap::{Mmap, Protection};

lazy_static! {
 static ref ENVIRONMENT: lmdb::Environment = {
    let _ = config::set_default("storage.path", "pumpkin.db");
    let storage_path = config::get_str("storage.path").unwrap().into_owned();
    fs::create_dir_all(storage_path.as_str()).expect("can't create directory");
    let map_size = config::get_int("storage.mapsize");
    storage::create_environment(storage_path, map_size)
 };
}

/// Accepts storage path, filename and length and prepares the file. It is important that the length
/// is the total length of the memory mapped file, otherwise the application _will segfault_ when
/// trying to read those sections later. There is no way to handle that.
/// The mmap file is structured as such now:
/// Byte Range         Used for
/// [0..20]            Last known HTC timestamp
fn prepare_mmap(storage_path: &str, filename: &str, length: u64) -> Mmap {
    let mut scratchpad_pathbuf = PathBuf::from(storage_path);
    scratchpad_pathbuf.push(filename);
    scratchpad_pathbuf.set_extension("dat");
    let scratchpad_path = scratchpad_pathbuf.as_path();
    let scratchpad_file = OpenOptions::new().create(true).write(true)
        .open(scratchpad_path).expect("Could not open or create scratchpad");
    let _ = scratchpad_file.set_len(length);
    Mmap::open_path(scratchpad_path, Protection::ReadWrite)
        .expect("Could not open scratchpad")
}

fn main() {
    let _ = config::merge(config::Environment::new("pumpkindb"));
    let _ = config::merge(config::File::new("pumpkindb.toml", config::FileFormat::Toml));
    let _ = config::set_default("server.port", 9981);
    let _ = config::set_default("storage.path", "pumpkin.db");
    let storage_path = config::get_str("storage.path").unwrap().into_owned();
    fs::create_dir_all(storage_path.as_str()).expect("can't create directory");

    // Initialize Mmap
    let scratchpad = prepare_mmap(storage_path.as_str(), "scratchpad", 20);
    let mut hlc_state = scratchpad.into_view_sync();
    hlc_state.restrict(0, 20).expect("Could not prepare HLC state");

    // Initialize logging
    let log_config = config::get_str("logging.config");
    let mut log_configured = false;
    if log_config.is_some() {
        let log_file_path = log_config.unwrap().into_owned();
        if fs::metadata(&log_file_path).is_ok() {
            log4rs::init_file(&log_file_path, Default::default()).unwrap();
            log_configured = true;
        } else {
            println!("{} not found", &log_file_path);
        }
    }

    if !log_configured {
        let appender = log4rs::config::Appender::builder()
            .build("console",
                   Box::new(log4rs::append::console::ConsoleAppender::builder().build()));
        let root = log4rs::config::Root::builder().appender("console").build(log::LogLevelFilter::Info);
        let _ = log4rs::init_config(log4rs::config::Config::builder().appender(appender).build(root).unwrap());
        warn!("No logging configuration specified, switching to console logging");
    }

    info!("Starting up");

    let mut senders = Vec::new();

    let mut publisher = pubsub::Publisher::new();
    let publisher_accessor = publisher.accessor();
    let _ = thread::spawn(move || publisher.run());
    let storage = Arc::new(storage::Storage::new(&ENVIRONMENT));
    let timestamp = Arc::new(timestamp::Timestamp::new(Some(hlc_state)));

    for i in 0..num_cpus::get() {
        info!("Starting scheduler on core {}.", i);
        let (sender, receiver) = script::Scheduler::create_sender();
        let publisher_clone = publisher_accessor.clone();
        let storage_clone = storage.clone();
        let timestamp_clone = timestamp.clone();
        thread::spawn(move || {
            let mut scheduler = script::Scheduler::new(
                &storage_clone,
                publisher_clone,
                timestamp_clone,
                receiver,
            );
            scheduler.run()
        });
        senders.push(sender)
    }

    server::run(config::get_int("server.port").unwrap(), senders, publisher_accessor.clone());

}
<|endoftext|>


<commit_before>//#![deny(warnings)]

extern crate git2;
extern crate docopt;
extern crate rustc_serialize;

use std::process::{Command, exit};

use docopt::Docopt;
use git2::{Repository, Error, StatusOptions, ErrorCode};

#[derive(RustcDecodable)]
struct Args {
    arg_spec: Vec<String>,
    flag_git_dir: Option<String>,
}

/// Gets the current branch.
///
/// # Examples
///
/// ```
/// let repo = try!(Repository::open("."));
/// let branchname = try!(branch(&repo));
/// ```
fn branch<'a>(repo: &'a Repository) -> Result<String, Error> {
    let head = match repo.head() {
        Ok(head) => Some(head),
            Err(ref e) if e.code() == ErrorCode::UnbornBranch ||
                          e.code() == ErrorCode::NotFound
                       => None,
            Err(e) => return Err(e),
    };
    let branchref = head.as_ref().and_then(|h| h.shorthand());
    let branchname = branchref.unwrap_or("HEAD (no branch)").to_string();

    Ok(branchname)
}

fn run(args: &Args) -> Result<(), Error> {
    let path = args.flag_git_dir.clone().unwrap_or(".".to_string());
    let repo = try!(Repository::open(&path));

    if repo.is_bare() {
        return Err(Error::from_str("Bare repositories are not allowed"))
    }

    let mut opts = StatusOptions::new();

    for spec in args.arg_spec.iter() {
        opts.pathspec(spec);
    }

    let current_branch = try!(branch(&repo));

    println!("Current branch {:?}", current_branch);

    checkout("master");
    pull("origin", "master");
    checkout(current_branch);
    // rebase("master");

    return Ok(())
}

fn checkout(target_branch: &str) {
    let output = Command::new("git")
                         .arg("checkout")
                         .arg(target_branch)
                         .output()
                         .unwrap_or_else(|e| {
                            panic!("failed to execute process: {}", e) });
    let status_code = output.status.code();

    println!("Checking out {} ...", target_branch);

    match status_code {
        Some(0) => println!("{}", String::from_utf8_lossy(&output.stdout)),
        Some(_) => {
             println!("{}", String::from_utf8_lossy(&output.stderr));
             exit(status_code.unwrap());
        }
        None => println!("Something wrong happened"),
    }
}

fn pull(target_remote: &str, target_branch: &str) {
    let output = Command::new("git")
                         .arg("pull")
                         .arg("--quiet")
                         .arg("--rebase=preserve")
                         .arg(target_remote)
                         .arg(target_branch)
                         .output()
                         .unwrap_or_else(|e| {
                            panic!("failed to execute process: {}", e) });
    let status_code = output.status.code();

    println!("Pulling {} {} ...", target_remote, target_branch);

    match status_code {
        Some(0) => println!("{}", String::from_utf8_lossy(&output.stdout)),
        Some(_) => {
             println!("{}", String::from_utf8_lossy(&output.stderr));
             exit(status_code.unwrap());
        }
        None => println!("Something wrong happened"),
    }
}

fn main() {
    const USAGE: &'static str = "
usage: absorb [options] [--] [<branchname>]

Options:
    -h, --help                  show this message
";

    let args = Docopt::new(USAGE).and_then(|d| d.decode())
                                 .unwrap_or_else(|e| e.exit());
    match run(&args) {
        Ok(()) => {}
        Err(e) => println!("error: {}", e),
    }
}
<commit_msg>Fix current_branch reference<commit_after>//#![deny(warnings)]

extern crate git2;
extern crate docopt;
extern crate rustc_serialize;

use std::process::{Command, exit};

use docopt::Docopt;
use git2::{Repository, Error, StatusOptions, ErrorCode};

#[derive(RustcDecodable)]
struct Args {
    arg_spec: Vec<String>,
    flag_git_dir: Option<String>,
}

/// Gets the current branch.
///
/// # Examples
///
/// ```
/// let repo = try!(Repository::open("."));
/// let branchname = try!(branch(&repo));
/// ```
fn branch<'a>(repo: &'a Repository) -> Result<String, Error> {
    let head = match repo.head() {
        Ok(head) => Some(head),
            Err(ref e) if e.code() == ErrorCode::UnbornBranch ||
                          e.code() == ErrorCode::NotFound
                       => None,
            Err(e) => return Err(e),
    };
    let branchref = head.as_ref().and_then(|h| h.shorthand());
    let branchname = branchref.unwrap_or("HEAD (no branch)").to_string();

    Ok(branchname)
}

fn run(args: &Args) -> Result<(), Error> {
    let path = args.flag_git_dir.clone().unwrap_or(".".to_string());
    let repo = try!(Repository::open(&path));

    if repo.is_bare() {
        return Err(Error::from_str("Bare repositories are not allowed"))
    }

    let mut opts = StatusOptions::new();

    for spec in args.arg_spec.iter() {
        opts.pathspec(spec);
    }

    let current_branch = try!(branch(&repo));

    println!("Current branch {:?}", current_branch);

    checkout("master");
    pull("origin", "master");
    checkout(&current_branch);
    // rebase("master");

    return Ok(())
}

fn checkout(target_branch: &str) {
    let output = Command::new("git")
                         .arg("checkout")
                         .arg(target_branch)
                         .output()
                         .unwrap_or_else(|e| {
                            panic!("failed to execute process: {}", e) });
    let status_code = output.status.code();

    println!("Checking out {} ...", target_branch);

    match status_code {
        Some(0) => println!("{}", String::from_utf8_lossy(&output.stdout)),
        Some(_) => {
             println!("{}", String::from_utf8_lossy(&output.stderr));
             exit(status_code.unwrap());
        }
        None => println!("Something wrong happened"),
    }
}

fn pull(target_remote: &str, target_branch: &str) {
    let output = Command::new("git")
                         .arg("pull")
                         .arg("--quiet")
                         .arg("--rebase=preserve")
                         .arg(target_remote)
                         .arg(target_branch)
                         .output()
                         .unwrap_or_else(|e| {
                            panic!("failed to execute process: {}", e) });
    let status_code = output.status.code();

    println!("Pulling {} {} ...", target_remote, target_branch);

    match status_code {
        Some(0) => println!("{}", String::from_utf8_lossy(&output.stdout)),
        Some(_) => {
             println!("{}", String::from_utf8_lossy(&output.stderr));
             exit(status_code.unwrap());
        }
        None => println!("Something wrong happened"),
    }
}

fn main() {
    const USAGE: &'static str = "
usage: absorb [options] [--] [<branchname>]

Options:
    -h, --help                  show this message
";

    let args = Docopt::new(USAGE).and_then(|d| d.decode())
                                 .unwrap_or_else(|e| e.exit());
    match run(&args) {
        Ok(()) => {}
        Err(e) => println!("error: {}", e),
    }
}
<|endoftext|>


<commit_before>mod vm;
mod command;
mod parse;
mod program;

use std::io::Write;
use std::env;
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;

use vm::VM;
use program::Program;
use parse::parse;

fn main() {
    let vm = VM::new();
    let program = Program::new();
    match env::args().nth(1) {
        None => repl(vm, program),
        Some(arg) => interpreter(arg, vm, program),
    }
}

fn repl (mut vm: VM, mut program: Program) -> () {
    loop {
        print!("{}", if program.is_seeking {"... "} else {"bf> "});
        std::io::stdout().flush().unwrap();

        let mut line = String::new();
        let commands = match std::io::stdin().read_line(&mut line) {
            Ok(0) => break,
            Ok(_) => parse(&line),
            Err(error) => panic!(error),
        };
        program.append(&commands);
        program.execute(&mut vm);
    }
}

fn interpreter(arg: String, mut vm: VM, mut program: Program) -> () {
    let path = Path::new(&arg);
    let mut contents = String::new();
    File::open(&path)
        .unwrap()
        .read_to_string(&mut contents)
        .unwrap();

    program.append(&parse(&contents));
    program.execute(&mut vm);
}
<commit_msg>Simplify imports<commit_after>mod vm;
mod command;
mod parse;
mod program;

use std::io::{Write, Read};
use std::env;
use std::path::Path;
use std::fs::File;

use vm::VM;
use program::Program;
use parse::parse;

fn main() {
    let vm = VM::new();
    let program = Program::new();
    match env::args().nth(1) {
        None => repl(vm, program),
        Some(arg) => interpreter(arg, vm, program),
    }
}

fn repl (mut vm: VM, mut program: Program) -> () {
    loop {
        print!("{}", if program.is_seeking {"... "} else {"bf> "});
        std::io::stdout().flush().unwrap();

        let mut line = String::new();
        let commands = match std::io::stdin().read_line(&mut line) {
            Ok(0) => break,
            Ok(_) => parse(&line),
            Err(error) => panic!(error),
        };
        program.append(&commands);
        program.execute(&mut vm);
    }
}

fn interpreter(arg: String, mut vm: VM, mut program: Program) -> () {
    let path = Path::new(&arg);
    let mut contents = String::new();
    File::open(&path)
        .unwrap()
        .read_to_string(&mut contents)
        .unwrap();

    program.append(&parse(&contents));
    program.execute(&mut vm);
}
<|endoftext|>