Created
January 26, 2026 21:38
-
-
Save zekefast/afc68478b56e2c065373b9875091d06d to your computer and use it in GitHub Desktop.
Solution to Affinidi's coding challange on Staff Software Engineer (Rust) position
This file contains hidden or 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
| [package] | |
| name = "coding_challange" | |
| version = "0.1.0" | |
| edition = "2024" | |
| [dependencies] | |
| serde_json = "1.0.149" |
This file contains hidden or 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
| use serde_json::json; | |
| use serde_json::Value; | |
| use serde_json::Map; | |
| const KEY_SEPARATOR: &str = "."; | |
| fn main() {} | |
| fn filter<'a>(mut json: Value, paths: impl Iterator<Item = &'a str>) -> Value { | |
| serde_json::to_value(paths.fold(Map::new(), |mut acc, path: &str| { | |
| let mut parts = path.split(KEY_SEPARATOR); | |
| if let (Some(head), tail) = (parts.next(), parts) | |
| && let Some(json) = json.get_mut(head) | |
| { | |
| let mut tail = tail.peekable(); | |
| acc.insert( | |
| head.to_owned(), | |
| if tail.peek().is_some() { | |
| filter(json.take(), tail) | |
| } else { | |
| json.take() | |
| }); | |
| } | |
| acc | |
| })).unwrap() | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use super::*; | |
| #[test] | |
| fn empty() { | |
| let given = json!({}); | |
| let expected = json!({}); | |
| let mask = vec!["a", "b", "c"]; | |
| assert_eq!(filter(given, mask.into_iter()), expected); | |
| } | |
| #[test] | |
| fn nested() { | |
| let given = json!({ | |
| "a": { | |
| "b": 1, | |
| "c": 2, | |
| }, | |
| "d": 3, | |
| "e": 4, | |
| }); | |
| let expected = json!({ | |
| "a": { | |
| "b": 1, | |
| }, | |
| "d": 3, | |
| }); | |
| let mask = vec!["a.b", "d"]; | |
| assert_eq!(filter(given, mask.into_iter()), expected); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run in Rust Playground.