Skip to content

Commit 443894b

Browse files
committed
updated and fixed code, deleted 'sqlite' database
1 parent 2461d53 commit 443894b

File tree

8 files changed

+42
-200
lines changed

8 files changed

+42
-200
lines changed

src/add_file.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ pub mod add_fn
2424
{
2525
crate::log::logger::start("ADD ".to_string());
2626

27-
let TIME_DATE : [String; 2] = crate::time::time_fn::start();
27+
let time_date : [String; 2] = crate::time::time_fn::start();
2828

2929
let save_info : [String; 4] = [
3030
file_path.to_string(),
3131
unq_name.to_string(),
32-
TIME_DATE[0].to_string(),
33-
TIME_DATE[1].to_string()];
32+
time_date[0].to_string(),
33+
time_date[1].to_string()];
3434

3535

36-
let CREATE_COPY = crate::database::add::start(save_info);
37-
if (CREATE_COPY == true) { create_save(&unq_name, &file_path); }
36+
let create_copy = crate::database::add::start(save_info);
37+
if create_copy == true { create_save(&unq_name, &file_path); }
3838
else { println!(":("); }
3939
}
4040
}

src/delete.rs

Lines changed: 3 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,8 @@
1-
pub mod delete_fn {
2-
#![allow(warnings)]
3-
4-
// PACKAGES
5-
use std::fs;
6-
use std::error::Error;
7-
8-
extern crate rusqlite;
9-
use rusqlite::{params, Connection, Result, NO_PARAMS};
10-
// PACKAGES
11-
12-
#[derive(Debug)]
13-
struct FileStr {
14-
id: i64,
15-
file_path: String,
16-
save_name: String,
17-
saved_date: String,
18-
saved_time: String
19-
}
20-
21-
22-
// CREATE NEW CLEARED ARRAY with DELETED ELEMENT, and DELETE SAVE FILE
23-
fn delete_save(save_name: String) -> Result<Vec<FileStr>, Box<dyn Error>> {
24-
25-
{ // BLOCK FOR DELETE SAVE FILE
26-
let save_path = format!("rustix/saves/{:02}.txt", save_name);
27-
28-
match fs::remove_file(save_path) {
29-
Err(error) => println!("{:?}", error.kind()),
30-
Ok(_) => println!("The save was successfully deleted !"),
31-
}
32-
}
33-
34-
let conn = Connection::open("rustix/storage.db3")?;
35-
let mut stmt = conn.prepare("SELECT * FROM main")?;
36-
37-
let mut base = stmt.query_map(NO_PARAMS, |row| {
38-
Ok(FileStr {
39-
id: row.get(0)?,
40-
file_path: row.get(1)?, save_name: row.get(2)?,
41-
saved_date: row.get(3)?, saved_time: row.get(4)?,
42-
})
43-
})?;
44-
45-
let mut new_base: Vec<FileStr> = Vec::new();
46-
let mut new_id: i64 = 1;
47-
48-
for x in base.into_iter() {
49-
if x.as_ref().unwrap().save_name == save_name { continue; }
50-
else { new_base.push(x.unwrap()); }
51-
}
52-
for x in new_base.iter_mut() {
53-
x.id = new_id;
54-
new_id += 1;
55-
}
56-
57-
Ok(new_base)
58-
}
59-
60-
61-
// UPDATE DB and INSERT UPDATED DB
62-
fn update_db(base: Vec<FileStr>) -> Result<(), Box<dyn Error>> {
63-
let conn = Connection::open("rustix/storage.db3")?;
64-
conn.execute("DROP TABLE IF EXISTS main", NO_PARAMS)?;
65-
66-
conn.execute("CREATE TABLE IF NOT EXISTS main (
67-
id INTEGER PRIMARY KEY,
68-
file_path TEXT NOT NULL, save_name TEXT UNIQUE,
69-
saved_date TEXT NOT NULL, saved_time TEXT NOT NULL)", NO_PARAMS,
70-
)?;
71-
72-
for x in base {
73-
conn.execute("INSERT INTO main (
74-
id, file_path, save_name, saved_date, saved_time) VALUES (?1, ?2, ?3, ?4, ?5)",
75-
params![x.id, x.file_path, x.save_name, x.saved_date, x.saved_time],
76-
)?;
77-
}
78-
79-
conn.close();
80-
Ok(())
81-
}
82-
1+
pub mod delete_fn
2+
{
833
// START POINT
844
pub fn start(save_name: String) {
855
crate::log::logger::start("DELETE".to_string());
86-
87-
update_db(delete_save(save_name).unwrap());
6+
crate::database::del::start(save_name);
887
}
898
}

