use crate::lazy_output::FromLazyOutput;
use crate::open_output::{open_output, Output};
use crate::{MediaType, Pseudonym};
use anyhow::anyhow;
use clap::{AmbientAuthority, TryFromOsArg};
use io_streams::StreamWriter;
use layered_io::{Bufferable, LayeredWriter, WriteLayered};
use std::ffi::{OsStr, OsString};
use std::fmt::{self, Arguments, Debug, Formatter};
use std::io::{self, IoSlice, Write};
use terminal_io::{NeverTerminalWriter, TerminalWriter, WriteTerminal};
pub struct OutputByteStream {
name: String,
writer: LayeredWriter<NeverTerminalWriter<StreamWriter>>,
media_type: MediaType,
}
impl OutputByteStream {
#[inline]
pub fn write_pseudonym(&mut self, pseudonym: &Pseudonym) -> io::Result<()> {
Write::write_all(self, pseudonym.name.as_bytes())
}
#[inline]
pub fn pseudonym(&self) -> Pseudonym {
Pseudonym::new(self.name.clone())
}
#[inline]
pub fn media_type(&self) -> &MediaType {
&self.media_type
}
fn from_output(output: Output) -> anyhow::Result<Self> {
let writer = NeverTerminalWriter::new(output.writer);
let writer = TerminalWriter::with_handle(writer);
if writer.is_output_terminal() {
return Err(anyhow!("attempted to write binary output to a terminal"));
}
let writer = LayeredWriter::new(writer.into_inner());
Ok(Self {
name: output.name,
writer,
media_type: output.media_type,
})
}
}
#[doc(hidden)]
impl TryFromOsArg for OutputByteStream {
type Error = anyhow::Error;
#[inline]
fn try_from_os_str_arg(
os: &OsStr,
ambient_authority: AmbientAuthority,
) -> anyhow::Result<Self> {
open_output(os, MediaType::unknown(), ambient_authority).and_then(Self::from_output)
}
}
impl WriteLayered for OutputByteStream {
#[inline]
fn close(&mut self) -> io::Result<()> {
self.writer.close()
}
}
impl Write for OutputByteStream {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.writer.write(buf)
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
self.writer.flush()
}
#[inline]
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
self.writer.write_vectored(bufs)
}
#[cfg(can_vector)]
#[inline]
fn is_write_vectored(&self) -> bool {
self.writer.is_write_vectored()
}
#[inline]
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
self.writer.write_all(buf)
}
#[cfg(write_all_vectored)]
#[inline]
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> {
self.writer.write_all_vectored(bufs)
}
#[inline]
fn write_fmt(&mut self, fmt: Arguments<'_>) -> io::Result<()> {
self.writer.write_fmt(fmt)
}
}
impl Bufferable for OutputByteStream {
#[inline]
fn abandon(&mut self) {
self.writer.abandon()
}
}
impl FromLazyOutput for OutputByteStream {
type Err = anyhow::Error;
fn from_lazy_output(
name: OsString,
media_type: MediaType,
ambient_authority: AmbientAuthority,
) -> Result<Self, anyhow::Error> {
open_output(&name, media_type, ambient_authority).and_then(Self::from_output)
}
}
impl Debug for OutputByteStream {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut b = f.debug_struct("OutputByteStream");
b.field("media_type", &self.media_type);
b.finish()
}
}