{"id":13732329,"url":"https://github.com/rukai/treeflection","last_synced_at":"2025-03-17T04:32:28.148Z","repository":{"id":57670581,"uuid":"72254244","full_name":"rukai/treeflection","owner":"rukai","description":"A rust library that provides pseudo-reflection for structs and enums","archived":false,"fork":false,"pushed_at":"2020-10-02T12:17:19.000Z","size":140,"stargazers_count":22,"open_issues_count":6,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-14T11:49:27.258Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","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/rukai.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-29T00:37:30.000Z","updated_at":"2023-12-26T03:02:00.000Z","dependencies_parsed_at":"2022-09-26T20:41:10.000Z","dependency_job_id":null,"html_url":"https://github.com/rukai/treeflection","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rukai%2Ftreeflection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rukai%2Ftreeflection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rukai%2Ftreeflection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rukai%2Ftreeflection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rukai","download_url":"https://codeload.github.com/rukai/treeflection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221671999,"owners_count":16861345,"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":"2024-08-03T02:01:53.246Z","updated_at":"2024-10-27T11:57:46.522Z","avatar_url":"https://github.com/rukai.png","language":"Rust","readme":"# Treeflection [![Build Status](https://travis-ci.org/rukai/treeflection.svg?branch=master)](https://travis-ci.org/rukai/treeflection) [![Crates.io](https://img.shields.io/crates/v/treeflection.svg)](https://crates.io/crates/treeflection) [![Docs](https://docs.rs/treeflection/badge.svg)](https://docs.rs/treeflection)\n## treeflection_derive [![Crates.io](https://img.shields.io/crates/v/treeflection_derive.svg)](https://crates.io/crates/treeflection_derive) [![Docs](https://docs.rs/treeflection_derive/badge.svg)](https://docs.rs/treeflection_derive)\n\nTreeflection runs a command stored as a string on a tree of structs, collections and primitive types.\n\n## Commands\n\nA command to set an int in a Vec in a struct in another struct in a Hashmap to the value 50 looks like:\n`someHashMap[\"key\"].someChild.anotherChild[0]:set 50`\n\nFor the full syntax take a look at the [Command Manual](commandManual.md)\n\n## Usage\n\nThe `Node` trait must be implemented for every type in the tree.\nThen a new `NodeRunner` is created using the command string and passed to the node_step method of the root node.\nThe `NodeRunner` is then passed to the children specified in the command and then runs the command on the final specified child.\nUse the treeflection_derive crate to #[Derive(Node)] your own structs or write your own handlers.\n\n### Vec example\n\n```rust\nextern crate treeflection;\nuse treeflection::{NodeRunner, Node};\n\npub fn main() {\n    let mut test_vec = vec!(0, 413, 358, 42);\n\n    let command = \"[1]:get\";\n    let result = test_vec.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"413\");\n\n    let command = \"[1]:set 1111\";\n    let result = test_vec.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"\");\n\n    let command = \"[1]:get\";\n    let result = test_vec.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"1111\");\n}\n```\n\n### Custom struct example\n\nUse the treeflection_derive crate to #[Derive(Node)] your own structs or write your own handlers.\nYour structs also need to impl the traits Serialize, Deserialize, Default and Clone as well as `extern crate serde_json`.\nThis is because serde_json is used to get/set entire structs.\n\nCurrently `use treeflection::{NodeRunner, Node, NodeToken};` must be included so the macro can access these types.\n\n```rust\nextern crate treeflection;\n#[macro_use] extern crate treeflection_derive;\n#[macro_use] extern crate serde_derive;\nextern crate serde_json;\n\nuse treeflection::{NodeRunner, Node, NodeToken};\n\n#[derive(Node, Serialize, Deserialize, Default, Clone)]\nstruct SolarSystem {\n    pub mercury:  Planet,\n    pub earth:    Planet,\n    pub mars:     Planet,\n        planet_x: Planet,\n}\n\nimpl SolarSystem {\n    pub fn new() -\u003e SolarSystem {\n        SolarSystem {\n            mercury:  Planet { radius: 2440.0 },\n            earth:    Planet { radius: 6371.0 },\n            mars:     Planet { radius: 3390.0 },\n            planet_x: Planet { radius: 1337.0 },\n        }\n    }\n}\n\n#[NodeActions(\n    // we want the function circumference to be accessible via treeflection by the same name\n    NodeAction(function=\"circumference\", return_string),\n\n    // we want the function explode_internal_naming_scheme to be accessible via treeflection\n    // by the name explode and we want to ignore its return value so that it will compile despite not returning a String\n    NodeAction(action=\"explode\", function=\"explode_internal_naming_scheme\"),\n)]\n#[derive(Node, Serialize, Deserialize, Default, Clone)]\nstruct Planet {\n    pub radius: f32\n}\n\nimpl Planet {\n    pub fn circumference(\u0026self) -\u003e String {\n        (self.radius * 2.0 * std::f32::consts::PI).to_string()\n    }\n\n    pub fn explode_internal_naming_scheme(\u0026mut self) {\n        self.radius = 0.0;\n    }\n}\n\n\npub fn main() {\n    let mut ss = SolarSystem::new();\n\n    // serialize the whole struct into json\n    let command = \":get\";\n    let result = ss.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result,\nr#\"{\n  \"mercury\": {\n    \"radius\": 2440.0\n  },\n  \"earth\": {\n    \"radius\": 6371.0\n  },\n  \"mars\": {\n    \"radius\": 3390.0\n  },\n  \"planet_x\": {\n    \"radius\": 1337.0\n  }\n}\"#);\n\n    // access properties\n    let command = \"earth.radius:get\";\n    let result = ss.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"6371\");\n\n    // call methods on the struct\n    let command = \"earth:circumference\";\n    let result = ss.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"40030.176\");\n\n    let command = \"earth:explode\";\n    let result = ss.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"\");\n    assert_eq!(ss.earth.radius, 0.0);\n\n    // private properties are not accessible via treeflection\n    let command = \"planet_x:get\";\n    let result = ss.node_step(NodeRunner::new(command).unwrap());\n    assert_eq!(result, \"SolarSystem does not have a property 'planet_x'\");\n}\n```\n\n## Contributing\n\nThis library is designed around the specific needs of [Canon Collision](https://github.com/rukai/canon_collision).\nPull requests are welcome but if the changes go against the needs of Canon Collision you will be stuck with your own fork. :)\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frukai%2Ftreeflection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frukai%2Ftreeflection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frukai%2Ftreeflection/lists"}