-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aniruddh Radhakrishnan
committed
Sep 6, 2023
1 parent
948c214
commit 4611d79
Showing
4 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ authors = ["Garrett Berg <[email protected]>"] | |
license = "MIT" | ||
|
||
[dependencies] | ||
serde_json = { version = "1.0.105" , optional = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
use crate::DisplayStr; | ||
use std::str::FromStr; | ||
use std::hash::Hash; | ||
use std::collections::HashMap; | ||
|
||
pub trait Map<K , V> where K: Hash + Eq + FromStr, V: DisplayStr{ | ||
|
||
///Prioritized first | ||
fn get(&self, key : &K) -> Option<&V>; | ||
|
||
|
||
///Returned an owned item instead of a reference | ||
///Prioritized second | ||
#[allow(unused_variables)] | ||
fn get_owned(&self, key : &K) -> Option<V>{ | ||
None //This is done because HashMap and serde_json Map can both return references | ||
} | ||
} | ||
|
||
impl<K,V> Map<K,V> for HashMap<K,V> where K: Hash + Eq + FromStr, V: DisplayStr { | ||
fn get(&self, key : &K) -> Option<&V>{ | ||
HashMap::get(self, key) | ||
} | ||
} | ||
|
||
impl Map<String, String> for std::env::Vars { | ||
fn get(&self, _key : &String) -> Option<&String>{None} | ||
fn get_owned(&self, key : &String) -> Option<String>{ | ||
std::env::var(key).ok() | ||
} | ||
} | ||
|
||
mod serde_json_impl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::Map; | ||
extern crate serde_json; | ||
use self::serde_json::{ Value, Map as JsonMap}; | ||
use crate::DisplayStr; | ||
|
||
impl Map<String, Value> for JsonMap<String, Value>{ | ||
fn get(&self, key : &String) -> Option<&Value> { | ||
JsonMap::get(self, key) | ||
} | ||
} | ||
|
||
|
||
impl DisplayStr for Value{ | ||
fn display_str(&self, f: &mut crate::Formatter) -> crate::Result<()> { | ||
match self { | ||
Value::Bool(b) => b.display_str(f), | ||
Value::Null => "null".display_str(f), | ||
Value::Number(n) => n.to_string().display_str(f), | ||
Value::String(s) => s.display_str(f), | ||
Value::Array(a) => format!("{:?}" , a).display_str(f), | ||
Value::Object(a) => format!("{:?}", a).display_str(f), | ||
} | ||
} | ||
} |