Skip to content

Commit

Permalink
made utils mod a directory
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhexists committed Nov 29, 2023
1 parent 6995be1 commit c09d14d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 61 deletions.
6 changes: 4 additions & 2 deletions src/branches.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//This file will handles all the logic to create a new branch

use crate::utils::read_vault_ignore;
use crate::utils::sync_current_dir;

use std::fs;
use std::io::{self};
use std::path::Path;
use crate::utils::utils:: read_vault_ignore;
use crate::utils::sync_dir::sync_current_dir;


//Creates a new branch :)
//@TODO - Add all current components to the new branch
Expand Down
46 changes: 46 additions & 0 deletions src/utils/dir_structure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::collections::HashMap;
use std::fs;
use std::path::Path;

pub fn get_directory_structure(
directory_path: &str,
) -> HashMap<String, Vec<HashMap<String, String>>> {
let path: &Path = Path::new(directory_path);

if path.is_dir() {
let mut result: HashMap<String, Vec<HashMap<String, String>>> = HashMap::new();
let mut files: Vec<HashMap<String, String>> = Vec::new();

if let Ok(entries) = fs::read_dir(path) {
for entry in entries {
if let Ok(entry) = entry {
let file_name: String = entry.file_name().to_string_lossy().into_owned();
let file_path: std::path::PathBuf = entry.path();

let is_directory: bool = file_path.is_dir();

let mut file_info: HashMap<String, String> = HashMap::new();
file_info.insert("name".to_string(), file_name.clone());
file_info.insert("is_directory".to_string(), is_directory.to_string());
file_info.insert("is_read".to_string(), "false".to_string());

if is_directory {
let subdirectory_structure: HashMap<String, Vec<HashMap<String, String>>> =
get_directory_structure(&file_path.to_string_lossy());
file_info.insert(
"contents".to_string(),
serde_json::to_string(&subdirectory_structure).unwrap(),
);
}

files.push(file_info);
}
}
}

result.insert("contents".to_string(), files);
result
} else {
HashMap::new()
}
}
3 changes: 3 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod utils;
pub mod sync_dir;
pub mod dir_structure;
64 changes: 5 additions & 59 deletions src/utils.rs → src/utils/sync_dir.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::collections::HashMap;
use std::fs;
use std::fs::read_to_string;
use std::fs::File;
use std::io::Read;
use std::io::{self};
use std::fs;
use std::path::Path;

use crate::utils::utils::read_vault_ignore;


pub fn sync_current_dir(current_dir: &Path, current_branch: &str) -> io::Result<()> {
let ignored_files: Vec<String> = read_vault_ignore();
let vault_path: &Path = Path::new(".vault");
Expand Down Expand Up @@ -53,59 +54,4 @@ pub fn sync_current_dir(current_dir: &Path, current_branch: &str) -> io::Result<
}
}
Ok(())
}

pub fn read_vault_ignore() -> Vec<String> {
let filename: &str = ".vaultignore";
let path: &Path = Path::new(filename);
let mut result: Vec<String> = Vec::new();
if path.exists() {
for line in read_to_string(filename).unwrap().lines() {
result.push(line.to_string())
}
}
result
}

pub fn get_directory_structure(
directory_path: &str,
) -> HashMap<String, Vec<HashMap<String, String>>> {
let path: &Path = Path::new(directory_path);

if path.is_dir() {
let mut result: HashMap<String, Vec<HashMap<String, String>>> = HashMap::new();
let mut files: Vec<HashMap<String, String>> = Vec::new();

if let Ok(entries) = fs::read_dir(path) {
for entry in entries {
if let Ok(entry) = entry {
let file_name: String = entry.file_name().to_string_lossy().into_owned();
let file_path: std::path::PathBuf = entry.path();

let is_directory: bool = file_path.is_dir();

let mut file_info: HashMap<String, String> = HashMap::new();
file_info.insert("name".to_string(), file_name.clone());
file_info.insert("is_directory".to_string(), is_directory.to_string());
file_info.insert("is_read".to_string(), "false".to_string());

if is_directory {
let subdirectory_structure: HashMap<String, Vec<HashMap<String, String>>> =
get_directory_structure(&file_path.to_string_lossy());
file_info.insert(
"contents".to_string(),
serde_json::to_string(&subdirectory_structure).unwrap(),
);
}

files.push(file_info);
}
}
}

result.insert("contents".to_string(), files);
result
} else {
HashMap::new()
}
}
}
15 changes: 15 additions & 0 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::fs::read_to_string;
use std::path::Path;


pub fn read_vault_ignore() -> Vec<String> {
let filename: &str = ".vaultignore";
let path: &Path = Path::new(filename);
let mut result: Vec<String> = Vec::new();
if path.exists() {
for line in read_to_string(filename).unwrap().lines() {
result.push(line.to_string())
}
}
result
}

0 comments on commit c09d14d

Please sign in to comment.