-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ::= src/reading/read_path_buf_like.rs
- Loading branch information
1 parent
2d98d1b
commit b18f0ce
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/*********************** GNU General Public License 3.0 ***********************\ | ||
| | | ||
| Copyright (C) 2024 Kevin Matthes | | ||
| | | ||
| This program is free software: you can redistribute it and/or modify | | ||
| it under the terms of the GNU General Public License as published by | | ||
| the Free Software Foundation, either version 3 of the License, or | | ||
| (at your option) any later version. | | ||
| | | ||
| This program is distributed in the hope that it will be useful, | | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | | ||
| GNU General Public License for more details. | | ||
| | | ||
| You should have received a copy of the GNU General Public License | | ||
| along with this program. If not, see <https://www.gnu.org/licenses/>. | | ||
| | | ||
\******************************************************************************/ | ||
|
||
use std::path::PathBuf; | ||
use sysexits::Result; | ||
|
||
/// Read from files given as instances convertible to a [`std::path::PathBuf`]. | ||
pub trait ReadPathBufLike { | ||
/// Read from the file this method is called on. | ||
/// | ||
/// The instance this method is called on needs to be convertible to a | ||
/// [`std::path::PathBuf`]. The return value either contains the read file | ||
/// contents as a [`String`] in case of success or a [`sysexits::ExitCode`] | ||
/// to describe the error cause, otherwise. Error messages are not written | ||
/// to [`std::io::Stderr`]. | ||
/// | ||
/// # Errors | ||
/// | ||
/// See [`sysexits::ExitCode`]. | ||
fn read(&self) -> Result<String>; | ||
} | ||
|
||
impl<T> ReadPathBufLike for T | ||
where | ||
PathBuf: From<T>, | ||
T: Clone, | ||
{ | ||
fn read(&self) -> Result<String> { | ||
let path = PathBuf::from(self.clone()); | ||
|
||
match std::fs::read_to_string(path) { | ||
Ok(s) => Ok(s), | ||
Err(e) => Err(e.into()), | ||
} | ||
} | ||
} | ||
|
||
/******************************************************************************/ |