test_alex_lib/
lib.rs

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::env;
use std::fs;
use std::fs::File;
use std::io::{ErrorKind, Read};


/// # Exemple
/// read text from file
///
/// # Examples
///
/// ```
///use test_alex_lib::Terminal;
///
///fn main() {
///    let file = Terminal::new();
///    let p =file.find(file.get_index(2));
///    //file.create(file.get_index(1), file.get_index(2));
///    println!("{}", p);
///    
///}
/// ```
#[derive()]
pub struct Terminal {
    value: Vec<String>
}

impl Terminal {
    pub fn new() -> Terminal {
        Terminal {
            value: env::args().collect()
        }
    }

    pub fn get(&self) -> String {
        let output = self.value.iter().next();
        let p  = match output {
            Some(x) => x,
            None => "Error get()"
        };
        p.to_string()
    }

    pub fn get_index(&self, x: usize) -> String {
        let &n = &self.value.get(x);
        let c = match n {
            Some(x) => x,
            None => "Error get_index()"
        };
        c.to_string()
    }

    pub fn is_err(&self, x: usize) -> bool {
        let &n = &self.value.get(x);
        let c = match n {
            Some(_) => false,
            None => true
        };
        c
    }

    pub fn read_file(&self) -> String{
        let mut output = String::new();
        let file_name = self.get_index(1);
        let mut file = File::open(file_name.clone()).unwrap_or_else(|error| {
            if error.kind() == ErrorKind::NotFound {
                panic!("Error in Searching File: {:#?}", error);
            }
            else {
                panic!("Error: {:#?}", error);
            }
        });
        file.read_to_string( &mut output).unwrap();
        output
    }

    pub fn find(&self, word: String) -> String {
        let p = self.read_file().to_ascii_lowercase();
        let out: String = p.lines().filter(|line| line.contains(word.as_str())).collect();
        out
    }

    pub fn create(&self, name: String, text: String) -> std::io::Result<()>{
        File::create(name.clone())?;
        fs::write(name, text)?;
        Ok(())
    }
}