#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]
pub use bmux_session_models as models;
pub use models::{
ClientError, ClientId, ClientInfo, PaneError, PaneId, Session, SessionError, SessionId,
SessionInfo, WindowError, WindowId,
};
use anyhow::Result;
use std::collections::BTreeMap;
use tracing::warn;
#[derive(Debug, Default)]
pub struct SessionManager {
sessions: BTreeMap<SessionId, Session>,
}
impl SessionManager {
#[must_use]
pub const fn new() -> Self {
Self {
sessions: BTreeMap::new(),
}
}
pub fn create_session(&mut self, name: Option<String>) -> Result<SessionId> {
let session = Session::new(name);
let id = session.id;
self.sessions.insert(id, session);
Ok(id)
}
pub fn insert_session(&mut self, session: Session) -> Result<()> {
let id = session.id;
if self.sessions.contains_key(&id) {
return Err(anyhow::anyhow!("Session already exists: {id}"));
}
self.sessions.insert(id, session);
Ok(())
}
#[must_use]
pub fn get_session(&self, session_id: &SessionId) -> Option<&Session> {
self.sessions.get(session_id)
}
pub fn get_session_mut(&mut self, session_id: &SessionId) -> Option<&mut Session> {
self.sessions.get_mut(session_id)
}
#[must_use]
pub fn list_sessions(&self) -> Vec<SessionInfo> {
self.sessions.values().map(Into::into).collect()
}
pub fn remove_session(&mut self, session_id: &SessionId) -> Result<()> {
if self.sessions.remove(session_id).is_some() {
Ok(())
} else {
Err(anyhow::anyhow!("Session not found: {}", session_id))
}
}
#[must_use]
pub fn session_count(&self) -> usize {
self.sessions.len()
}
}