File: common.rs

package info (click to toggle)
rust-trawldb 0.2.9-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: makefile: 2
file content (162 lines) | stat: -rw-r--r-- 4,602 bytes parent folder | download | duplicates (2)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use io::Write;
use std::collections::HashMap;
use std::{fs::File, io};
use trawldb::{parser::CliArgs, Client};
use uuid::Uuid;

fn new_tmp_file(contents: &str) -> Result<(File, String), io::Error> {
    let mut tmp_dir = std::env::temp_dir();
    let filename = format!("{}.res", Uuid::new_v4());
    tmp_dir.push(filename);
    let mut file_handle = File::create(tmp_dir.clone())?;
    file_handle.write_all(contents.as_bytes())?;
    file_handle.flush()?;
    Ok((file_handle, tmp_dir.to_str().unwrap().to_string()))
}

fn get_config_str() -> (&'static str, HashMap<String, String>) {
    let config_str = "home_dir:/home
    key_one:val_one\t
    invalid-line's#!
    lockscreen-timeout:10     
    \tscreen-resolution:    1920x1080  
    valid: \"line\"
    \"hello\' : invalid
    /home: invalid
    string\\: invalid";
    let expected_map: HashMap<_, _> = [
        ("home_dir", "/home"),
        ("key_one", "val_one"),
        ("lockscreen-timeout", "10"),
        ("valid", "\"line\""),
        ("screen-resolution", "1920x1080"),
    ]
    .iter()
    .map(|&(k, v)| (k.to_string(), v.to_string()))
    .collect();
    (config_str, expected_map)
}

fn get_file() -> (String, HashMap<String, String>) {
    let (config_str, resources) = get_config_str();
    let (_, path_str) = new_tmp_file(config_str).unwrap();
    (path_str, resources)
}

pub async fn get_resources_prop() -> zbus::Result<HashMap<String, String>> {
    let client = Client::new().await.unwrap();
    client.proxy().resources().await
}

pub async fn arg_filename_default() -> HashMap<String, String> {
    let (file_path, resources) = get_file();
    let args = CliArgs {
        nocpp: false,
        filename: None,
        cpp: None,
        load: Some(file_path),
        merge: None,
        edit: None,
        backup: None,
        get: None,
        query: None,
        remove: false,
        define: vec![],
        include: vec![],
    };
    let client = Client::new().await.unwrap();
    client.run(&args).await.unwrap();
    resources
}

pub async fn arg_load() -> HashMap<String, String> {
    let mut curr_resources = arg_filename_default().await;
    let file_path = new_tmp_file("key_1: val2").unwrap().1;
    curr_resources.insert(String::from("key_1"), String::from("val2"));

    let args = CliArgs {
        nocpp: false,
        filename: None,
        cpp: None,
        load: Some(file_path),
        merge: None,
        edit: None,
        backup: None,
        get: None,
        query: None,
        remove: false,
        define: vec![],
        include: vec![],
    };
    let client = Client::new().await.unwrap();
    client.run(&args).await.unwrap();
    curr_resources
}

pub async fn arg_merge() -> HashMap<String, String> {
    let mut curr_resources = arg_filename_default().await;
    let file = new_tmp_file("key_1: val2").unwrap().1;
    curr_resources.insert(String::from("key_1"), String::from("val2"));
    let args = CliArgs {
        nocpp: false,
        filename: None,
        cpp: None,
        load: None,
        merge: Some(file),
        edit: None,
        backup: None,
        get: None,
        query: None,
        remove: false,
        define: vec![],
        include: vec![],
    };
    let client = Client::new().await.unwrap();
    client.run(&args).await.unwrap();
    curr_resources
}

pub async fn arg_query() -> (String, String) {
    let _ = arg_filename_default().await;
    let q = "screen".to_string();
    let expected = "lockscreen-timeout :\t10\nscreen-resolution :\t1920x1080";
    (q, expected.to_string())
}

pub async fn query_all_result() -> String {
    let _ = arg_filename_default().await;
    let client = Client::new().await.unwrap();
    let expected = client.proxy().query("").await.unwrap();
    expected
}

pub async fn arg_edit(bak: Option<String>) -> (String, String, String) {
    let _ = arg_filename_default().await;
    let (_, path) = new_tmp_file("hello world").unwrap();
    let args = CliArgs {
        nocpp: false,
        filename: None,
        cpp: None,
        load: None,
        merge: None,
        edit: Some(path.clone()),
        backup: bak.clone(),
        get: None,
        query: None,
        remove: false,
        define: vec![],
        include: vec![],
    };
    let client = Client::new().await.unwrap();
    client.run(&args).await.unwrap();
    (
        path.clone(),
        path + &bak.unwrap_or(String::from(".bak")),
        "hello world".to_string(),
    )
}

pub async fn clear_resources() {
    let client = Client::new().await.unwrap();
    client.proxy().remove_all().await.unwrap();
}