Skip to content

Commit defe1cc

Browse files
committed
Empty file handling.
1 parent 6077f53 commit defe1cc

10 files changed

Lines changed: 45 additions & 25 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ bm c/config set key=value[,k=v] : Edit config values
1212
bm c/config get key[,key2] : Get config values
1313
bm h/help : Prints help text.
1414
bm --help : Prints help text.
15+
bm debug <any full command> : For any command, activates debug mode.
1516
```
1617

1718
### Add Options

src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Command {
4444
}
4545

4646
pub fn print_command(&self) {
47-
println!("{:?} --> {:?}", self.type_of, self.args);
47+
println!("[DBG|CMD:{:?} --> {:?}]", self.type_of, self.args);
4848
}
4949

5050
/// Execute

src/commands/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ enum PathType {
1111
}
1212

1313
pub fn add(params: &Option<Vec<String>>, store: &mut HashMap<String, String>) -> ExecutionResult {
14-
let mut name : String;
14+
let name : String;
1515
let mut path: String;
1616
let mut addanyway = false;
1717
let mut overwritable = false;

src/commands/delete.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use std::collections::HashMap;
2-
use std::fmt::format;
3-
use std::path::PathBuf;
4-
use path_absolutize::Absolutize;
52
use crate::utils::error;
63
use crate::utils::success::ExecutionResult;
74

85
/// bm delete NAME
96
pub fn delete(params: &Option<Vec<String>>, store: &mut HashMap<String, String>) -> ExecutionResult {
10-
let mut name : String;
7+
let name : String;
118
match params {
129
// Show all
1310
None => {

src/commands/help.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
use std::collections::HashMap;
2-
use std::fmt::format;
3-
use std::path::PathBuf;
4-
use path_absolutize::Absolutize;
51
use crate::utils::error;
6-
use crate::utils::success::ExecutionResult;
72

83
/// bm delete NAME
94
/// Print help text and exit with non-zero code.
@@ -38,6 +33,10 @@ pub fn print_help() {
3833
3934
help Prints this help text.
4035
36+
debug Activates debug mode for other commands.
37+
Must be given as the first argument to application.
38+
Example: bm debug add name path --option
39+
4140
OPTIONS
4241
-o, --overwrite Overwrite previous value of bookmark.
4342

src/commands/show.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use std::collections::HashMap;
2-
use std::fmt::format;
3-
use std::path::PathBuf;
4-
use path_absolutize::Absolutize;
52
use crate::utils::error;
63
use crate::utils::success::ExecutionResult;
74

85
/// bm show
96
/// bm show NAME
107
pub fn show(params: &Option<Vec<String>>, store: &mut HashMap<String, String>) -> ExecutionResult {
11-
let mut name : String;
8+
let name : String;
129
match params {
1310
// Show all
1411
None => {

src/main.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@ use crate::parser::toml_deserialize::parse_string;
1010
fn main() {
1111
let store_path = file::dir::create_dir_and_file_if_not_exist();
1212
let store_content = file::file_reader::read_file(&store_path);
13+
let (command, debug_mode) = utils::startup::parse_cli_options();
14+
15+
if debug_mode {
16+
println!("[DBG|File Content: {}]", store_content);
17+
}
18+
1319
let mut store_map = parse_string(store_content);
14-
// println!("\n--> {:?}\n", store_map);
15-
let command = utils::startup::parse_cli_options();
16-
// command.print_command();
20+
21+
22+
if debug_mode {
23+
command.print_command();
24+
}
1725
let result = command.execute(&mut store_map);
18-
// println!("{:?}", result);
26+
27+
if debug_mode {
28+
result.print();
29+
}
1930

2031
if result.write_to_file {
2132
let new_store_content = parser::toml_serialize::create_store_content(store_map);

src/parser/toml_deserialize.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ pub fn parse_string(toml_string : String) -> HashMap<String, String> {
1414

1515
let parse_result: Result<HashMap<String, Vec<Bookmark>>, toml::de::Error> = toml::from_str(toml_string.as_str());
1616
let mut bm_map : HashMap<String, String> = Default::default();
17+
let mut vec: Vec<Bookmark> = Vec::<Bookmark>::new();
1718
match parse_result {
1819
Ok(bookmarks_table) => {
19-
let bookmarks = &bookmarks_table["bookmark"];
20+
let bookmarks = &bookmarks_table.get("bookmark").unwrap_or(&vec);
2021
bm_map = bookmarks.into_iter().map(|bm| (bm.name_clone(), bm.dir_clone()) ).collect();
2122
},
2223
Err(e) => {

src/utils/startup.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use crate::commands::{Command, CommandType};
22
use crate::utils::error;
33
use crate::utils::error::ErrorCode;
44

5-
65
/// Print usage text and exit with non-zero code.
76
fn print_usage(any_code : Option<ErrorCode>) {
8-
let help = r#"Usage: bm <asdc> [name] [directory]
7+
let help = r#"Usage: bm [debug] <asdc> [name] [directory]
98
For detailed help, type `bm help`
109
"#;
1110
print!("{}\n",help);
@@ -15,9 +14,18 @@ fn print_usage(any_code : Option<ErrorCode>) {
1514
}
1615
}
1716

18-
pub fn parse_cli_options() -> Command {
17+
pub fn parse_cli_options() -> (Command, bool) {
1918
let mut command: Command = Command::new(CommandType::NONE, None);
2019
let mut arguments : Vec<String> = std::env::args().collect();
20+
let mut command_index = 1;
21+
let mut debug_mode = false;
22+
23+
if arguments.len() > 1 && arguments[1] == "debug" {
24+
println!("[DBG|Debug Mode]");
25+
arguments.remove(1);
26+
command_index = 2;
27+
debug_mode = true;
28+
}
2129

2230
// No argument is given.
2331
// ./bm
@@ -36,7 +44,7 @@ pub fn parse_cli_options() -> Command {
3644
args_options = Some(arguments);
3745
}
3846

39-
match std::env::args().nth(1).as_deref() {
47+
match std::env::args().nth(command_index).as_deref() {
4048
Some("h") | Some("help") | Some("-h") | Some("--help") => {
4149
command = Command::new(CommandType::HELP, None); // args_option = None
4250
}
@@ -61,5 +69,5 @@ pub fn parse_cli_options() -> Command {
6169
print_usage(None);
6270
}
6371
}
64-
command
72+
(command, debug_mode)
6573
}

src/utils/success.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ impl Default for ExecutionResult {
1111
write_to_file: false,
1212
}
1313
}
14+
}
15+
16+
impl ExecutionResult {
17+
pub fn print(&self) {
18+
println!("[DBG|Result:{:?}]", self);
19+
}
1420
}

0 commit comments

Comments
 (0)