~gbmor/clinte

e276b9bfaf5795ed9e5ec4896fc9d2678ff8e51c — Ben Morrison 2 years ago ebc4efa
ensure submitted posts are utf8
4 files changed, 22 insertions(+), 7 deletions(-)

M Cargo.lock
M Cargo.toml
M src/db.rs
M src/main.rs
M Cargo.lock => Cargo.lock +1 -1
@@ 119,7 119,7 @@ dependencies = [

[[package]]
name = "clinte"
version = "0.3.0"
version = "0.3.1"
dependencies = [
 "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
 "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",

M Cargo.toml => Cargo.toml +1 -1
@@ 1,6 1,6 @@
[package]
name = "clinte"
version = "0.3.0"
version = "0.3.1"
authors = ["Ben Morrison <ben@gbmor.dev>"]
edition = "2018"


M src/db.rs => src/db.rs +1 -1
@@ 31,7 31,7 @@ impl Conn {

        conn.execute(
            "CREATE TABLE IF NOT EXISTS posts (
            id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
            id INTEGER PRIMARY KEY NOT NULL,
            title TEXT NOT NULL,
            author TEXT NOT NULL,
            body TEXT NOT NULL

M src/main.rs => src/main.rs +19 -4
@@ 50,14 50,28 @@ fn main() {
    list_matches(&db);
}

// Make sure nobody encodes narsty characters
// into a message to negatively affect other
// users
fn str_to_utf8(str: &str) -> String {
    str.chars()
        .map(|c| {
            let mut buf = [0; 4];
            c.encode_utf8(&mut buf).to_string()
        })
        .collect::<String>()
}

fn list_matches(db: &db::Conn) {
    let mut stmt = db.conn.prepare("SELECT * FROM posts").unwrap();
    let out = stmt
        .query_map(rusqlite::NO_PARAMS, |row| {
            let id = row.get(0)?;
            let title = row.get(1)?;
            let author = row.get(2)?;
            let body = row.get(3)?;
            let id: u32 = row.get(0)?;
            let title: String = row.get(1)?;
            let author: String = row.get(2)?;
            let body: String = row.get(3)?;
            let title = str_to_utf8(&title);
            let body = str_to_utf8(&body);
            Ok(db::Post {
                id,
                title,


@@ 191,6 205,7 @@ fn delete(db: &db::Conn) {
    let mut id_num_in = String::new();
    io::stdin().read_line(&mut id_num_in).unwrap();
    let id_num_in: u32 = id_num_in.trim().parse().unwrap();
    println!();

    let del_stmt = format!("DELETE FROM posts WHERE id = {}", id_num_in);
    let get_stmt = format!("SELECT * FROM posts WHERE id = {}", id_num_in);