Skip to content

Instantly share code, notes, and snippets.

@zekefast
Created January 26, 2026 21:38
Show Gist options
  • Select an option

  • Save zekefast/afc68478b56e2c065373b9875091d06d to your computer and use it in GitHub Desktop.

Select an option

Save zekefast/afc68478b56e2c065373b9875091d06d to your computer and use it in GitHub Desktop.
Solution to Affinidi's coding challange on Staff Software Engineer (Rust) position
[package]
name = "coding_challange"
version = "0.1.0"
edition = "2024"
[dependencies]
serde_json = "1.0.149"
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);
}
}
@zekefast
Copy link
Author

zekefast commented Jan 26, 2026

Run in Rust Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment