Skip to content

Commit

Permalink
Modified with trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Aniruddh Radhakrishnan committed Sep 6, 2023
1 parent 948c214 commit 4611d79
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ authors = ["Garrett Berg <[email protected]>"]
license = "MIT"

[dependencies]
serde_json = { version = "1.0.105" , optional = true }
41 changes: 33 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mod types;
mod fmtnum;
mod macros;

mod maptrait;
pub use maptrait::Map;

pub use fmtstr::strfmt_map;
pub use formatter::Formatter;
pub use types::{Alignment, FmtError, Result, Sign};
Expand Down Expand Up @@ -50,7 +53,7 @@ fmtfloat!(f32 f64);
///
/// println!("{}", strfmt("{Alpha} {Beta:<5.2}",&my_vars).unwrap());
/// ```
pub fn strfmt<'a, K, T: DisplayStr>(fmtstr: &str, vars: &HashMap<K, T>) -> Result<String>
pub fn strfmt<'a, K, T: DisplayStr>(fmtstr: &str, vars: &impl Map<K, T>) -> Result<String>
where
K: Hash + Eq + FromStr,
{
Expand All @@ -61,13 +64,21 @@ where
return Err(new_key_error(fmt.key));
}
};
let v = match vars.get(&k) {
Some(v) => v,
None => {
return Err(new_key_error(fmt.key));
}
};
v.display_str(&mut fmt)

let v = vars.get(&k);

if v.is_some(){
return v.unwrap().display_str(&mut fmt);
}

let v = vars.get_owned(&k);

if v.is_some(){
return v.as_ref().unwrap().display_str(&mut fmt)
}

return Err(new_key_error(fmt.key));

};
strfmt_map(fmtstr, &formatter)
}
Expand Down Expand Up @@ -119,6 +130,13 @@ impl DisplayStr for String {
}
}

impl DisplayStr for bool {
fn display_str(&self, f: &mut Formatter) -> Result<()> {
f.str(self.to_string().as_str())
}
}


impl DisplayStr for &str {
fn display_str(&self, f: &mut Formatter) -> Result<()> {
f.str(self)
Expand All @@ -131,6 +149,13 @@ impl DisplayStr for &dyn DisplayStr {
}
}


impl <T : DisplayStr> DisplayStr for &T{
fn display_str(&self, f: &mut Formatter) -> Result<()> {
(*self).display_str(f)
}
}

impl DisplayStr for Box<dyn DisplayStr> {
fn display_str(&self, f: &mut Formatter) -> Result<()> {
self.as_ref().display_str(f)
Expand Down
33 changes: 33 additions & 0 deletions src/maptrait.rs
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;
24 changes: 24 additions & 0 deletions src/maptrait/serde_json_impl.rs
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),
}
}
}

0 comments on commit 4611d79

Please sign in to comment.