Skip to content

Commit

Permalink
0.1.1 Update
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaStev committed Nov 29, 2024
1 parent ca2a877 commit 54109b9
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "envie"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["LunaStev <lunastev@gurmstudis.com>"]
description = "Envie is a lightweight and user-friendly library for managing environment variables in Rust. It helps you load and parse .env files, retrieve variables with ease, and provides type-safe access to boolean and other data types."
Expand Down
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Envie is a lightweight and user-friendly library for managing environment variab
Add Envie to your `Cargo.toml` file:

```toml
envie = "0.1.0"
envie = "0.1.1"
```

Then run:
Expand All @@ -29,13 +29,39 @@ Here’s a quick example of how to use Envie:
use envie::Envie;

fn main() {
let env = Envie::load().expect("Failed to load .env file");
// Load the .env file and exit if it fails
let mut env = Envie::load().expect("Failed to load .env file");

// Get the DATABASE_URL value, or use "default_url" if it's not found
let database_url = env.get("DATABASE_URL").unwrap_or_else(|| "default_url".to_string());
let debug_mode: bool = env.get_bool("DEBUG_MODE").unwrap_or(false);

println!("Database URL: {}", database_url);

// Get the DEBUG_MODE value, or set it to false if it's not found
let debug_mode = env.get_bool("DEBUG_MODE").unwrap_or_else(|_| false);
println!("Debug Mode: {}", debug_mode);

// Add or update a new environment variable
env.set("NEW_VARIABLE", "12345").expect("Failed to set NEW_VARIABLE");
println!("NEW_VARIABLE set to 12345");

// Print all environment variables
let all_vars = env.get_all();
println!("All environment variables:");
for (key, value) in all_vars {
println!("{} = {}", key, value);
}

// Delete a specific environment variable
env.remove("NEW_VARIABLE").expect("Failed to remove NEW_VARIABLE");
println!("NEW_VARIABLE removed");

// Reload the .env file to reflect the changes
let env = Envie::load().expect("Failed to load .env file after removal");
let all_vars = env.get_all();
println!("All environment variables after removal:");
for (key, value) in all_vars {
println!("{} = {}", key, value);
}
}
```
### Example .env file
Expand Down
59 changes: 57 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// src/lib.rs
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::Write;

pub struct Envie {
variables: HashMap<String, String>,
pub variables: HashMap<String, String>,
}

impl Envie {
Expand Down Expand Up @@ -36,6 +36,61 @@ impl Envie {
})
}

/// Get a value as an integer.
pub fn get_int(&self, key: &str) -> Result<i32, String> {
self.get(key)
.ok_or(format!("Key '{}' not found", key))
.and_then(|v| v.parse().map_err(|_| format!("Invalid integer value for key '{}'", key)))
}

/// Get all environment variables as a HashMap.
pub fn get_all(&self) -> HashMap<String, String> {
self.variables.clone()
}

/// Set a value for a given key and update the .env file.
pub fn set(&mut self, key: &str, value: &str) -> Result<(), String> {
self.variables.insert(key.to_string(), value.to_string());
let content = fs::read_to_string(".env").unwrap_or_default();
let mut updated_content = String::new();
let mut found = false;

for line in content.lines() {
if line.starts_with(&format!("{}=", key)) {
updated_content.push_str(&format!("{}={}\n", key, value));
found = true;
} else {
updated_content.push_str(line);
updated_content.push('\n');
}
}

if !found {
updated_content.push_str(&format!("{}={}\n", key, value));
}

fs::write(".env", updated_content).map_err(|_| "Failed to write to .env file")?;
Ok(())
}

/// Remove a key-value pair and update the .env file.
pub fn remove(&mut self, key: &str) -> Result<(), String> {
self.variables.remove(key);

let content = fs::read_to_string(".env").unwrap_or_default();
let mut updated_content = String::new();

for line in content.lines() {
if !line.starts_with(&format!("{}=", key)) {
updated_content.push_str(line);
updated_content.push('\n');
}
}

fs::write(".env", updated_content).map_err(|_| "Failed to write to .env file")?;
Ok(())
}

/// Parse the content of a .env file into a HashMap.
fn parse(content: &str) -> HashMap<String, String> {
content
Expand Down

0 comments on commit 54109b9

Please sign in to comment.