Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified with trait #35

Merged
merged 2 commits into from
Sep 8, 2023
Merged

Conversation

loggerheads-with-binary
Copy link

Persistent to issue #17 I have modified the implementation of the strfmt function by using a crate-level exported Map trait instead of using a reference to a HashMap

The Map trait is automatically impleemeneted for std::collection::HashMap, serde_json::Map<String, Value> and for std::env::Vars. For any other custom structs it can be implemented at the user-level

This is the implementation of the trait

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  
    }
}

In general, the .get() method is used. However for cases where a local object is created and to be returned for the string-formatting, an object reference cannot be returned since it will be destroyed pursuant Rust's lifetime rules. Hence, in such cases .get_owned can be used which will return an owned object and not a reference.

By default, .get() is first tried. If it returns None, it tries .get_owned(). If both return None, then new_key_error is raised

Additionally, all the tests for this have also passed pursuant to cargo test

@vitiral
Copy link
Owner

vitiral commented Sep 6, 2023

Thanks!

Not related to your change, but is there really no stdlib version of this yet? Very disappointing IMO. Regardless, I don't see how this can hurt so I'll probably merge soon.

@loggerheads-with-binary
Copy link
Author

loggerheads-with-binary commented Sep 6, 2023

Thanks!

Not related to your change, but is there really no stdlib version of this yet? Very disappointing IMO. Regardless, I don't see how this can hurt so I'll probably merge soon.

Yeah, there seems to be a couple of external libraries providing this trait. However, I believe given this project has zero dependencies, I implemented one within the library

Also, going through your Github issues, people seemed to be interested in compatibility with serde_json::Map, which is now compatible with as a feature.

@vitiral
Copy link
Owner

vitiral commented Sep 8, 2023

What are you thinking for version information. Does this require a semver change? I'm leaning no since no tests were changed yet they still pass.

@vitiral vitiral merged commit 2c02f24 into vitiral:master Sep 8, 2023
@vitiral
Copy link
Owner

vitiral commented Sep 8, 2023

Also, I switched to serde_json = { version = "1.0.0" , optional = true } since the Map trait exists in 1.0

https://docs.rs/serde_json/1.0.0/serde_json/struct.Map.html

vitiral added a commit that referenced this pull request Sep 8, 2023
@vitiral
Copy link
Owner

vitiral commented Sep 8, 2023

let me know if that sounds good and I can release the version

@loggerheads-with-binary
Copy link
Author

I believe crates.io will require some version change for a modified push
Also I believe the documentation may need to be updated. Do you want me to take care of the modified documentation or would you like to do that?

@vitiral
Copy link
Owner

vitiral commented Sep 8, 2023

Yes, please do!

I don't do rust anymore so this library is only maintained by contributors.

vitiral added a commit that referenced this pull request Sep 15, 2023
Messages:
* Revert "#35: v0.2.5 and use serde_json 1.0.0"
* Revert "Added feature flag (fixed serde_json as a feature)"
* Revert "Modified with trait"

commits:
* This reverts commit d00ed1a.
* This reverts commit 2c02f24.
* This reverts commit 4611d79.
@vitiral vitiral mentioned this pull request Sep 15, 2023
vitiral added a commit that referenced this pull request Sep 15, 2023
Messages:
* Revert "#35: v0.2.5 and use serde_json 1.0.0"
* Revert "Added feature flag (fixed serde_json as a feature)"
* Revert "Modified with trait"

commits:
* This reverts commit d00ed1a.
* This reverts commit 2c02f24.
* This reverts commit 4611d79.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants