Skip to content

Commit 7eda571

Browse files
author
RAR27
authored
Merge pull request #10 from RAR27/iss8
Iss8
2 parents a63b23a + 996ab56 commit 7eda571

File tree

2 files changed

+66
-38
lines changed

2 files changed

+66
-38
lines changed

src/lib.rs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ pub fn wallpaper_current_time(
5454
program: Arc<Option<String>>,
5555
times: &[Time],
5656
) -> Result<(), Box<dyn Error>> {
57+
// we should be able to unwrap the result from canonicalize since we already found
58+
// out that the directory is valid
5759
let mut dir_iter = sorted_dir_iter(dir);
5860

59-
dir_iter
60-
.next()
61-
.unwrap()
62-
.map_err(|_| Errors::DirNonExistantError(dir.to_string()))?;
61+
dir_iter.next();
6362

6463
let mut prog_handle: Command = Command::new("");
6564
let mut times_iter = times.iter();
@@ -127,17 +126,6 @@ pub fn wallpaper_current_time(
127126
Ok(())
128127
}
129128

130-
fn prog_handle_loader(filepath_set: &str, program: Arc<Option<String>>, prog_handle: &mut Command) {
131-
if let Some(prog_str) = program.as_deref() {
132-
let mut prog_split = prog_str.split_whitespace();
133-
*prog_handle = Command::new(prog_split.next().unwrap());
134-
for word in prog_split {
135-
prog_handle.arg(word);
136-
}
137-
prog_handle.arg(filepath_set);
138-
}
139-
}
140-
141129
pub fn wallpaper_listener(
142130
dir: String,
143131
dir_count: usize,
@@ -186,6 +174,17 @@ pub fn wallpaper_listener(
186174
}
187175
}
188176

177+
fn prog_handle_loader(filepath_set: &str, program: Arc<Option<String>>, prog_handle: &mut Command) {
178+
if let Some(prog_str) = program.as_deref() {
179+
let mut prog_split = prog_str.split_whitespace();
180+
*prog_handle = Command::new(prog_split.next().unwrap());
181+
for word in prog_split {
182+
prog_handle.arg(word);
183+
}
184+
prog_handle.arg(filepath_set);
185+
}
186+
}
187+
189188
pub fn listener_setup(dir: &str) -> (usize, Result<Time, Errors>, Time, Vec<Time>) {
190189
let dir_count = WalkDir::new(dir).into_iter().count() - 1;
191190
let step_time = if dir_count == 0 {
@@ -209,10 +208,9 @@ pub fn print_schedule(dir: &str, dir_count: usize) -> Result<(), Box<dyn Error>>
209208
return Err(Errors::CountCompatError(dir_count).into());
210209
}
211210

212-
dir_iter
213-
.next()
214-
.unwrap()
215-
.map_err(|_| Errors::DirNonExistantError(dir.to_string()))?;
211+
dir_iter.next();
212+
213+
let mut dir_iter = sorted_dir_iter(dir);
216214

217215
while i < 24 * 60 {
218216
println!(
@@ -227,7 +225,7 @@ pub fn print_schedule(dir: &str, dir_count: usize) -> Result<(), Box<dyn Error>>
227225
Ok(())
228226
}
229227

230-
fn sorted_dir_iter(dir: &str) -> IntoIter {
228+
pub fn sorted_dir_iter(dir: &str) -> IntoIter {
231229
WalkDir::new(dir)
232230
.sort_by(|a, b| {
233231
alphanumeric_sort::compare_str(
@@ -276,12 +274,12 @@ fn error_checking(
276274
None => Err(Errors::ConfigFileError(ConfigFileErrors::Empty)),
277275
Some(time) => Ok(time),
278276
}?;
279-
280277
if 1440 % dir_count != 0 || dir_count == 0 {
281278
return Err(Errors::CountCompatError(dir_count).into());
282-
};
279+
}
283280
Ok(*loop_time)
284281
}
282+
285283
#[cfg(windows)]
286284
fn de_command_spawn(filepath_set: &str) -> Result<(), Box<dyn Error>> {
287285
unsafe {

src/main.rs

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use crate::errors::{ConfigFileErrors, Errors};
2121
use clap::{App, AppSettings, Arg};
2222
use dirs::config_dir;
23-
use dyn_wall_rs::{print_schedule, time_track::Time, wallpaper_listener};
23+
use dyn_wall_rs::{print_schedule, sorted_dir_iter, time_track::Time, wallpaper_listener};
24+
use std::fs::canonicalize;
2425
use std::{
2526
error::Error, fs::create_dir_all, fs::File, io::Read, io::Write, str::FromStr, sync::Arc,
2627
};
@@ -84,18 +85,32 @@ fn main() {
8485
if let Some(auto) = matches.value_of("Auto") {
8586
let dir_count = WalkDir::new(auto).into_iter().count() - 1;
8687

87-
if let Err(e) =
88-
wallpaper_listener(String::from(auto), dir_count, Arc::clone(&program), None)
89-
{
90-
eprintln!("{}", e);
88+
match check_dir_exists(auto) {
89+
Err(e) => eprintln!("{}", e),
90+
Ok(_) => {
91+
let auto = canonicalize(auto).unwrap();
92+
let auto = auto.to_str().unwrap();
93+
if let Err(e) =
94+
wallpaper_listener(String::from(auto), dir_count, Arc::clone(&program), None)
95+
{
96+
eprintln!("{}", e);
97+
}
98+
}
9199
}
92100
}
93101

94102
if let Some(s) = matches.value_of("Schedule") {
95103
let dir_count = WalkDir::new(s).into_iter().count() - 1;
96104

97-
if let Err(e) = print_schedule(s, dir_count) {
98-
eprintln!("{}", e);
105+
match check_dir_exists(s) {
106+
Err(e) => eprintln!("{}", e),
107+
Ok(_) => {
108+
let s = canonicalize(s).unwrap();
109+
let s = s.to_str().unwrap();
110+
if let Err(e) = print_schedule(s, dir_count) {
111+
eprintln!("{}", e);
112+
}
113+
}
99114
}
100115
}
101116

@@ -106,16 +121,21 @@ fn main() {
106121
Err(e) => {
107122
eprintln!("{}", e);
108123
}
109-
Ok(times) => {
110-
if let Err(e) = wallpaper_listener(
111-
String::from(c),
112-
dir_count,
113-
Arc::clone(&program),
114-
Some(times),
115-
) {
116-
eprintln!("{}", e);
124+
Ok(times) => match check_dir_exists(c) {
125+
Err(e) => eprintln!("{}", e),
126+
Ok(_) => {
127+
let c = canonicalize(c).unwrap();
128+
let c = c.to_str().unwrap();
129+
if let Err(e) = wallpaper_listener(
130+
String::from(c),
131+
dir_count,
132+
Arc::clone(&program),
133+
Some(times),
134+
) {
135+
eprintln!("{}", e);
136+
}
117137
}
118-
}
138+
},
119139
}
120140
}
121141
}
@@ -186,3 +206,13 @@ fn create_config() -> Result<(), Box<dyn Error>> {
186206
config_file.write_all(default_test.as_bytes())?;
187207
Ok(())
188208
}
209+
210+
fn check_dir_exists(dir: &str) -> Result<(), Errors> {
211+
let mut dir_iter = sorted_dir_iter(dir);
212+
213+
if dir_iter.next().unwrap().is_err() {
214+
Err(Errors::DirNonExistantError(dir.to_string()))
215+
} else {
216+
Ok(())
217+
}
218+
}

0 commit comments

Comments
 (0)