#![allow(clippy::type_complexity)]
#[cfg(feature = "async")]
pub mod async_session;
#[cfg(not(feature = "async"))]
pub mod sync_session;
use std::io::{Read, Write};
use crate::{
interact::{InteractSession, NoAction, NoFilter},
process::Process,
stream::log::LoggedStream,
Error,
};
#[cfg(feature = "async")]
use crate::process::IntoAsyncStream;
#[cfg(unix)]
pub(crate) type Proc = crate::process::unix::UnixProcess;
#[cfg(all(unix, not(feature = "async")))]
pub(crate) type Stream = crate::process::unix::PtyStream;
#[cfg(all(unix, feature = "async"))]
pub(crate) type Stream = crate::process::unix::AsyncPtyStream;
#[cfg(windows)]
pub(crate) type Proc = crate::process::windows::WinProcess;
#[cfg(all(windows, not(feature = "async")))]
pub(crate) type Stream = crate::process::windows::ProcessStream;
#[cfg(all(windows, feature = "async"))]
pub(crate) type Stream = crate::process::windows::AsyncProcessStream;
#[cfg(not(feature = "async"))]
pub type Session<P = Proc, S = Stream> = sync_session::Session<P, S>;
#[cfg(feature = "async")]
pub type Session<P = Proc, S = Stream> = async_session::Session<P, S>;
impl Session {
pub fn spawn(command: <Proc as Process>::Command) -> Result<Self, Error> {
let mut process = Proc::spawn_command(command)?;
let stream = process.open_stream()?;
#[cfg(feature = "async")]
let stream = stream.into_async_stream()?;
let session = Self::new(process, stream)?;
Ok(session)
}
pub(crate) fn spawn_cmd(cmd: &str) -> Result<Self, Error> {
let mut process = Proc::spawn(cmd)?;
let stream = process.open_stream()?;
#[cfg(feature = "async")]
let stream = stream.into_async_stream()?;
let session = Self::new(process, stream)?;
Ok(session)
}
}
#[cfg(not(feature = "async"))]
impl<P, S: Read> Session<P, S> {
pub fn with_log<W: Write>(self, logger: W) -> Result<Session<P, LoggedStream<S, W>>, Error> {
self.swap_stream(|stream| LoggedStream::new(stream, logger))
}
}
#[cfg(feature = "async")]
impl<P, S> Session<P, S> {
pub fn with_log<W: Write>(self, logger: W) -> Result<Session<P, LoggedStream<S, W>>, Error> {
self.swap_stream(|stream| LoggedStream::new(stream, logger))
}
}
impl<P, S> Session<P, S> {
#[cfg_attr(
all(unix, not(feature = "async"), not(feature = "polling")),
doc = "```no_run"
)]
#[cfg_attr(
not(all(unix, not(feature = "async"), not(feature = "polling"))),
doc = "```ignore"
)]
pub fn interact<I, O>(
&mut self,
input: I,
output: O,
) -> InteractSession<
(),
Self,
O,
I,
NoFilter,
NoFilter,
NoAction<Self, O, ()>,
NoAction<Self, O, ()>,
NoAction<Self, O, ()>,
> {
InteractSession::new(self, output, input, ())
}
}