1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#[derive(Debug, Serialize, Deserialize)] pub struct NaverAuth { pub client_id: String, pub client_secret: String, } impl NaverAuth { pub fn new<'a>(client_id: &'a str, client_secret: &'a str) -> NaverAuth { NaverAuth { client_id: client_id.to_owned(), client_secret: client_secret.to_owned(), } } } impl PartialEq for NaverAuth { fn eq(&self, other: &NaverAuth) -> bool { self.client_id == other.client_id && self.client_secret == other.client_secret } } #[derive(Debug)] pub enum NaverAuthError { NoClientId, NoClientSecret, } pub fn get_auth_from_env() -> Result<NaverAuth, NaverAuthError> { let client_id = std::env::var("NAVER_CLIENT_ID"); if client_id.is_err() { return Err(NaverAuthError::NoClientId); } let client_secret = std::env::var("NAVER_CLIENT_SECRET"); if client_secret.is_err() { return Err(NaverAuthError::NoClientSecret); } Ok(NaverAuth { client_id: client_id.unwrap(), client_secret: client_secret.unwrap(), }) } #[cfg(test)] mod tests { use super::*; #[test] fn test_new() { let auth = NaverAuth::new("client_id", "client_secret"); assert_eq!( auth, NaverAuth { client_id: "client_id".to_owned(), client_secret: "client_secret".to_owned(), } ); } #[test] fn test_get_env() { let auth = get_auth_from_env(); assert!(auth.is_ok()); } }