pub enum OutputArg {
Stdout,
Path(PathBuf),
}
Expand description
An output path that can refer to either standard output or a file system path
Variants§
Stdout
Refers to standard output.
This is the variant returned by OutputArg::default()
.
Path(PathBuf)
Refers to a file system path (stored in .0
)
Implementations§
Source§impl OutputArg
impl OutputArg
Sourcepub fn from_arg<S: Into<PathBuf>>(arg: S) -> OutputArg
pub fn from_arg<S: Into<PathBuf>>(arg: S) -> OutputArg
Construct a OutputArg
from a string, usually one taken from
command-line arguments. If the string equals "-"
(i.e., it contains
only a single hyphen/dash), OutputArg::Stdout
is returned;
otherwise, an OutputArg::Path
is returned.
§Example
use patharg::OutputArg;
use std::path::PathBuf;
let p1 = OutputArg::from_arg("-");
assert_eq!(p1, OutputArg::Stdout);
let p2 = OutputArg::from_arg("./-");
assert_eq!(p2, OutputArg::Path(PathBuf::from("./-")));
Sourcepub fn is_stdout(&self) -> bool
pub fn is_stdout(&self) -> bool
Returns true if the output arg is the Stdout
variant of OutputArg
.
§Example
use patharg::OutputArg;
let p1 = OutputArg::from_arg("-");
assert!(p1.is_stdout());
let p2 = OutputArg::from_arg("file.txt");
assert!(!p2.is_stdout());
Sourcepub fn is_path(&self) -> bool
pub fn is_path(&self) -> bool
Returns true if the output arg is the Path
variant of OutputArg
.
§Example
use patharg::OutputArg;
let p1 = OutputArg::from_arg("-");
assert!(!p1.is_path());
let p2 = OutputArg::from_arg("file.txt");
assert!(p2.is_path());
Sourcepub fn path_ref(&self) -> Option<&PathBuf>
pub fn path_ref(&self) -> Option<&PathBuf>
Retrieve a reference to the inner PathBuf
. If the output arg is
the Stdout
variant, this returns None
.
§Example
use patharg::OutputArg;
use std::path::PathBuf;
let p1 = OutputArg::from_arg("-");
assert_eq!(p1.path_ref(), None);
let p2 = OutputArg::from_arg("file.txt");
assert_eq!(p2.path_ref(), Some(&PathBuf::from("file.txt")));
Sourcepub fn path_mut(&mut self) -> Option<&mut PathBuf>
pub fn path_mut(&mut self) -> Option<&mut PathBuf>
Retrieve a mutable reference to the inner PathBuf
. If the output
arg is the Stdout
variant, this returns None
.
§Example
use patharg::OutputArg;
use std::path::PathBuf;
let mut p1 = OutputArg::from_arg("-");
assert_eq!(p1.path_mut(), None);
let mut p2 = OutputArg::from_arg("file.txt");
assert_eq!(p2.path_mut(), Some(&mut PathBuf::from("file.txt")));
Sourcepub fn into_path(self) -> Option<PathBuf>
pub fn into_path(self) -> Option<PathBuf>
Consume the output arg and return the inner PathBuf
. If the output
arg is the Stdout
variant, this returns None
.
§Example
use patharg::OutputArg;
use std::path::PathBuf;
let p1 = OutputArg::from_arg("-");
assert_eq!(p1.into_path(), None);
let p2 = OutputArg::from_arg("file.txt");
assert_eq!(p2.into_path(), Some(PathBuf::from("file.txt")));
Sourcepub fn create(&self) -> Result<OutputArgWriter>
pub fn create(&self) -> Result<OutputArgWriter>
Open the output arg for writing.
If the output arg is the Stdout
variant, this returns a locked
reference to stdout. Otherwise, if the output arg is a Path
variant,
the given path is opened for writing; if the path does not exist, it is
created.
The returned writer implements std::io::Write
.
§Errors
Has the same error conditions as std::fs::File::create
.
§Example
use patharg::OutputArg;
use std::env::args_os;
use std::io::{self, Write};
fn main() -> io::Result<()> {
let outfile = args_os().nth(1)
.map(OutputArg::from_arg)
.unwrap_or_default();
let mut f = outfile.create()?;
// The "{}" is replaced by either the output filepath or a hyphen.
write!(&mut f, "I am writing to {}.", outfile)?;
Ok(())
}
Sourcepub fn write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()>
pub fn write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()>
Write a slice as the entire contents of the output arg.
If the output arg is the Stdout
variant, the given data is written to
stdout. Otherwise, if the output arg is a Path
variant, the contents
of the given path are replaced with the given data; if the path does
not exist, it is created first.
§Errors
Has the same error conditions as std::io::Write::write_all
and
std::fs::write
.
§Example
use patharg::OutputArg;
use std::env::args_os;
use std::io;
fn main() -> io::Result<()> {
let outfile = args_os().nth(1)
.map(OutputArg::from_arg)
.unwrap_or_default();
outfile.write("This is the output arg's new content.\n")?;
Ok(())
}
Source§impl OutputArg
impl OutputArg
Sourcepub async fn async_create(&self) -> Result<AsyncOutputArgWriter>
Available on crate feature tokio
only.
pub async fn async_create(&self) -> Result<AsyncOutputArgWriter>
tokio
only.Asynchronously open the output arg for writing.
If the output arg is the Stdout
variant, this returns a reference to
stdout. Otherwise, if the output arg is a Path
variant, the given
path is opened for writing; if the path does not exist, it is created.
The returned writer implements tokio::io::AsyncWrite
.
§Errors
Has the same error conditions as tokio::fs::File::create
.
§Example
use patharg::OutputArg;
use std::env::args_os;
use tokio::io::AsyncWriteExt;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let outfile = args_os().nth(1)
.map(OutputArg::from_arg)
.unwrap_or_default();
let mut f = outfile.async_create().await?;
// The "{}" is replaced by either the output filepath or a hyphen.
let msg = format!("I am writing to {}.\n", outfile);
f.write_all(msg.as_ref()).await?;
Ok(())
}
Sourcepub async fn async_write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()>
Available on crate feature tokio
only.
pub async fn async_write<C: AsRef<[u8]>>(&self, contents: C) -> Result<()>
tokio
only.Asynchronously write a slice as the entire contents of the output arg.
If the output arg is the Stdout
variant, the given data is written to
stdout. Otherwise, if the output arg is a Path
variant, the contents
of the given path are replaced with the given data; if the path does
not exist, it is created first.
§Errors
Has the same error conditions as
tokio::io::AsyncWriteExt::write_all
and tokio::fs::write
.
§Example
use patharg::OutputArg;
use std::env::args_os;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let outfile = args_os().nth(1)
.map(OutputArg::from_arg)
.unwrap_or_default();
outfile.async_write("This is the output arg's new content.\n").await?;
Ok(())
}
Trait Implementations§
Source§impl<'de> Deserialize<'de> for OutputArg
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for OutputArg
serde
only.Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserializes a PathBuf
and converts it to an OutputArg
with
OutputArg::from_arg()
.
Source§impl Display for OutputArg
impl Display for OutputArg
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Displays OutputArg::Stdout
as -
(a single hyphen/dash) or as
<stdout>
if the {:#}
format is used. Always displays
OutputArg::Path
using std::path::Path::display()
.
Source§impl Ord for OutputArg
impl Ord for OutputArg
Source§impl PartialOrd for OutputArg
impl PartialOrd for OutputArg
Source§impl Serialize for OutputArg
Available on crate feature serde
only.
impl Serialize for OutputArg
serde
only.Source§fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
Serializes OutputArg::Stdout
as "-"
(a string containing a single
hyphen/dash). Serializes OutputArg::Path
as the inner PathBuf
;
this will fail if the path is not valid UTF-8.
impl Eq for OutputArg
impl StructuralPartialEq for OutputArg
Auto Trait Implementations§
impl Freeze for OutputArg
impl RefUnwindSafe for OutputArg
impl Send for OutputArg
impl Sync for OutputArg
impl Unpin for OutputArg
impl UnwindSafe for OutputArg
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more