pub enum InputArg {
Stdin,
Path(PathBuf),
}
Expand description
An input path that can refer to either standard input or a file system path
Variants§
Stdin
Refers to standard input.
This is the variant returned by InputArg::default()
.
Path(PathBuf)
Refers to a file system path (stored in .0
)
Implementations§
source§impl InputArg
impl InputArg
sourcepub fn from_arg<S: Into<PathBuf>>(arg: S) -> InputArg
pub fn from_arg<S: Into<PathBuf>>(arg: S) -> InputArg
Construct an InputArg
from a string, usually one taken from
command-line arguments. If the string equals "-"
(i.e., it contains
only a single hyphen/dash), InputArg::Stdin
is returned; otherwise,
an InputArg::Path
is returned.
Example
use patharg::InputArg;
use std::path::PathBuf;
let p1 = InputArg::from_arg("-");
assert_eq!(p1, InputArg::Stdin);
let p2 = InputArg::from_arg("./-");
assert_eq!(p2, InputArg::Path(PathBuf::from("./-")));
sourcepub fn is_stdin(&self) -> bool
pub fn is_stdin(&self) -> bool
Returns true if the input arg is the Stdin
variant of InputArg
.
Example
use patharg::InputArg;
let p1 = InputArg::from_arg("-");
assert!(p1.is_stdin());
let p2 = InputArg::from_arg("file.txt");
assert!(!p2.is_stdin());
sourcepub fn is_path(&self) -> bool
pub fn is_path(&self) -> bool
Returns true if the input arg is the Path
variant of InputArg
.
Example
use patharg::InputArg;
let p1 = InputArg::from_arg("-");
assert!(!p1.is_path());
let p2 = InputArg::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 input arg is
the Stdin
variant, this returns None
.
Example
use patharg::InputArg;
use std::path::PathBuf;
let p1 = InputArg::from_arg("-");
assert_eq!(p1.path_ref(), None);
let p2 = InputArg::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 input
arg is the Stdin
variant, this returns None
.
Example
use patharg::InputArg;
use std::path::PathBuf;
let mut p1 = InputArg::from_arg("-");
assert_eq!(p1.path_mut(), None);
let mut p2 = InputArg::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 input arg and return the inner PathBuf
. If the input
arg is the Stdin
variant, this returns None
.
Example
use patharg::InputArg;
use std::path::PathBuf;
let p1 = InputArg::from_arg("-");
assert_eq!(p1.into_path(), None);
let p2 = InputArg::from_arg("file.txt");
assert_eq!(p2.into_path(), Some(PathBuf::from("file.txt")));
sourcepub fn open(&self) -> Result<InputArgReader>
pub fn open(&self) -> Result<InputArgReader>
Open the input arg for reading.
If the input arg is the Stdin
variant, this returns a locked
reference to stdin. Otherwise, if the path arg is a Path
variant,
the given path is opened for reading.
The returned reader implements std::io::BufRead
.
Errors
Has the same error conditions as std::fs::File::open
.
Example
use patharg::InputArg;
use std::env::args_os;
use std::io::{self, Read};
fn main() -> io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let mut f = infile.open()?;
let mut buffer = [0; 16];
let n = f.read(&mut buffer)?;
println!("First {} bytes: {:?}", n, &buffer[..n]);
Ok(())
}
sourcepub fn read(&self) -> Result<Vec<u8>>
pub fn read(&self) -> Result<Vec<u8>>
Read the entire contents of the input arg into a bytes vector.
If the input arg is the Stdin
variant, the entire contents of stdin
are read. Otherwise, if the input arg is a Path
variant, the
contents of the given path are read.
Errors
Has the same error conditions as std::io::Read::read_to_end
and
std::fs::read
.
Example
use patharg::InputArg;
use std::env::args_os;
use std::io;
fn main() -> io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let input = infile.read()?;
println!("Read {} bytes from input", input.len());
Ok(())
}
sourcepub fn read_to_string(&self) -> Result<String>
pub fn read_to_string(&self) -> Result<String>
Read the entire contents of the input arg into a string.
If the input arg is the Stdin
variant, the entire contents of stdin
are read. Otherwise, if the input arg is a Path
variant, the
contents of the given path are read.
Errors
Has the same error conditions as std::io::read_to_string
and
std::fs::read_to_string
.
Example
use patharg::InputArg;
use std::env::args_os;
use std::io;
fn main() -> io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let input = infile.read_to_string()?;
println!("Read {} characters from input", input.len());
Ok(())
}
sourcepub fn lines(&self) -> Result<Lines>
pub fn lines(&self) -> Result<Lines>
Return an iterator over the lines of the input arg.
If the input arg is the Stdin
variant, this locks stdin and returns
an iterator over its lines; the lock is released once the iterator is
dropped. Otherwise, if the input arg is a Path
variant, the given
path is opened for reading, and an iterator over its lines is returned.
The returned iterator yields instances of std::io::Result<String>
,
where each individual item has the same error conditions as
std::io::BufRead::read_line()
.
Errors
Has the same error conditions as InputArg::open()
.
Example
use patharg::InputArg;
use std::env::args_os;
use std::io;
fn main() -> io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
for (i, r) in infile.lines()?.enumerate() {
let line = r?;
println!("Line {} is {} characters long.", i + 1, line.len());
}
Ok(())
}
source§impl InputArg
impl InputArg
sourcepub async fn async_open(&self) -> Result<AsyncInputArgReader>
Available on crate feature tokio
only.
pub async fn async_open(&self) -> Result<AsyncInputArgReader>
tokio
only.Asynchronously open the input arg for reading.
If the input arg is the Stdin
variant, this returns a reference to
stdin. Otherwise, if the path arg is a Path
variant, the given path
is opened for reading.
The returned reader implements tokio::io::AsyncRead
.
Errors
Has the same error conditions as tokio::fs::File::open
.
Example
use patharg::InputArg;
use std::env::args_os;
use tokio::io::AsyncReadExt;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let mut f = infile.async_open().await?;
let mut buffer = [0; 16];
let n = f.read(&mut buffer).await?;
println!("First {} bytes: {:?}", n, &buffer[..n]);
Ok(())
}
sourcepub async fn async_read(&self) -> Result<Vec<u8>>
Available on crate feature tokio
only.
pub async fn async_read(&self) -> Result<Vec<u8>>
tokio
only.Asynchronously read the entire contents of the input arg into a bytes vector.
If the input arg is the Stdin
variant, the entire contents of stdin
are read. Otherwise, if the input arg is a Path
variant, the
contents of the given path are read.
Errors
Has the same error conditions as
tokio::io::AsyncReadExt::read_to_end
and tokio::fs::read
.
Example
use patharg::InputArg;
use std::env::args_os;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let input = infile.async_read().await?;
println!("Read {} bytes from input", input.len());
Ok(())
}
sourcepub async fn async_read_to_string(&self) -> Result<String>
Available on crate feature tokio
only.
pub async fn async_read_to_string(&self) -> Result<String>
tokio
only.Asynchronously read the entire contents of the input arg into a string.
If the input arg is the Stdin
variant, the entire contents of stdin
are read. Otherwise, if the input arg is a Path
variant, the
contents of the given path are read.
Errors
Has the same error conditions as
tokio::io::AsyncReadExt::read_to_string
and
tokio::fs::read_to_string
.
Example
use patharg::InputArg;
use std::env::args_os;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let input = infile.async_read_to_string().await?;
println!("Read {} characters from input", input.len());
Ok(())
}
sourcepub async fn async_lines(&self) -> Result<AsyncLines>
Available on crate feature tokio
only.
pub async fn async_lines(&self) -> Result<AsyncLines>
tokio
only.Return a stream over the lines of the input arg.
If the input arg is the Stdin
variant, this returns a stream over the
lines of stdin. Otherwise, if the input arg is a Path
variant, the
given path is opened for reading, and a stream over its lines is
returned.
The returned stream yields instances of std::io::Result<String>
,
where each individual item has the same error conditions as
tokio::io::AsyncBufReadExt::read_line()
.
Errors
Has the same error conditions as InputArg::async_open()
.
Example
use patharg::InputArg;
use std::env::args_os;
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let infile = args_os().nth(1)
.map(InputArg::from_arg)
.unwrap_or_default();
let mut i = 1;
let mut stream = infile.async_lines().await?;
while let Some(r) = stream.next().await {
let line = r?;
println!("Line {} is {} characters long.", i, line.len());
i += 1;
}
Ok(())
}
Trait Implementations§
source§impl<'de> Deserialize<'de> for InputArg
Available on crate feature serde
only.
impl<'de> Deserialize<'de> for InputArg
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 InputArg
with
InputArg::from_arg()
.
source§impl Display for InputArg
impl Display for InputArg
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Displays InputArg::Stdin
as -
(a single hyphen/dash) or as
<stdin>
if the {:#}
format is used. Always displays
InputArg::Path
using std::path::Path::display()
.
source§impl Ord for InputArg
impl Ord for InputArg
source§impl PartialEq for InputArg
impl PartialEq for InputArg
source§impl PartialOrd for InputArg
impl PartialOrd for InputArg
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for InputArg
Available on crate feature serde
only.
impl Serialize for InputArg
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 InputArg::Stdin
as "-"
(a string containing a single
hyphen/dash). Serializes InputArg::Path
as the inner PathBuf
;
this will fail if the path is not valid UTF-8.