Function cli_utils_hoijui::create_output_writer
source · pub fn create_output_writer<P: AsRef<Path>>(
ident: Option<P>,
) -> Result<Box<dyn Write>>Expand description
Creates a writer from a string identifier.
Both None and Some("-") mean stdout.
See also: write_to_file
§Example
use cli_utils_hoijui::create_output_writer;
let lines = vec!["line 1", "line 2", "line 3"];
let out_file = None as Option<&str>; // writes to stdout
let mut writer = create_output_writer(out_file)?;
let out_file = Some("-"); // writes to stdout
let mut writer = create_output_writer(out_file)?;
let out_file = Some("my_dir/my_file.txt"); // writes to file "$CWD/my_dir/my_file.txt"
let mut writer = create_output_writer(out_file)?;
let path_buf = PathBuf::from_str("my_dir/my_file.txt").expect("This failing should be impossilbe!");
let out_file = Some(path_buf.as_path()); // writes to file "$CWD/my_dir/my_file.txt"
let mut writer = create_output_writer(out_file)?;
let out_file = Some(path_buf); // writes to file "$CWD/my_dir/my_file.txt"
let mut writer = create_output_writer(out_file)?;
for line in lines {
writer.write_all(line.as_bytes())?;
writer.write_all(b"\n")?;
}§Errors
If a file path is specified, and it is not possible to write to it.