{"id":27928659,"url":"https://github.com/canop/iq","last_synced_at":"2025-05-07T02:41:12.981Z","repository":{"id":266645361,"uuid":"898962531","full_name":"Canop/iq","owner":"Canop","description":"introspect query","archived":false,"fork":false,"pushed_at":"2025-04-02T11:15:30.000Z","size":57,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-04T07:14:58.535Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Canop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-05T11:14:51.000Z","updated_at":"2025-04-05T21:12:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"c23d850e-a239-443e-92ee-376e2f1ae6c0","html_url":"https://github.com/Canop/iq","commit_stats":null,"previous_names":["canop/iq"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Fiq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Fiq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Fiq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Canop%2Fiq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Canop","download_url":"https://codeload.github.com/Canop/iq/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252802471,"owners_count":21806511,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-07T02:41:12.361Z","updated_at":"2025-05-07T02:41:12.959Z","avatar_url":"https://github.com/Canop.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introspect Query\n\n[![MIT][s2]][l2] [![Latest Version][s1]][l1] [![docs][s3]][l3] [![Chat on Miaou][s4]][l4]\n\n[s1]: https://img.shields.io/crates/v/iq.svg\n[l1]: https://crates.io/crates/iq\n\n[s2]: https://img.shields.io/badge/license-MIT-blue.svg\n[l2]: LICENSE\n\n[s3]: https://docs.rs/iq/badge.svg\n[l3]: https://docs.rs/iq/\n\n[s4]: https://miaou.dystroy.org/static/shields/room.svg\n[l4]: https://miaou.dystroy.org/3768?rust\n\n\nIQ (Introspect Query) lets you query standard structs, maps, enums, arrays, tuples, and\nnested combinations of these, to get deep values with a simple path syntax.\n\nValues jut have to implement serde's `Serialize` trait.\nBoth values and queries are dynamic, and can be provided at runtime.\n\nIQ is efficient: the explored value isn't serialized, the `Serialize` trait is used to visit it and the visit goes only to the desired target, skipping other branches.\n\nSee the [IQ](trait.IQ.html) trait for all extract functions.\n\n```rust\nuse iq::IQ;\nuse serde::{ Deserialize, Serialize };\n\n#[derive(Debug, Serialize)]\nstruct Car {\n    pub engine: String,\n    pub passengers: Vec\u003cDog\u003e,\n    pub driver: Dog,\n}\n#[derive(Debug, PartialEq, Serialize, Deserialize)]\nstruct Dog {\n    pub name: String,\n    pub ears: u8,\n}\n\nlet car = Car {\n    engine: \"V8\".to_string(),\n    passengers: vec![\n        Dog {\n            name: \"Roverandom\".to_string(),\n            ears: 1,\n        },\n        Dog {\n            name: \"Laïka\".to_string(),\n            ears: 2,\n        },\n    ],\n    driver: Dog {\n        name: \"Rex\".to_string(),\n        ears: 2,\n    },\n};\n\n// extract \"primitive\" values as strings with extract_primitive\nassert_eq!(car.extract_primitive(\"driver.ears\").unwrap(), \"2\");\nassert_eq!(car.extract_primitive(\"driver.name\").unwrap(), \"Rex\");\nassert_eq!(car.extract_primitive(\"passengers.1.name\").unwrap(), \"Laïka\");\nassert_eq!(car.extract_primitive(\"passengers.1\"), None); // it's not a primitive\n\n// extract any value as Json with extract_json\nassert_eq!(car.extract_json(\"wrong.path\"), None);\nassert_eq!(car.extract_json(\"driver.ears\").unwrap(), \"2\");\nassert_eq!(car.extract_json(\"driver.name\").unwrap(), r#\"\"Rex\"\"#);\nassert_eq!(\n    car.extract_json(\"passengers.0\").unwrap(),\n    r#\"{\"name\":\"Roverandom\",\"ears\":1}\"#\n);\nassert_eq!(car.extract_json(\"passengers.3\"), None);\n\n// extract any deserializable value with extract_value\nassert_eq!(car.extract_value(\"driver.ears\").unwrap(), Some(2));\nassert_eq!(\n    car.extract_value(\"passengers.1\").unwrap(),\n    Some(Dog {\n        name: \"Laïka\".to_string(),\n        ears: 2\n    }),\n);\n\n// You don't have to concat tokens if you build the path\nassert_eq!(\n    car.extract_primitive(vec![\"passengers\", \"0\", \"ears\"])\n        .unwrap(),\n    \"1\"\n);\n\n// Extract functions are available both on the IQ trait and as standalone functions.\nassert_eq!(iq::extract_primitive(\u0026car, \"driver.name\").unwrap(), \"Rex\");\n\n// You can extract the size of the deep array/tuple/map/struct/string\nassert_eq!(car.extract_size(\"passengers\"), Some(2)); // count of array items\nassert_eq!(car.extract_size(\"passengers.1.name\"), Some(5)); // count of chars in \"Laïka\"\nassert_eq!(car.extract_size(\"passengers.1\"), Some(2)); // count of fields in the Dog struct\nassert_eq!(car.extract_size(\"passengers.3\"), None); // There's no third passenger\nassert_eq!(car.extract_size(\"\"), Some(3)); // count of fields in the Car struct\nassert_eq!(iq::size_of(\u0026car), Some(3)); // same than previous, when you don't want to dive\nassert_eq!(iq::size_of(\u0026(\"a\", 1)), Some(2)); // works with tuples too\n\n// If iq is compiled with the \"template\" feature, you get a mini templating utility\nlet template = iq::Template::new(\"{driver.name} drives a {engine} car.\");\nassert_eq!(template.render(\u0026car), \"Rex drives a V8 car.\");\n\n```\n\nIQ also works with enums, maps, and tuples: more tests can be found in libs.rs.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanop%2Fiq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcanop%2Fiq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanop%2Fiq/lists"}