nethsm_cli/
passphrase_file.rs1use std::{
4 fs::read_to_string,
5 path::{Path, PathBuf},
6 str::FromStr,
7};
8
9use nethsm::Passphrase;
10
11#[derive(Debug, thiserror::Error)]
13pub enum Error {
14 #[error("I/O error: {0}")]
16 Io(#[from] std::io::Error),
17 #[error("Path error: {0}")]
19 Path(#[from] core::convert::Infallible),
20}
21
22#[derive(Clone, Debug)]
24pub struct PassphraseFile {
25 pub passphrase: Passphrase,
27}
28
29impl PassphraseFile {
30 pub fn new(path: &Path) -> Result<Self, Error> {
36 Ok(Self {
37 passphrase: Passphrase::new(read_to_string(path)?),
38 })
39 }
40}
41
42impl FromStr for PassphraseFile {
43 type Err = Error;
44
45 fn from_str(s: &str) -> Result<Self, Self::Err> {
46 PassphraseFile::new(&PathBuf::from_str(s)?)
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use std::fs::File;
53 use std::io::Write;
54
55 use rand::{RngExt, rng};
56 use rstest::rstest;
57 use testdir::testdir;
58 use testresult::TestResult;
59
60 use super::*;
61
62 #[rstest]
63 fn passphrase_file() -> TestResult {
64 let mut i = 0;
65 while i < 20 {
66 let lines = rng().random_range(0..20);
67 let lines_vec = (0..lines)
68 .map(|_x| "this is a passphrase".to_string())
69 .collect::<Vec<String>>();
70 let path = testdir!().join(format!("passphrase_file_lines_{lines}.txt"));
71 let mut file = File::create(&path)?;
72 file.write_all(lines_vec.join("\n").as_bytes())?;
73
74 let passphrase_file = PassphraseFile::new(&path);
75 assert!(passphrase_file.is_ok());
76 i += 1;
77 }
78
79 Ok(())
80 }
81}