From 78bfb66a95ad1c105e4ab21e0fc3f5b85ade1982 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 1 Jul 2020 17:56:08 +0100 Subject: style(tvldb): Add blank lines between items This makes the code slightly more readable. For users that use editors without semantic navigation, this also makes it easier to jump around between items in the files. I looked into whether a rustfmt setting exists for this, but unfortunately the answer is currently no. Change-Id: I37b19fa6ab038c71b924c45dbc12b298e660e8cf Reviewed-on: https://cl.tvl.fyi/c/depot/+/827 Reviewed-by: BuildkiteCI Reviewed-by: eta Tested-by: BuildkiteCI --- fun/tvldb/src/keyword.rs | 11 +++++++++++ fun/tvldb/src/main.rs | 9 +++++++++ fun/tvldb/src/models.rs | 3 +++ 3 files changed, 23 insertions(+) (limited to 'fun') diff --git a/fun/tvldb/src/keyword.rs b/fun/tvldb/src/keyword.rs index 78dc36f35f12..fe296f03df9e 100644 --- a/fun/tvldb/src/keyword.rs +++ b/fun/tvldb/src/keyword.rs @@ -8,6 +8,7 @@ pub struct KeywordDetails { pub keyword: Keyword, pub entries: Vec, } + impl KeywordDetails { pub fn learn(&mut self, nick: &str, text: &str, dbc: &PgConnection) -> Result { let now = ::chrono::Utc::now().naive_utc(); @@ -27,6 +28,7 @@ impl KeywordDetails { self.entries.push(new); Ok(self.entries.len()) } + pub fn process_moves(&mut self, moves: &[(i32, i32)], dbc: &PgConnection) -> Result<(), Error> { for (oid, new_idx) in moves { { @@ -39,6 +41,7 @@ impl KeywordDetails { self.entries = Self::get_entries(self.keyword.id, dbc)?; Ok(()) } + pub fn swap(&mut self, idx_a: usize, idx_b: usize, dbc: &PgConnection) -> Result<(), Error> { let mut moves = vec![]; for ent in self.entries.iter() { @@ -55,6 +58,7 @@ impl KeywordDetails { self.process_moves(&moves, dbc)?; Ok(()) } + pub fn update(&mut self, idx: usize, val: &str, dbc: &PgConnection) -> Result<(), Error> { let ent = self .entries @@ -69,6 +73,7 @@ impl KeywordDetails { ent.text = val.to_string(); Ok(()) } + pub fn delete(&mut self, idx: usize, dbc: &PgConnection) -> Result<(), Error> { // step 1: delete the element { @@ -91,11 +96,13 @@ impl KeywordDetails { self.process_moves(&moves, dbc)?; Ok(()) } + pub fn add_zwsp_to_name(name: &str) -> Option { let second_index = name.char_indices().nth(1).map(|(i, _)| i)?; let (start, end) = name.split_at(second_index); Some(format!("{}​{}", start, end)) } + pub fn format_entry(&self, idx: usize) -> Option { if let Some(ent) = self.entries.get(idx.saturating_sub(1)) { let gen_clr = if self.keyword.chan == "*" { @@ -118,6 +125,7 @@ impl KeywordDetails { None } } + pub fn get_or_create(word: &str, c: &str, dbc: &PgConnection) -> Result { if let Some(ret) = Self::get(word, c, dbc)? { Ok(ret) @@ -125,6 +133,7 @@ impl KeywordDetails { Ok(Self::create(word, c, dbc)?) } } + pub fn create(word: &str, c: &str, dbc: &PgConnection) -> Result { let val = NewKeyword { name: word, @@ -141,6 +150,7 @@ impl KeywordDetails { entries: vec![], }) } + fn get_entries(kid: i32, dbc: &PgConnection) -> Result, Error> { let entries: Vec = { use crate::schema::entries::dsl::*; @@ -151,6 +161,7 @@ impl KeywordDetails { }; Ok(entries) } + pub fn get<'a, T: Into>>( word: T, c: &str, diff --git a/fun/tvldb/src/main.rs b/fun/tvldb/src/main.rs index 9fe7584b61b7..5d2e670337f6 100644 --- a/fun/tvldb/src/main.rs +++ b/fun/tvldb/src/main.rs @@ -52,6 +52,7 @@ impl App { .send_notice(nick, format!("[{}] \x0304Error:\x0f {}", chan, msg))?; Ok(()) } + pub fn keyword_from_captures( &mut self, learn: &::regex::Captures, @@ -78,6 +79,7 @@ impl App { } Ok(kwd) } + pub fn handle_move( &mut self, target: &str, @@ -114,6 +116,7 @@ impl App { } Ok(()) } + pub fn handle_learn( &mut self, target: &str, @@ -129,6 +132,7 @@ impl App { .send_notice(target, kwd.format_entry(idx).unwrap())?; Ok(()) } + pub fn handle_insert_last_quote( &mut self, target: &str, @@ -152,6 +156,7 @@ impl App { .send_notice(target, kwd.format_entry(idx).unwrap())?; Ok(()) } + pub fn handle_increment( &mut self, target: &str, @@ -183,6 +188,7 @@ impl App { } Ok(()) } + pub fn handle_query( &mut self, target: &str, @@ -258,6 +264,7 @@ impl App { } Ok(()) } + pub fn handle_privmsg(&mut self, from: &str, chan: &str, msg: &str) -> Result<(), Error> { lazy_static! { static ref LEARN_RE: Regex = @@ -297,6 +304,7 @@ impl App { } Ok(()) } + pub fn handle_msg(&mut self, m: Message) -> Result<(), Error> { match m.command { Command::PRIVMSG(channel, message) => { @@ -320,6 +328,7 @@ impl App { Ok(()) } } + fn main() -> Result<(), Error> { println!("[+] loading configuration"); let default_log_filter = "paroxysm=info".to_string(); diff --git a/fun/tvldb/src/models.rs b/fun/tvldb/src/models.rs index 78ddce0af96c..721efbbb2e61 100644 --- a/fun/tvldb/src/models.rs +++ b/fun/tvldb/src/models.rs @@ -7,6 +7,7 @@ pub struct Keyword { pub name: String, pub chan: String, } + #[derive(Queryable)] pub struct Entry { pub id: i32, @@ -16,12 +17,14 @@ pub struct Entry { pub creation_ts: NaiveDateTime, pub created_by: String, } + #[derive(Insertable)] #[table_name = "keywords"] pub struct NewKeyword<'a> { pub name: &'a str, pub chan: &'a str, } + #[derive(Insertable)] #[table_name = "entries"] pub struct NewEntry<'a> { -- cgit 1.4.1