Skip to content

Commit

Permalink
refactor: Use special new type for session ids instead of i32
Browse files Browse the repository at this point in the history
  • Loading branch information
PotentialStyx committed Apr 13, 2024
1 parent f806799 commit 539a26f
Show file tree
Hide file tree
Showing 22 changed files with 173 additions and 130 deletions.
6 changes: 3 additions & 3 deletions services/src/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub struct Chat {
history: Vec<goval::ChatMessage>,
}

use crate::{ClientInfo, IPCMessage, SendSessions};
use crate::{ClientInfo, IPCMessage, SendSessions, Session};

use super::traits;
use anyhow::{format_err, Result};
Expand All @@ -21,7 +21,7 @@ impl traits::Service for Chat {
&mut self,
info: &super::types::ChannelInfo,
message: goval::Command,
session: i32,
session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down Expand Up @@ -51,7 +51,7 @@ impl traits::Service for Chat {
&mut self,
_info: &super::types::ChannelInfo,
_client: ClientInfo,
_session: i32,
_session: Session,
_sender: tokio::sync::mpsc::UnboundedSender<IPCMessage>,
) -> Result<Option<goval::Command>> {
let mut scrollback = goval::Command::default();
Expand Down
4 changes: 3 additions & 1 deletion services/src/dotreplit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub struct DotReplit {}
use crate::Session;

use super::traits;

use anyhow::{format_err, Result};
Expand All @@ -10,7 +12,7 @@ impl traits::Service for DotReplit {
&mut self,
_info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
4 changes: 2 additions & 2 deletions services/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub struct Exec {
current_ref: String,
}

use crate::Proc;
use crate::{Proc, Session};

use super::traits;
use anyhow::{format_err, Result};
Expand All @@ -16,7 +16,7 @@ impl traits::Service for Exec {
&mut self,
info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
4 changes: 2 additions & 2 deletions services/src/fsevents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub struct FSEvents {
}

use super::traits;
use crate::{FSEvent, FSWatcher};
use crate::{FSEvent, FSWatcher, Session};
use anyhow::{format_err, Result};
use goval::{Command, File, FileEvent};
use tracing::{error, trace, warn};
Expand All @@ -30,7 +30,7 @@ impl traits::Service for FSEvents {
&mut self,
_info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
4 changes: 3 additions & 1 deletion services/src/gcsfiles.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub struct GCSFiles {}

use crate::Session;

use super::traits;
use anyhow::{format_err, Result};
use async_trait::async_trait;
Expand All @@ -12,7 +14,7 @@ impl traits::Service for GCSFiles {
&mut self,
_info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
8 changes: 4 additions & 4 deletions services/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub struct Git {
}
use std::collections::HashMap;

use crate::ReplspaceMessage;
use crate::{ReplspaceMessage, Session};

use super::traits;
use anyhow::{format_err, Result};
Expand All @@ -17,7 +17,7 @@ impl traits::Service for Git {
&mut self,
_info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down Expand Up @@ -61,10 +61,10 @@ impl traits::Service for Git {
&mut self,
info: &super::types::ChannelInfo,
msg: ReplspaceMessage,
session: i32,
session: Session,
respond: Option<Sender<ReplspaceMessage>>,
) -> Result<()> {
if session == 0 {
if session == Session::zero() {
warn!(
?msg,
"Got replspace message from an unknown session, ignoring"
Expand Down
6 changes: 3 additions & 3 deletions services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Channel {

// Private functions
impl Channel {
async fn message(&mut self, message: goval::Command, session: i32) -> Result<()> {
async fn message(&mut self, message: goval::Command, session: Session) -> Result<()> {
if let Some(mut msg) = self
._inner
.message(&self.info, message.clone(), session)
Expand All @@ -130,7 +130,7 @@ impl Channel {

async fn attach(
&mut self,
session: i32,
session: Session,
client: ClientInfo,
sender: tokio::sync::mpsc::UnboundedSender<IPCMessage>,
) -> Result<()> {
Expand All @@ -149,7 +149,7 @@ impl Channel {
Ok(())
}

async fn detach(&mut self, session: i32) -> Result<()> {
async fn detach(&mut self, session: Session) -> Result<()> {
self.info.sessions.retain(|sess, _| sess != &session);
self.info.clients.retain(|sess, _| sess != &session);
self._inner.detach(&self.info, session).await?;
Expand Down
6 changes: 3 additions & 3 deletions services/src/ot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
time::{Duration, SystemTime, UNIX_EPOCH},
};

use crate::{client::ClientInfo, fs_watcher::FSWatcher, FSEvent, IPCMessage};
use crate::{client::ClientInfo, fs_watcher::FSWatcher, FSEvent, IPCMessage, Session};

use super::traits;
use anyhow::{format_err, Result};
Expand Down Expand Up @@ -48,7 +48,7 @@ impl traits::Service for OT {
&mut self,
info: &super::types::ChannelInfo,
message: goval::Command,
session: i32,
session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down Expand Up @@ -384,7 +384,7 @@ impl traits::Service for OT {
&mut self,
_info: &super::types::ChannelInfo,
_client: ClientInfo,
_session: i32,
_session: Session,
_sender: tokio::sync::mpsc::UnboundedSender<IPCMessage>,
) -> Result<Option<goval::Command>> {
if self.path.is_empty() {
Expand Down
8 changes: 4 additions & 4 deletions services/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tracing::{debug, warn};

use super::traits;
use super::types::pty::Pty;
use crate::{ClientInfo, IPCMessage};
use crate::{ClientInfo, IPCMessage, Session};
use anyhow::{format_err, Result};

#[async_trait]
Expand All @@ -25,7 +25,7 @@ impl traits::Service for Output {
&mut self,
info: &super::types::ChannelInfo,
_client: ClientInfo,
session: i32,
session: Session,
sender: tokio::sync::mpsc::UnboundedSender<IPCMessage>,
) -> Result<Option<goval::Command>> {
if let Some(pty) = &mut self.pty {
Expand Down Expand Up @@ -58,7 +58,7 @@ impl traits::Service for Output {
Ok(Some(status))
}

async fn detach(&mut self, _info: &super::types::ChannelInfo, session: i32) -> Result<()> {
async fn detach(&mut self, _info: &super::types::ChannelInfo, session: Session) -> Result<()> {
if let Some(pty) = &mut self.pty {
pty.session_leave(session).await?;
}
Expand All @@ -69,7 +69,7 @@ impl traits::Service for Output {
&mut self,
info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
29 changes: 16 additions & 13 deletions services/src/presence.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub struct Presence {
users: Vec<goval::User>,
files: HashMap<i32, goval::FileOpened>,
files: HashMap<Session, goval::FileOpened>,
}
use crate::{ClientInfo, IPCMessage, SendSessions};
use crate::{ClientInfo, IPCMessage, SendSessions, Session};
use std::{
collections::HashMap,
time::{SystemTime, UNIX_EPOCH},
Expand All @@ -28,7 +28,7 @@ impl traits::Service for Presence {
&mut self,
info: &super::types::ChannelInfo,
message: goval::Command,
session: i32,
session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand All @@ -39,25 +39,28 @@ impl traits::Service for Presence {
goval::command::Body::FollowUser(follow) => {
let follow_notif = goval::Command {
body: Some(goval::command::Body::FollowUser(goval::FollowUser {
session,
session: session.into(),
})),
..Default::default()
};

info.send(follow_notif, SendSessions::Only(follow.session))
info.send(follow_notif, SendSessions::Only(Session(follow.session)))
.await?;
Ok(None)
}
goval::command::Body::UnfollowUser(unfollow) => {
let unfollow_notif = goval::Command {
body: Some(goval::command::Body::UnfollowUser(goval::UnfollowUser {
session,
session: session.into(),
})),
..Default::default()
};

info.send(unfollow_notif, SendSessions::Only(unfollow.session))
.await?;
info.send(
unfollow_notif,
SendSessions::Only(Session(unfollow.session)),
)
.await?;
Ok(None)
}
goval::command::Body::OpenFile(file) => {
Expand All @@ -76,7 +79,7 @@ impl traits::Service for Presence {
let _inner = goval::FileOpened {
user_id: user.id,
file: file.file,
session,
session: session.into(),
timestamp,
};

Expand All @@ -98,7 +101,7 @@ impl traits::Service for Presence {
&mut self,
info: &super::types::ChannelInfo,
client: ClientInfo,
session: i32,
session: Session,
_sender: tokio::sync::mpsc::UnboundedSender<IPCMessage>,
) -> Result<Option<goval::Command>> {
let mut roster = goval::Command::default();
Expand All @@ -115,7 +118,7 @@ impl traits::Service for Presence {
roster.body = Some(goval::command::Body::Roster(_inner));

let user = goval::User {
session,
session: session.into(),
id: client.id,
name: client.username,
..Default::default()
Expand All @@ -134,14 +137,14 @@ impl traits::Service for Presence {
Ok(Some(roster))
}

async fn detach(&mut self, info: &super::types::ChannelInfo, session: i32) -> Result<()> {
async fn detach(&mut self, info: &super::types::ChannelInfo, session: Session) -> Result<()> {
self.files.remove(&session);
let mut part = goval::Command::default();
let mut flag = false;

let users = self.users.clone();
for (idx, user) in users.iter().enumerate() {
if user.session == session {
if user.session == session.0 {
flag = true;
part.body = Some(goval::command::Body::Part(user.clone()));
self.users.swap_remove(idx);
Expand Down
8 changes: 4 additions & 4 deletions services/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::debug;

use super::traits;
use super::types::pty::Pty;
use crate::{ClientInfo, IPCMessage};
use crate::{ClientInfo, IPCMessage, Session};
use anyhow::{format_err, Result};

#[async_trait]
Expand All @@ -18,14 +18,14 @@ impl traits::Service for Shell {
&mut self,
_info: &super::types::ChannelInfo,
_client: ClientInfo,
session: i32,
session: Session,
sender: tokio::sync::mpsc::UnboundedSender<IPCMessage>,
) -> Result<Option<goval::Command>> {
self.pty.session_join(session, sender).await?;
Ok(None)
}

async fn detach(&mut self, _info: &super::types::ChannelInfo, session: i32) -> Result<()> {
async fn detach(&mut self, _info: &super::types::ChannelInfo, session: Session) -> Result<()> {
self.pty.session_leave(session).await?;
Ok(())
}
Expand All @@ -34,7 +34,7 @@ impl traits::Service for Shell {
&mut self,
_info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
4 changes: 3 additions & 1 deletion services/src/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub struct Snapshot {}

use crate::Session;

use super::traits;
use anyhow::{format_err, Result};
use async_trait::async_trait;
Expand All @@ -10,7 +12,7 @@ impl traits::Service for Snapshot {
&mut self,
_info: &super::types::ChannelInfo,
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
4 changes: 3 additions & 1 deletion services/src/toolchain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
pub struct Toolchain {}
use crate::Session;

use super::traits;
use async_trait::async_trait;
use tracing::debug;
Expand All @@ -11,7 +13,7 @@ impl traits::Service for Toolchain {
&mut self,
_info: &super::types::ChannelInfo, // TODO: use this to give real toolchain info
message: goval::Command,
_session: i32,
_session: Session,
) -> Result<Option<goval::Command>> {
let body = match message.body.clone() {
None => return Err(format_err!("Expected command body")),
Expand Down
Loading

0 comments on commit 539a26f

Please sign in to comment.