Skip to content

Commit

Permalink
chore: add rustfmt configuration and format codebase
Browse files Browse the repository at this point in the history
- Introduced rustfmt.toml for code formatting settings
- Ran `cargo fmt` to apply formatting to the entire project
  • Loading branch information
unrenamed committed Jul 13, 2024
1 parent edba5d0 commit f88a275
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 45 deletions.
8 changes: 8 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
imports_granularity = "Module"

# The following lines may be uncommented on nightly Rust.
# Once these features have stabilized, they should be added
# to the always-enabled options above.
# wrap_comments = true
# comment_width = 70
# normalize_comments = true
3 changes: 2 additions & 1 deletion src/auth/ban.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use humantime;
use std::{str::FromStr, time::Duration};
use std::str::FromStr;
use std::time::Duration;

#[derive(Debug, PartialEq)]
struct BanDuration(Duration);
Expand Down
7 changes: 4 additions & 3 deletions src/auth/pubkey_file_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,10 @@ mod tests {

#[test]
fn test_save_keys_encode_error() {
// russh_keys::write_public_key_base64 can only fail with IO error
// Since the IO error is already handled when opening the file, we
// cannot mock russh_keys::write_public_key_base64 to fail.
// russh_keys::write_public_key_base64 can only fail with IO
// error Since the IO error is already handled when
// opening the file, we cannot mock
// russh_keys::write_public_key_base64 to fail.
}

#[test]
Expand Down
3 changes: 1 addition & 2 deletions src/chat/room/member.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use chrono::{DateTime, Utc};
use tokio::sync::{mpsc, watch};

use crate::chat::message::Message;
use crate::chat::message::{self, MessageFormatter};
use crate::chat::message::{self, Message, MessageFormatter};
use crate::chat::user::User;

#[derive(Clone)]
Expand Down
4 changes: 3 additions & 1 deletion src/chat/user/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use chrono::{DateTime, Utc};
use rand::seq::SliceRandom;
use rand::Rng;
use russh_keys::key::PublicKey;
use std::{collections::BTreeSet, fmt::Display, time::Duration};
use std::collections::BTreeSet;
use std::fmt::Display;
use std::time::Duration;

use crate::utils;

Expand Down
3 changes: 2 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pub struct Cli {
#[arg(long, default_value_t = 22)]
pub port: u16,

/// Private key to identify server with. Defaults to a temporary ed25519 key
/// Private key to identify server with. Defaults to a temporary
/// ed25519 key
#[arg(short = 'i', long, value_name = "KEY")]
pub identity: Option<String>,

Expand Down
27 changes: 11 additions & 16 deletions src/server/session/handler.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
use std::sync::Arc;

use log::{error, info};
use russh::server::Auth;
use russh::server::Handler;
use russh::server::Msg;
use russh::server::Response;
use russh::server::Session;
use russh::Channel;
use russh::ChannelId;
use russh::MethodSet;
use russh::Pty;
use russh::server::{Auth, Handler, Msg, Response, Session};
use russh::{Channel, ChannelId, MethodSet, Pty};
use russh_keys::key::PublicKey;
use tokio::sync::mpsc::Sender;
use tokio::sync::Mutex;

use crate::auth;
use crate::terminal::TerminalHandle;

use super::SessionEvent;
use super::SessionRepositoryEvent;
use super::{SessionEvent, SessionRepositoryEvent};

/// Server handler. Each client will have their own handler.
pub struct ThinHandler {
Expand Down Expand Up @@ -260,12 +252,15 @@ impl Handler for ThinHandler {

/// Handles cleanup when the connection ends.
///
/// This implementation of `Drop` is primarily designed to gracefully manage unexpected disconnects,
/// such as when a client abruptly kills the connection without sending a disconnect signal.
/// This implementation of `Drop` is primarily designed to gracefully
/// manage unexpected disconnects, such as when a client abruptly
/// kills the connection without sending a disconnect signal.
///
/// Upon dropping the `ThinHandler`, it will check if there is an associated `session_event_sender`.
/// This ensures that the disconnection event is properly handled even if the connectionc termination
/// was before the session is opened (e.g. one of the authenticated methods rejected the connection)
/// Upon dropping the `ThinHandler`, it will check if there is an
/// associated `session_event_sender`. This ensures that the
/// disconnection event is properly handled even if the connectionc
/// termination was before the session is opened (e.g. one of the
/// authenticated methods rejected the connection)
impl Drop for ThinHandler {
fn drop(&mut self) {
if let Some(sender) = &self.session_event_sender {
Expand Down
4 changes: 1 addition & 3 deletions src/server/session/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ mod handler {
use async_trait::async_trait;
use client::Config;
use futures::Future;
use russh::client;
use russh::server;
use russh::MethodSet;
use russh::{client, server, MethodSet};
use russh_keys::key::{KeyPair, PublicKey};
use server::{Auth, Config as ServerConfig, Handler};
use std::sync::Arc;
Expand Down
6 changes: 2 additions & 4 deletions src/server/session_workflow/autocomplete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ use super::handler::WorkflowHandler;
use super::WorkflowContext;

use crate::auth::Auth;
use crate::chat::ChatRoom;
use crate::chat::{
Command, CommandProps, OplistCommand, OplistLoadMode, WhitelistCommand, WhitelistLoadMode,
CHAT_COMMANDS, OPLIST_COMMANDS, WHITELIST_COMMANDS,
ChatRoom, Command, CommandProps, OplistCommand, OplistLoadMode, Theme, TimestampMode,
WhitelistCommand, WhitelistLoadMode, CHAT_COMMANDS, OPLIST_COMMANDS, WHITELIST_COMMANDS,
};
use crate::chat::{Theme, TimestampMode};
use crate::terminal::Terminal;

#[derive(Default)]
Expand Down
8 changes: 3 additions & 5 deletions src/server/session_workflow/command_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ use async_trait::async_trait;
use std::fmt::Write;

use crate::auth::{Auth, BanAttribute, BanQuery};
use crate::chat::message;
use crate::chat::message::Message;
use crate::chat::ChatRoom;
use crate::chat::{
format_commands, Command, CommandProps, OplistCommand, OplistLoadMode, WhitelistCommand,
WhitelistLoadMode, CHAT_COMMANDS, OPLIST_COMMANDS, WHITELIST_COMMANDS,
format_commands, message, ChatRoom, Command, CommandProps, OplistCommand, OplistLoadMode,
Theme, TimestampMode, User, UserStatus, WhitelistCommand, WhitelistLoadMode, CHAT_COMMANDS,
OPLIST_COMMANDS, WHITELIST_COMMANDS,
};
use crate::chat::{Theme, TimestampMode, User, UserStatus};
use crate::terminal::Terminal;
use crate::utils::{self, sanitize};

Expand Down
3 changes: 2 additions & 1 deletion src/terminal/handle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use log::{error, trace};
use russh::{server::Handle, ChannelId};
use russh::server::Handle;
use russh::ChannelId;

#[derive(Clone)]
pub struct TerminalHandle {
Expand Down
12 changes: 8 additions & 4 deletions src/terminal/input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt::Display;
use unicode_segmentation::UnicodeSegmentation;

use super::{input_history::InputHistory, unicode};
use super::input_history::InputHistory;
use super::unicode;

const MAX_HISTORY_SIZE: usize = 20;

Expand All @@ -15,12 +16,14 @@ struct InputState {
cursor_byte_pos: usize,
}

// Struct representing user input with snapshot capability and input history
// Struct representing user input with snapshot capability and input
// history
#[derive(Clone, Debug, Default)]
pub struct TerminalInput {
state: InputState, // Current input state
snapshot: Option<InputState>, // Snapshot of previous state
history: InputHistory<InputState, MAX_HISTORY_SIZE>, // Records the history of inputs made by the user
history: InputHistory<InputState, MAX_HISTORY_SIZE>, /* Records the history of inputs made
* by the user */
}

impl Display for TerminalInput {
Expand Down Expand Up @@ -211,7 +214,8 @@ impl TerminalInput {
}

// Sets the current state to the previous state in the input history.
// If there is no current navigation index in the history, it first takes a snapshot of the current state
// If there is no current navigation index in the history, it first
// takes a snapshot of the current state
pub fn set_history_prev(&mut self) {
if self.history.nav_index().is_none() {
self.make_snapshot();
Expand Down
3 changes: 2 additions & 1 deletion src/terminal/input_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ impl<T: Default + Clone, const N: usize> InputHistory<T, N> {

pub fn push(&mut self, item: T) {
self.history.push_back(item);
self.nav_index = None; // Reset navigation index when a new command is added
self.nav_index = None; // Reset navigation index when a new
// command is added
}

pub fn prev(&mut self) -> Option<&T> {
Expand Down
4 changes: 1 addition & 3 deletions src/terminal/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crossterm::cursor;
use crossterm::queue;
use crossterm::style;
use crossterm::terminal::{Clear, ClearType};
use crossterm::{cursor, queue, style};
use std::io::Write;
use unicode_segmentation::UnicodeSegmentation;

Expand Down

0 comments on commit f88a275

Please sign in to comment.