use super::edit::FileContent;
use anyhow::{Context, Result};
use std::io::Read;
pub struct View;
impl View {
pub fn view_all() -> Result<()> {
let mut password_file = {
FileContent::default()
.context("Failed to open files")?
.password_file
};
let mut password_string = String::new();
password_file
.read_to_string(&mut password_string)
.context("Failed to write 'passwords.toml' content to string")?;
let password_split = password_string.split('\n');
let password_vector: Vec<String> = password_split.map(|s| s.to_string()).collect();
for line in password_vector {
println!("{}", line.as_str());
}
Ok(())
}
pub fn view_password(pattern: String) -> Result<()> {
let mut password_file = {
FileContent::default()
.context("Failed to open files")?
.password_file
};
let mut password_string = String::new();
password_file
.read_to_string(&mut password_string)
.context("Failed to write 'passwords.toml' content to string")?;
let password_split = password_string.split('\n');
let password_vector: Vec<String> = password_split.map(|s| s.to_string()).collect();
for line in password_vector {
if line.contains(pattern.as_str())
&& !(line
.chars()
.nth(0)
.context("Failed to get first character of line")?
== '[')
{
println!("{}", line.as_str())
}
}
Ok(())
}
}