Skip to content

Commit

Permalink
clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Feb 3, 2024
1 parent e2da7f8 commit 5285f5e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use grammers_client::{Client, Config};
use grammers_session::Session;
use tokio::sync::RwLock;
use tokio::sync::Mutex;

pub mod consts;
mod db;
Expand Down Expand Up @@ -33,7 +33,7 @@ static FIXED_RECONNECT_POLICY: grammers_mtsender::FixedReconnect =
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
env_logger::init();
let db = Arc::new(RwLock::new(db::Db::new_with_file(DB_NAME)?));
let db = Arc::new(Mutex::new(db::Db::new_with_file(DB_NAME)?));
let env: BotInfo = envy::from_env()?;

let client = Client::connect(Config {
Expand Down
6 changes: 2 additions & 4 deletions src/openai/api.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fmt::format;

use grammers_client::types::Message;
use openai_api_rs::v1::{
api::Client,
Expand All @@ -17,15 +15,15 @@ pub enum GPTLenght {
}

impl GPTLenght {
fn to_max_tokens(&self) -> i64 {
fn to_max_tokens(self) -> i64 {
match self {
GPTLenght::Short => 64,
GPTLenght::Medium => 128,
GPTLenght::Long => 256,
}
}

fn to_prompt_text(&self) -> String {
fn to_prompt_text(self) -> String {
let result = match self {
GPTLenght::Short => "20 words",
GPTLenght::Medium => "50 words",
Expand Down
20 changes: 9 additions & 11 deletions src/openai/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use futures::future::join;
use grammers_client::types::Chat;
use grammers_client::Client;
use tokio::sync::RwLock;
use tokio::sync::{Mutex, RwLock};

use crate::consts;
use crate::db::Db;
Expand All @@ -13,7 +13,7 @@ pub use super::api::GPTLenght;

pub struct Processor {
client: Client,
db: Arc<RwLock<Db>>,
db: Arc<Mutex<Db>>,
openai: OpenAIClient,
}

Expand All @@ -40,7 +40,7 @@ struct CommandResult {

impl Processor {
// Creates processor and writing stream
pub fn new(client: Client, db: Arc<RwLock<Db>>, openai: OpenAIClient) -> Self {
pub fn new(client: Client, db: Arc<Mutex<Db>>, openai: OpenAIClient) -> Self {
Self { client, db, openai }
}

Expand Down Expand Up @@ -109,7 +109,7 @@ impl Processor {
}
}
};
return (join(msg_handler, processor), tx);
(join(msg_handler, processor), tx)
}

async fn process_command(&mut self, command: Command) -> anyhow::Result<CommandResult> {
Expand Down Expand Up @@ -173,7 +173,7 @@ impl Processor {
let chat = &chat;
let messages_id_to_load: Vec<i32> = self
.db
.read()
.lock()
.await
.get_messages_id(chat.id(), message_count)?;
let mut messages = Vec::with_capacity(message_count as usize);
Expand All @@ -190,11 +190,9 @@ impl Processor {
.flatten()
.filter(|message| {
if let Some(mentioned_by_user) = mentioned_by_user.as_ref() {
if let Some(sender) = message.sender() {
if let Chat::User(user) = sender {
if user.username() == Some(mentioned_by_user) {
return true;
}
if let Some(Chat::User(user)) = message.sender() {
if user.username() == Some(mentioned_by_user) {
return true;
}
}
return false;
Expand Down Expand Up @@ -226,7 +224,7 @@ impl Processor {
Command::SendPrompt {
recipient: recipient.clone(),
prompt,
gpt_length: gpt_length,
gpt_length,
}
})
.collect();
Expand Down
12 changes: 6 additions & 6 deletions src/telegram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use grammers_client::{
types::{Chat, Message, User},
Client, Update,
};
use tokio::sync::RwLock;
use tokio::sync::Mutex;

fn usage() -> String {
format!("Usage: ./summarize <number of messages to summarize>
Expand All @@ -21,15 +21,15 @@ use crate::{

pub struct Processor {
client: Client,
db: Arc<RwLock<Db>>,
db: Arc<Mutex<Db>>,
sender_channel: tokio::sync::mpsc::Sender<Command>,
me: User,
}

impl Processor {
pub async fn new(
client: Client,
db: Arc<RwLock<Db>>,
db: Arc<Mutex<Db>>,
sender: tokio::sync::mpsc::Sender<Command>,
) -> anyhow::Result<Self> {
let me = client.get_me().await?;
Expand Down Expand Up @@ -61,7 +61,7 @@ impl Processor {
async fn process_message(&mut self, message: Message) -> anyhow::Result<()> {
let mut splitted_string = message.text().split_whitespace();
let (cmd, bot_name) = if let Some(text) = splitted_string.next() {
let mut split = text.split("@");
let mut split = text.split('@');
let cmd = split.next().unwrap_or("");
let bot_name = split.next();
(cmd, bot_name)
Expand Down Expand Up @@ -91,10 +91,10 @@ impl Processor {
_ => unreachable!(),
};
self.summarize(message, length).await?;
} else if cmd.starts_with("/") || is_bot {
} else if cmd.starts_with('/') || is_bot {
} else {
self.db
.write()
.lock()
.await
.add_message_id(message.chat().id(), message.id())?;
}
Expand Down

0 comments on commit 5285f5e

Please sign in to comment.