Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 12 additions & 45 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
// src/config.rs
use serde::{Deserialize, Serialize};
use std::io::{self, Write};
use std::path::PathBuf;
use std::{fs, path::Path};
use std::path::{Path, PathBuf};
use std::{fs};
use thiserror::Error;

#[derive(Error, Debug)]
pub enum ConfigError {
#[error("IO error: {0}")]
Io(#[from] io::Error),
Io(#[from] std::io::Error),
#[error("TOML error: {0}")]
Toml(#[from] toml::ser::Error),
#[error("TOML de error: {0}")]
TomlDe(#[from] toml::de::Error),
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub enum PreferredDialect {
#[serde(rename = "cat")]
Cat,
Expand Down Expand Up @@ -51,39 +50,6 @@ impl Config {
fs::write(config_path, contents)?;
Ok(())
}

pub fn initialize(config_dir: &Path) -> Result<Self, ConfigError> {
let config_path = config_dir.join("config.toml");

if config_path.exists() {
return Self::load(&config_path);
}

// Create config directory if it doesn't exist
fs::create_dir_all(config_dir)?;

print!("🐱 Welcome to purrcrypt! Do you prefer cat or dog mode? [cat/dog]: ");
io::stdout().flush()?;

let mut input = String::new();
io::stdin().read_line(&mut input)?;

let config = Config {
dialect: match input.trim().to_lowercase().as_str() {
"dog" => PreferredDialect::Dog,
_ => PreferredDialect::Cat,
},
};

config.save(&config_path)?;

match config.dialect {
PreferredDialect::Cat => println!("😺 Meow! Cat mode activated!"),
PreferredDialect::Dog => println!("🐕 Woof! Dog mode activated!"),
}

Ok(config)
}
}

pub struct ConfigManager {
Expand All @@ -94,20 +60,21 @@ pub struct ConfigManager {
impl ConfigManager {
pub fn new(config_dir: &Path) -> Result<Self, ConfigError> {
let config_path = config_dir.join("config.toml");
let config = if config_path.exists() {
Config::load(&config_path)?
} else {
Config::initialize(config_dir)?
};
let config = Config::load(&config_path)?;

if !config_path.exists() {
fs::create_dir_all(config_dir)?;
config.save(&config_path)?;
}

Ok(Self {
config,
config_path,
})
}

pub fn get_dialect(&self) -> &PreferredDialect {
&self.config.dialect
pub fn get_dialect(&self) -> PreferredDialect {
self.config.dialect
}

pub fn set_dialect(&mut self, dialect: PreferredDialect) -> Result<(), ConfigError> {
Expand Down
2 changes: 1 addition & 1 deletion src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::fs;
use thiserror::Error;

#[cfg(unix)]
use fs::os::unix::fs::PermissionsExt;
use std::os::unix::fs::PermissionsExt;

#[derive(Error, Debug)]
pub enum KeyError {
Expand Down