use std::path::PathBuf;
use serde::{Deserialize, Serialize};
pub use rmux_proto::{PaneId, SessionId, SessionName, WindowId};
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum RmuxEndpoint {
#[default]
Default,
UnixSocket(PathBuf),
WindowsPipe(String),
}
impl RmuxEndpoint {
#[must_use]
pub fn is_default(&self) -> bool {
matches!(self, Self::Default)
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TerminalSizeSpec {
pub cols: u16,
pub rows: u16,
}
impl TerminalSizeSpec {
#[must_use]
pub const fn new(cols: u16, rows: u16) -> Self {
Self { cols, rows }
}
}
impl From<TerminalSizeSpec> for rmux_proto::TerminalSize {
fn from(value: TerminalSizeSpec) -> Self {
Self {
cols: value.cols,
rows: value.rows,
}
}
}
impl From<rmux_proto::TerminalSize> for TerminalSizeSpec {
fn from(value: rmux_proto::TerminalSize) -> Self {
Self {
cols: value.cols,
rows: value.rows,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct WindowRef {
pub session_name: SessionName,
pub window_index: u32,
}
impl WindowRef {
#[must_use]
pub fn new(session_name: SessionName, window_index: u32) -> Self {
Self {
session_name,
window_index,
}
}
#[must_use]
pub fn first(session_name: SessionName) -> Self {
Self::new(session_name, 0)
}
#[must_use]
pub fn to_proto(&self) -> rmux_proto::WindowTarget {
rmux_proto::WindowTarget::with_window(self.session_name.clone(), self.window_index)
}
}
impl From<WindowRef> for rmux_proto::WindowTarget {
fn from(value: WindowRef) -> Self {
Self::with_window(value.session_name, value.window_index)
}
}
impl From<rmux_proto::WindowTarget> for WindowRef {
fn from(value: rmux_proto::WindowTarget) -> Self {
Self {
session_name: value.session_name().clone(),
window_index: value.window_index(),
}
}
}
impl From<&WindowRef> for rmux_proto::WindowTarget {
fn from(value: &WindowRef) -> Self {
value.to_proto()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PaneRef {
pub session_name: SessionName,
pub window_index: u32,
pub pane_index: u32,
}
impl PaneRef {
#[must_use]
pub fn new(session_name: SessionName, window_index: u32, pane_index: u32) -> Self {
Self {
session_name,
window_index,
pane_index,
}
}
#[must_use]
pub fn in_first_window(session_name: SessionName, pane_index: u32) -> Self {
Self::new(session_name, 0, pane_index)
}
#[must_use]
pub fn to_proto(&self) -> rmux_proto::PaneTarget {
rmux_proto::PaneTarget::with_window(
self.session_name.clone(),
self.window_index,
self.pane_index,
)
}
}
impl From<PaneRef> for rmux_proto::PaneTarget {
fn from(value: PaneRef) -> Self {
Self::with_window(value.session_name, value.window_index, value.pane_index)
}
}
impl From<rmux_proto::PaneTarget> for PaneRef {
fn from(value: rmux_proto::PaneTarget) -> Self {
Self {
session_name: value.session_name().clone(),
window_index: value.window_index(),
pane_index: value.pane_index(),
}
}
}
impl From<&PaneRef> for rmux_proto::PaneTarget {
fn from(value: &PaneRef) -> Self {
value.to_proto()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TargetRef {
Session(SessionName),
Window(WindowRef),
Pane(PaneRef),
}
impl From<TargetRef> for rmux_proto::Target {
fn from(value: TargetRef) -> Self {
match value {
TargetRef::Session(session_name) => Self::Session(session_name),
TargetRef::Window(target) => Self::Window(target.into()),
TargetRef::Pane(target) => Self::Pane(target.into()),
}
}
}
impl From<rmux_proto::Target> for TargetRef {
fn from(value: rmux_proto::Target) -> Self {
match value {
rmux_proto::Target::Session(session_name) => Self::Session(session_name),
rmux_proto::Target::Window(target) => Self::Window(target.into()),
rmux_proto::Target::Pane(target) => Self::Pane(target.into()),
}
}
}
impl From<&TargetRef> for rmux_proto::Target {
fn from(value: &TargetRef) -> Self {
match value {
TargetRef::Session(session_name) => Self::Session(session_name.clone()),
TargetRef::Window(target) => Self::Window(target.into()),
TargetRef::Pane(target) => Self::Pane(target.into()),
}
}
}