Skip to content

Commit

Permalink
Dotenv returns result not option
Browse files Browse the repository at this point in the history
  • Loading branch information
swlkr committed Sep 9, 2024
1 parent cbf4f35 commit 81f1ad1
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ mod html;
mod router;

pub use axum;
pub use axum::middleware;
pub use axum::{
extract::*,
http::{self, header::*, Uri},
middleware::Next,
response::*,
routing::{any, delete, get, head, patch, post, put, trace},
middleware::Next,
*,
};
pub use axum_extra::{extract::*, headers};
pub use axum_extra::{self, extract::*, headers};
pub use cookie::Cookie;
pub use db::{db, rusqlite, tokio_rusqlite, Connection};
pub use html::{component, escape, html, Component, Elements, Render};
Expand Down Expand Up @@ -228,7 +229,7 @@ macro_rules! embed_static_files {
};
}

pub fn dotenv(s: &str) -> Option<String> {
pub fn dotenv(s: &str) -> Result<String> {
use std::collections::HashMap;
use std::env;
use std::sync::OnceLock;
Expand All @@ -250,7 +251,9 @@ pub fn dotenv(s: &str) -> Option<String> {
}
});

map.get(s).cloned()
map.get(s)
.cloned()
.ok_or(Error::Io("dotenv get failed".into()))
}

#[cfg(test)]
Expand All @@ -259,8 +262,8 @@ mod tests {

#[test]
fn env_works() {
assert_eq!(dotenv("HELLO"), Some("WORLD".into()));
assert_eq!(dotenv("GOODBYE"), None);
assert_eq!(dotenv("ABC"), Some("XYZ".into()));
assert_eq!(dotenv("HELLO"), Ok("WORLD".into()));
assert!(dotenv("GOODBYE").is_err(),);
assert_eq!(dotenv("ABC"), Ok("XYZ".into()));
}
}

0 comments on commit 81f1ad1

Please sign in to comment.