src/initialize.rs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
1-
pub mod init_fn {
2-
#![allow(warnings)]
3-
1+
pub mod init_fn
2+
{
43
// PACKAGES
54
use std::env;
65
use std::fs;
7-
use std::fs::{File, OpenOptions};
8-
9-
use serde::{Deserialize, Serialize};
10-
use serde_yaml::{self};
11-
12-
extern crate rusqlite;
13-
use rusqlite::{params, Connection, Result, NO_PARAMS};
14-
// PACKAGES
15-
16-
#[derive(Debug, Serialize, Deserialize)]
17-
struct Config {
18-
name: String,
19-
os_name: String,
20-
created_date: String,
21-
created_time: String,
22-
}
23-
24-
25-
fn create_db() -> Result<()>
26-
{
27-
let conn = Connection::open("rustix/storage.db3")?;
28-
29-
conn.execute("CREATE TABLE IF NOT EXISTS main (
30-
id INTEGER PRIMARY KEY,
31-
file_path TEXT NOT NULL, save_name TEXT UNIQUE,
32-
saved_date TEXT NOT NULL, saved_time TEXT NOT NULL)", NO_PARAMS,
33-
)?;
34-
35-
conn.close();
36-
37-
Ok(())
38-
}
396

407

418
fn create_yaml()
429
{
43-
let TIME_DATE: [String; 2] = crate::time::time_fn::start();
10+
let time_date: [String; 2] = crate::time::time_fn::start();
4411

4512
let cwd = env::current_dir().unwrap();
4613
let folder: String = String::from(cwd.to_string_lossy());
4714
let os_name = String::from(env::consts::OS);
48-
let created_date = &TIME_DATE[0];
49-
let created_time = &TIME_DATE[1];
15+
let created_date = &time_date[0];
16+
let created_time = &time_date[1];
5017

5118
let info = format!("name: {folder}\nos_name: {os_name}\ncreated_date: {created_date}\ncreated_time: {created_time}");
5219

@@ -61,11 +28,11 @@ pub mod init_fn {
6128
match fs::create_dir("rustix") {
6229
Err(why) => println!("{:?} !", why.kind()),
6330
Ok(_) => {
64-
File::create("rustix/log.txt");
65-
File::create("rustix/init.yml");
31+
fs::File::create("rustix/log.txt");
32+
fs::File::create("rustix/init.yml");
33+
fs::File::create("rustix/storage.txt");
6634
fs::create_dir("rustix/saves");
6735
create_yaml();
68-
create_db();
6936
},
7037
}
7138

src/log.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
pub mod logger {
1+
pub mod logger
2+
{
23
use std::fs::OpenOptions;
34
use std::io::prelude::*;
45

5-
pub fn start(action: String) {
6-
let TIME_DATE: [String; 2] = crate::time::time_fn::start();
6+
pub fn start(action: String)
7+
{
8+
let time_date: [String; 2] = crate::time::time_fn::start();
79

8-
let created_date = &TIME_DATE[0];
9-
let created_time = &TIME_DATE[1];
10+
let created_date = &time_date[0];
11+
let created_time = &time_date[1];
1012

1113
let info = format!("{} | {} - {}", action, created_date, created_time);
1214

src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ pub use crate::print::print_fn;
2222
mod log;
2323

2424
mod time;
25+
26+
mod database;
2527
// MODULES
2628

2729

src/print.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
pub mod print_fn
22
{
33
// PACKAGES
4-
use std::fs::File;
5-
use std::error::Error;
4+
use std::fs;
65
use std::io::{prelude::*, BufReader};
76

87
use serde::{Deserialize, Serialize};
@@ -21,7 +20,6 @@ pub mod print_fn
2120

2221
pub fn print_commands() {
2322
println!("Commands:\n");
24-
2523
println!("rustix-vcs init\n command to initialize the project\n");
2624
println!("rustix-vcs add src/main.js save_name\n command to save the file.\n");
2725
println!("rustix-vcs delete save_name\n command to delete the save.\n");
@@ -34,7 +32,7 @@ pub mod print_fn
3432

3533

3634
fn show_log() -> std::io::Result<()> {
37-
let file = File::open("rustix/log.txt")?;
35+
let file = fs::File::open("rustix/log.txt")?;
3836
let reader = BufReader::new(file);
3937
for line in reader.lines() { println!("{}", line?); }
4038

@@ -43,19 +41,19 @@ pub mod print_fn
4341

4442

4543
pub fn read_yaml() {
46-
let f = File::open("rustix/init.yml").expect("Could not open file.");
47-
let mut data: Config = serde_yaml::from_reader(f).expect("Couldn't read");
44+
let f = fs::File::open("rustix/init.yml").expect("Could not open file.");
45+
let data: Config = serde_yaml::from_reader(f).expect("Couldn't read");
4846

4947
println!("INFO\n os: {}\n created date: {} - {}\n current path: {}\n\n",
5048
data.os_name, data.created_date, data.created_time, data.name);
5149
}
5250

5351

5452
fn print_db() {
55-
let SAVES_BASE = crate::database::get::start().unwrap();
53+
let saves_base = crate::database::get::start().unwrap();
5654
let mut id : i64 = 1;
5755

58-
for x in SAVES_BASE.into_iter() {
56+
for x in saves_base.into_iter() {
5957
println!("{}. {}\n - path\n {}\n - saved\n {}\n {}\n",
6058
id, x[1], x[0], x[2], x[3]);
6159

src/select.rs

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,8 @@
1-
pub mod select_fn {
2-
#![allow(warnings)]
3-
1+
pub mod select_fn
2+
{
43
// PACKAGES
54
use std::fs;
6-
use std::fs::File;
75
use std::io::Write;
8-
use std::error::Error;
9-
10-
extern crate rusqlite;
11-
use rusqlite::{params, Connection, Result, NO_PARAMS};
12-
// PACKAGES
13-
14-
#[derive(Debug)]
15-
struct FileStr {
16-
id: i64,
17-
file_path: String,
18-
save_name: String,
19-
saved_date: String,
20-
saved_time: String
21-
}
22-
23-
#[derive(Debug)]
24-
struct DBStr {
25-
id: i64,
26-
save_name: String,
27-
file_path: String
28-
}
29-
30-
31-
fn check_save_exist(save_name: &String) -> Result<[String; 4], Box<dyn Error>>
32-
{
33-
let conn = Connection::open("rustix/storage.db3")?;
34-
let mut stmt = conn.prepare("SELECT id, save_name, file_path FROM main")?;
35-
let mut base = stmt.query_map(NO_PARAMS, |row| { Ok(DBStr { id: row.get(0)?, save_name: row.get(1)?, file_path: row.get(2)?, }) })?;
36-
37-
let mut data: [String; 4] = ["".to_string(), "0".to_string(), "".to_string(), "".to_string()];
38-
39-
for e in base.into_iter() {
40-
let x = e.unwrap();
41-
if &x.save_name == save_name {
42-
data[0] = format!("{:?}", x.id); // ID
43-
data[1] = "1".to_string(); // EXIST {1 = YES, 0 = NO}
44-
data[2] = x.file_path; // FILE PATH
45-
data[3] = x.save_name; // NAME
46-
47-
break;
48-
} else { continue; }
49-
}
50-
51-
Ok(data)
52-
}
536

547

558
fn write_content(file_path: &String, file_name: &String)
@@ -58,21 +11,23 @@ pub mod select_fn {
5811
match fs::read_to_string(new_file_name) {
5912
Err(error) => println!("{:?}", error.kind()),
6013
Ok(x) => {
61-
let mut f = File::create(file_path).expect("Unable to create file");;
14+
let mut f = fs::File::create(file_path).expect("Unable to create file");
6215
f.write_all(x.as_bytes()).expect("Unable to write data");
6316

6417
println!("To a file {:?} moved saving {:?}", file_path, file_name);
6518
},
6619
}
6720
}
6821

22+
6923
// START POINT
70-
pub fn start(save_name: String) {
24+
pub fn start(save_name: String)
25+
{
7126
crate::log::logger::start("SELECT".to_string());
7227

73-
let db_str = check_save_exist(&save_name).unwrap();
74-
if db_str[1] == "0" { return println!("there is no save with this name !"); }
28+
let save_info : (bool, String) = crate::database::slc::start(&save_name);
7529

76-
write_content(&db_str[2], &db_str[3]);
30+
if save_info.0 == true { write_content(&save_info.1, &save_name); }
31+
else { println!("there is no save with this name !"); }
7732
}
7833
}

src/time.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
pub mod time_fn {
2-
#![allow(warnings)]
3-
1+
pub mod time_fn
2+
{
43
// PACKAGES
54
use std::process::Command;
65

0 commit comments

Comments
 (0)