style: fmt & clippy processed

This commit is contained in:
VC
2022-08-11 12:29:40 +02:00
parent dab8725f99
commit b11595bfca
10 changed files with 297 additions and 209 deletions

View File

@@ -1,6 +1,6 @@
use std::error::Error;
use log::debug;
use rusqlite::{Connection, params, OptionalExtension};
use rusqlite::{params, Connection, OptionalExtension};
use std::error::Error;
/// Struct for each query line
#[derive(Debug)]
@@ -11,22 +11,26 @@ pub struct TweetToToot {
/// if None is passed, read the last tweet from DB
/// if a tweet_id is passed, read this particular tweet from DB
pub fn read_state(conn: &Connection, s: Option<u64>) -> Result<Option<TweetToToot>, Box<dyn Error>> {
pub fn read_state(
conn: &Connection,
s: Option<u64>,
) -> Result<Option<TweetToToot>, Box<dyn Error>> {
debug!("Reading tweet_id {:?}", s);
let query: String;
match s {
Some(i) => query = format!("SELECT * FROM tweet_to_toot WHERE tweet_id = {}", i),
None => query = "SELECT * FROM tweet_to_toot ORDER BY tweet_id DESC LIMIT 1".to_string(),
let query: String = match s {
Some(i) => format!("SELECT * FROM tweet_to_toot WHERE tweet_id = {}", i),
None => "SELECT * FROM tweet_to_toot ORDER BY tweet_id DESC LIMIT 1".to_string(),
};
let mut stmt = conn.prepare(&query)?;
let t = stmt.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get(0)?,
toot_id: row.get(1)?,
let t = stmt
.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get(0)?,
toot_id: row.get(1)?,
})
})
}).optional()?;
.optional()?;
Ok(t)
}
@@ -37,7 +41,7 @@ pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Erro
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id) VALUES (?1, ?2)",
params![t.tweet_id, t.toot_id],
)?;
)?;
Ok(())
}
@@ -61,10 +65,7 @@ pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
#[cfg(test)]
mod tests {
use super::*;
use std::{
fs::remove_file,
path::Path,
};
use std::{fs::remove_file, path::Path};
#[test]
fn test_init_db() {
@@ -77,10 +78,7 @@ mod tests {
// open said file
let conn = Connection::open(d).unwrap();
conn.execute(
"SELECT * from tweet_to_toot;",
[],
).unwrap();
conn.execute("SELECT * from tweet_to_toot;", []).unwrap();
remove_file(d).unwrap();
}
@@ -99,7 +97,8 @@ mod tests {
VALUES
(100, 'A');",
[],
).unwrap();
)
.unwrap();
init_db(d).unwrap();
@@ -123,12 +122,14 @@ mod tests {
let mut stmt = conn.prepare("SELECT * FROM tweet_to_toot;").unwrap();
let t_out = stmt.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get(0).unwrap(),
toot_id: row.get(1).unwrap(),
let t_out = stmt
.query_row([], |row| {
Ok(TweetToToot {
tweet_id: row.get(0).unwrap(),
toot_id: row.get(1).unwrap(),
})
})
}).unwrap();
.unwrap();
assert_eq!(t_out.tweet_id, 123456789);
assert_eq!(t_out.toot_id, "987654321".to_string());
@@ -150,7 +151,8 @@ mod tests {
(101, 'A'),
(102, 'B');",
[],
).unwrap();
)
.unwrap();
let t_out = read_state(&conn, None).unwrap().unwrap();
@@ -188,7 +190,8 @@ mod tests {
VALUES
(100, 'A');",
[],
).unwrap();
)
.unwrap();
let t_out = read_state(&conn, Some(101)).unwrap();
@@ -210,7 +213,8 @@ mod tests {
VALUES
(100, 'A');",
[],
).unwrap();
)
.unwrap();
let t_out = read_state(&conn, Some(100)).unwrap().unwrap();
@@ -220,4 +224,3 @@ mod tests {
assert_eq!(t_out.toot_id, "A");
}
}