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 <eta@theta.eu.org> Tested-by: BuildkiteCI
This commit is contained in:
parent
03076c3977
commit
78bfb66a95
3 changed files with 23 additions and 0 deletions
|
@ -8,6 +8,7 @@ pub struct KeywordDetails {
|
||||||
pub keyword: Keyword,
|
pub keyword: Keyword,
|
||||||
pub entries: Vec<Entry>,
|
pub entries: Vec<Entry>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl KeywordDetails {
|
impl KeywordDetails {
|
||||||
pub fn learn(&mut self, nick: &str, text: &str, dbc: &PgConnection) -> Result<usize, Error> {
|
pub fn learn(&mut self, nick: &str, text: &str, dbc: &PgConnection) -> Result<usize, Error> {
|
||||||
let now = ::chrono::Utc::now().naive_utc();
|
let now = ::chrono::Utc::now().naive_utc();
|
||||||
|
@ -27,6 +28,7 @@ impl KeywordDetails {
|
||||||
self.entries.push(new);
|
self.entries.push(new);
|
||||||
Ok(self.entries.len())
|
Ok(self.entries.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process_moves(&mut self, moves: &[(i32, i32)], dbc: &PgConnection) -> Result<(), Error> {
|
pub fn process_moves(&mut self, moves: &[(i32, i32)], dbc: &PgConnection) -> Result<(), Error> {
|
||||||
for (oid, new_idx) in moves {
|
for (oid, new_idx) in moves {
|
||||||
{
|
{
|
||||||
|
@ -39,6 +41,7 @@ impl KeywordDetails {
|
||||||
self.entries = Self::get_entries(self.keyword.id, dbc)?;
|
self.entries = Self::get_entries(self.keyword.id, dbc)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn swap(&mut self, idx_a: usize, idx_b: usize, dbc: &PgConnection) -> Result<(), Error> {
|
pub fn swap(&mut self, idx_a: usize, idx_b: usize, dbc: &PgConnection) -> Result<(), Error> {
|
||||||
let mut moves = vec![];
|
let mut moves = vec![];
|
||||||
for ent in self.entries.iter() {
|
for ent in self.entries.iter() {
|
||||||
|
@ -55,6 +58,7 @@ impl KeywordDetails {
|
||||||
self.process_moves(&moves, dbc)?;
|
self.process_moves(&moves, dbc)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self, idx: usize, val: &str, dbc: &PgConnection) -> Result<(), Error> {
|
pub fn update(&mut self, idx: usize, val: &str, dbc: &PgConnection) -> Result<(), Error> {
|
||||||
let ent = self
|
let ent = self
|
||||||
.entries
|
.entries
|
||||||
|
@ -69,6 +73,7 @@ impl KeywordDetails {
|
||||||
ent.text = val.to_string();
|
ent.text = val.to_string();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn delete(&mut self, idx: usize, dbc: &PgConnection) -> Result<(), Error> {
|
pub fn delete(&mut self, idx: usize, dbc: &PgConnection) -> Result<(), Error> {
|
||||||
// step 1: delete the element
|
// step 1: delete the element
|
||||||
{
|
{
|
||||||
|
@ -91,11 +96,13 @@ impl KeywordDetails {
|
||||||
self.process_moves(&moves, dbc)?;
|
self.process_moves(&moves, dbc)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_zwsp_to_name(name: &str) -> Option<String> {
|
pub fn add_zwsp_to_name(name: &str) -> Option<String> {
|
||||||
let second_index = name.char_indices().nth(1).map(|(i, _)| i)?;
|
let second_index = name.char_indices().nth(1).map(|(i, _)| i)?;
|
||||||
let (start, end) = name.split_at(second_index);
|
let (start, end) = name.split_at(second_index);
|
||||||
Some(format!("{}{}", start, end))
|
Some(format!("{}{}", start, end))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn format_entry(&self, idx: usize) -> Option<String> {
|
pub fn format_entry(&self, idx: usize) -> Option<String> {
|
||||||
if let Some(ent) = self.entries.get(idx.saturating_sub(1)) {
|
if let Some(ent) = self.entries.get(idx.saturating_sub(1)) {
|
||||||
let gen_clr = if self.keyword.chan == "*" {
|
let gen_clr = if self.keyword.chan == "*" {
|
||||||
|
@ -118,6 +125,7 @@ impl KeywordDetails {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_or_create(word: &str, c: &str, dbc: &PgConnection) -> Result<Self, Error> {
|
pub fn get_or_create(word: &str, c: &str, dbc: &PgConnection) -> Result<Self, Error> {
|
||||||
if let Some(ret) = Self::get(word, c, dbc)? {
|
if let Some(ret) = Self::get(word, c, dbc)? {
|
||||||
Ok(ret)
|
Ok(ret)
|
||||||
|
@ -125,6 +133,7 @@ impl KeywordDetails {
|
||||||
Ok(Self::create(word, c, dbc)?)
|
Ok(Self::create(word, c, dbc)?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create(word: &str, c: &str, dbc: &PgConnection) -> Result<Self, Error> {
|
pub fn create(word: &str, c: &str, dbc: &PgConnection) -> Result<Self, Error> {
|
||||||
let val = NewKeyword {
|
let val = NewKeyword {
|
||||||
name: word,
|
name: word,
|
||||||
|
@ -141,6 +150,7 @@ impl KeywordDetails {
|
||||||
entries: vec![],
|
entries: vec![],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_entries(kid: i32, dbc: &PgConnection) -> Result<Vec<Entry>, Error> {
|
fn get_entries(kid: i32, dbc: &PgConnection) -> Result<Vec<Entry>, Error> {
|
||||||
let entries: Vec<Entry> = {
|
let entries: Vec<Entry> = {
|
||||||
use crate::schema::entries::dsl::*;
|
use crate::schema::entries::dsl::*;
|
||||||
|
@ -151,6 +161,7 @@ impl KeywordDetails {
|
||||||
};
|
};
|
||||||
Ok(entries)
|
Ok(entries)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<'a, T: Into<Cow<'a, str>>>(
|
pub fn get<'a, T: Into<Cow<'a, str>>>(
|
||||||
word: T,
|
word: T,
|
||||||
c: &str,
|
c: &str,
|
||||||
|
|
|
@ -52,6 +52,7 @@ impl App {
|
||||||
.send_notice(nick, format!("[{}] \x0304Error:\x0f {}", chan, msg))?;
|
.send_notice(nick, format!("[{}] \x0304Error:\x0f {}", chan, msg))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn keyword_from_captures(
|
pub fn keyword_from_captures(
|
||||||
&mut self,
|
&mut self,
|
||||||
learn: &::regex::Captures,
|
learn: &::regex::Captures,
|
||||||
|
@ -78,6 +79,7 @@ impl App {
|
||||||
}
|
}
|
||||||
Ok(kwd)
|
Ok(kwd)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_move(
|
pub fn handle_move(
|
||||||
&mut self,
|
&mut self,
|
||||||
target: &str,
|
target: &str,
|
||||||
|
@ -114,6 +116,7 @@ impl App {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_learn(
|
pub fn handle_learn(
|
||||||
&mut self,
|
&mut self,
|
||||||
target: &str,
|
target: &str,
|
||||||
|
@ -129,6 +132,7 @@ impl App {
|
||||||
.send_notice(target, kwd.format_entry(idx).unwrap())?;
|
.send_notice(target, kwd.format_entry(idx).unwrap())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_insert_last_quote(
|
pub fn handle_insert_last_quote(
|
||||||
&mut self,
|
&mut self,
|
||||||
target: &str,
|
target: &str,
|
||||||
|
@ -152,6 +156,7 @@ impl App {
|
||||||
.send_notice(target, kwd.format_entry(idx).unwrap())?;
|
.send_notice(target, kwd.format_entry(idx).unwrap())?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_increment(
|
pub fn handle_increment(
|
||||||
&mut self,
|
&mut self,
|
||||||
target: &str,
|
target: &str,
|
||||||
|
@ -183,6 +188,7 @@ impl App {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_query(
|
pub fn handle_query(
|
||||||
&mut self,
|
&mut self,
|
||||||
target: &str,
|
target: &str,
|
||||||
|
@ -258,6 +264,7 @@ impl App {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_privmsg(&mut self, from: &str, chan: &str, msg: &str) -> Result<(), Error> {
|
pub fn handle_privmsg(&mut self, from: &str, chan: &str, msg: &str) -> Result<(), Error> {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref LEARN_RE: Regex =
|
static ref LEARN_RE: Regex =
|
||||||
|
@ -297,6 +304,7 @@ impl App {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_msg(&mut self, m: Message) -> Result<(), Error> {
|
pub fn handle_msg(&mut self, m: Message) -> Result<(), Error> {
|
||||||
match m.command {
|
match m.command {
|
||||||
Command::PRIVMSG(channel, message) => {
|
Command::PRIVMSG(channel, message) => {
|
||||||
|
@ -320,6 +328,7 @@ impl App {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Error> {
|
fn main() -> Result<(), Error> {
|
||||||
println!("[+] loading configuration");
|
println!("[+] loading configuration");
|
||||||
let default_log_filter = "paroxysm=info".to_string();
|
let default_log_filter = "paroxysm=info".to_string();
|
||||||
|
|
|
@ -7,6 +7,7 @@ pub struct Keyword {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub chan: String,
|
pub chan: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Queryable)]
|
#[derive(Queryable)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
|
@ -16,12 +17,14 @@ pub struct Entry {
|
||||||
pub creation_ts: NaiveDateTime,
|
pub creation_ts: NaiveDateTime,
|
||||||
pub created_by: String,
|
pub created_by: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "keywords"]
|
#[table_name = "keywords"]
|
||||||
pub struct NewKeyword<'a> {
|
pub struct NewKeyword<'a> {
|
||||||
pub name: &'a str,
|
pub name: &'a str,
|
||||||
pub chan: &'a str,
|
pub chan: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Insertable)]
|
#[derive(Insertable)]
|
||||||
#[table_name = "entries"]
|
#[table_name = "entries"]
|
||||||
pub struct NewEntry<'a> {
|
pub struct NewEntry<'a> {
|
||||||
|
|
Loading…
Reference in a new issue