Skip to content

Commit

Permalink
feat: Add config creation
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Oct 7, 2023
1 parent 96c2e15 commit 53af159
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[package]
name = "ReSet-Lib"
name = "reset_lib"
version = "0.1.0"
edition = "2021"
description = "Data structure library for ReSet"
repository = "https://github.com/Xetibo/ReSet-Lib"
license = "GPL-3.0-only"

[dependencies]
directories-next = "2.0.0"
31 changes: 31 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use directories_next as dirs;
use std::{fmt, fs, path::PathBuf};

mod tests;

#[derive(Debug, Clone)]
struct PathNotFoundError;

impl fmt::Display for PathNotFoundError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "File could not be found")
}
}

fn create_config(project_organization: &str, project_name: &str) -> Option<PathBuf> {
let config_dir = dirs::ProjectDirs::from("com", project_organization, project_name)?;
let config_dir = config_dir.config_dir();
if !config_dir.exists() {
fs::create_dir(config_dir).expect("Could not create directory");
}
let metadata = fs::metadata(config_dir);
if metadata.is_err() {
return None;
}
let config_file = String::from(project_name) + ".toml";
let file_path = config_dir.join(config_file);
if !file_path.exists() {
fs::File::create(&file_path).expect("Could not write config file");
}
Some(config_dir.join(""))
}
7 changes: 7 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[test]
fn test_config_dir() {
use crate::create_config;
let organization_name = "xetibo";
let project_name = "globiTM";
assert!(create_config(organization_name, project_name).is_some());
}

0 comments on commit 53af159

Please sign in to comment.