#[cfg(feature = "async")]
mod async_session;
#[cfg(not(feature = "async"))]
mod sync_session;
use std::{io::Write, process::Command};
use crate::{interact::InteractSession, process::Process, stream::log::LogStream, Error};
#[cfg(not(feature = "async"))]
use std::io::Read;
#[cfg(feature = "async")]
use crate::process::IntoAsyncStream;
#[cfg(unix)]
type OsProc = crate::process::unix::UnixProcess;
#[cfg(windows)]
type OsProc = crate::process::windows::WinProcess;
#[cfg(all(unix, not(feature = "async")))]
type OsProcStream = crate::process::unix::PtyStream;
#[cfg(all(unix, feature = "async"))]
type OsProcStream = crate::process::unix::AsyncPtyStream;
#[cfg(all(windows, not(feature = "async")))]
type OsProcStream = crate::process::windows::ProcessStream;
#[cfg(all(windows, feature = "async"))]
type OsProcStream = crate::process::windows::AsyncProcessStream;
pub type OsProcess = OsProc;
pub type OsStream = OsProcStream;
pub type OsSession = Session<OsProc, OsStream>;
#[cfg(feature = "async")]
pub use async_session::Session;
#[cfg(not(feature = "async"))]
pub use sync_session::Session;
impl Session<OsProc, OsProcStream> {
pub fn spawn(command: Command) -> Result<Self, Error> {
let mut process = OsProcess::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 = OsProcess::spawn(cmd)?;
let stream = process.open_stream()?;
#[cfg(feature = "async")]
let stream = stream.into_async_stream()?;
let session = Self::new(process, stream)?;
Ok(session)
}
}
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<&mut Self, I, O, ()> {
InteractSession::new(self, input, output, ())
}
}
#[cfg(not(feature = "async"))]
pub fn log<W, P, S>(session: Session<P, S>, dst: W) -> Result<Session<P, LogStream<S, W>>, Error>
where
W: Write,
S: Read,
{
session.swap_stream(|s| LogStream::new(s, dst))
}
#[cfg(feature = "async")]
pub fn log<W, P, S>(session: Session<P, S>, dst: W) -> Result<Session<P, LogStream<S, W>>, Error>
where
W: Write,
{
session.swap_stream(|s| LogStream::new(s, dst))
}