1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! View files' content

use super::edit::FileContent;
use anyhow::{Context, Result};
use std::io::Read;

/// View methods
pub struct View;

impl View {
    /// Get passwords.toml content and print out
    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(())
    }
    /// Get all matches from passwords.toml using pattern and print out
    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(())
    }
}