shm-rs

A Rust crate which allows to dynamically configure and read the schema based configuration files and also serialize data into JSON or binary files so later it could be deserialized into structs directly.

This crate also capable to construct a Rust structs and enumerators from the static schema files.

A statis scheme file is a file which contains a description of: - how to read user provided config file - how to store read data - how to serialize it (in which order)

A dynamic scheme file is a file which contains configurations which is required to read and serialize.

For the syntax see /docs/.

Sequence

text ┌───────────────┐ ┌──────────────┐ │ scheme.shm │ │ config.shm │ └───────┬───────┘ └───────┬──────┘ │ │ │ │ │ │ ┌───────▼───────┐ ┌───────▼────────┐ │ │ │ │ │ File / Buffer │ │ File / Buffer │ │ │ │ │ └──────┬────────┘ └───────┬────────┘ │ │ │stream │stream │ │ ┌──────▼────────┐ ┌─────▼─────┐ │ Lexer │ │ Lexer │ └──────┬────────┘ └─────┬─────┘ │ │ │Nodes │Nodes │ │ ┌──────▼────────┐ ┌───────▼─────────┐ │ │ │ │ │ StaticScheme ├────────┬────► DynamicScheme │ │ │ │ │ │ └──────┬────────┘ │ └───────┬─────────┘ │ Structures │ │ │ Description │ │Parsed Data │ │ │ ┌──────▼────────┐ │ ┌───────▼─────────┐ │ Config │ │ │ Collected Data │ │ Structure │ │ │ Structured Data │ │ │ │ │ │ └──────┬────────┘ │ └───────┬─────────┘ │ │ │ │ │ │ ┌──────▼────────┐ │ ┌───────▼─────────┐ │ Rust Code │ │ │ │ │ Structures │ └────► Serializator │ │ Enums │ │ │ └───────────────┘ └───────┬─────────┘ │ │ ┌───────▼─────────┐ │ Serialized Data │ │ │ │ Binary or JSON │ └─────────────────┘

Example

Example of dynamic scheme file ```scheme (machine (address "01:40/5010-A3F.0") ; router's address (to access admin panel) (address "01:40/5010-A40.0") ; another adress (to access admin panel) )

;; A 100MBit interface uplink from ;; Clients from floor 2 using only this connection (interface "wan0" (phys-location "00:00:01") (address "01:40/5010-A3F") (mtu 2048u) ; 2 kiB (address-mask "01:40/5010-(A3F-A4A)") )

;; A 1Gbit interface uplink from (interface "wwan1" (phys-location "00:00:02") (address "01:40/5010-A3F") (mtu 2048u) (address-mask "01:40/5010-(A3F-A4A)") )</p> <p>;; All client machines connected to router (floor 1) (interface "lan0" (phys-location "00:01:01") (address "01:40/5010-A3F") (mtu 2048u) (address-mask "01:40/5010-A3F.(0-FFFF)") )</p> <p>;; All client machines connected to router (floor 2) (interface "lan1" (phys-location "00:01:02") (address "01:40/5010-A40") (mtu 2048u) (address-mask "01:40/5010-A40.(0-FFFF)") )</p> <p>;; Route announcement ;; Clients on lan0 would receive: ;; to: 01:40/5010-(A3F-A40) metric: 1 ;; to: 01:40/5010-any metric 1 ;; to: any metric 1 (route-table "global" (interface-alias "lan0" ; announce the following routes on interface lan0 (route-to "01:40/5010-(A3F-A40).<em>" (metric 1u) ) (route-to "01:40/5010-</em>" (via "wan0") (metric auto) ) ; routing globally to whole internet (route-to "*" (via "wwan1") (metric auto) ) )</p> <pre><code>(interface-alias "lan1" (route-to "01:40/5010-(A3F-A40).*" (metric 1u) ) (route-to "*" (via "wan0") (metric auto) ) ; do not route to global internet via wwan1 ) ;; inbound routing ;; announce to uplink that we have something on our network ;; if there are any machines that is local only then don't announce the ;; adress to uplink, so no route to host would be returned by uplink for the ;; remote host. (interface-alias "wan0" (route-to "01:40/5010-A3F.*" (via "lan0") (metric 1u) ) (route-to "01:40/5010-A40.*" (via "lan1") (metric 1u) ) ) (interface-alias "wwan1" (route-to "01:40/5010-A3F.*" (via "lan0") (metric 1u) ) ; no route to lan1 members via this interface ) </code></pre> <p>)</p> <p>```</p> <p>For the above the following static scheme is required in order to read and serialize it: ```scheme (version 1000)</p> <p>(serializator "networking" (define "auto" 0u '("metric"))</p> <pre><code>(procedure "address" (arg "a_addr" string) ) ;(struct "NetAddr" "address" ; (field "net_addr" (f/string '("a_addr"))) ;) ; -- MACHINE (procedure "machine" (proc "p_address" '("address") (allow '(collection))) ) (struct "NetMachine" "machine" ;(field "nm_addrs" (f/vector (f/struct '("p_address")))) (field "nm_addrs" (f/vector (f/string '("p_address" "a_addr")))) ) ; -- COMMON (procedure "phys-location" (arg "a_phys_loc" string) ) (procedure "mtu" (arg "a_mtu" uint) ) (procedure "address-mask" (arg "a_addr_mask" string) ) ; -- INTERFACE (procedure "interface" (arg "a_if_alias" string) (proc "p_phys_loc" '("phys-location")) (proc "p_addr" '("address")) (proc "p_mtu" '("mtu") (allow '(optional))) (proc "p_address_mask" '("address-mask")) ) (struct "NetInterface" "interface" (field "ni_ifalias" (f/string '("a_if_alias"))) (field "ni_ifloc" (f/string '("p_phys_loc" "a_phys_loc"))) (field "ni_addr" (f/string '("p_addr" "a_addr"))) (field "ni_ifmtu" (f/optional) (f/uint '("p_mtu" "a_mtu"))) (field "ni_ifaddr_mask" (f/string '("p_address_mask" "a_addr_mask"))) ) ; -- ROUTE-TABLE (procedure "via" (arg "a_via" string) ) (procedure "metric" (arg "a_metric" symbol uint) ) (procedure "route-to" (arg "a_route_addr" string) (proc "p_via" '("via") (allow '(optional))) (proc "p_metric" '("metric")) ) (struct "NetRouteTo" "route-to" (field "rt_dest" (f/string '("a_route_addr"))) (field "rt_via" (f/optional) (f/string '("p_via" "a_via"))) (field "rt_metric" (f/uint '("p_metric" "a_metric"))) ) (procedure "interface-alias" (arg "a_route_title" string) (proc "p_route_to" '("route-to") (allow '(collection))) ) (struct "NetInterfaceAlias" "interface-alias" (field "if_alias" (f/string '("a_route_title"))) (field "if_routes_to" (f/vector (f/struct '("p_route_to")))) ) (procedure "route-table" (arg "a_route_title" string) (proc "p_route_tables" '("interface-alias") (allow '(collection optional))) ) (struct "NetRouteTable" "route-table" (field "rt_title" (f/string '("a_route_title"))) (field "rt_tbls" (f/optional) (f/vector (f/struct '("p_route_tables")))) ) ; -- ROOT (rootprocedure (proc "p_machine" '("machine")) (proc "p_interface" '("interface") (allow '(collection))) (proc "p_route_tables" '("route-table") (allow '(collection))) ) (rootstruct "Network" (field "net_machine" (f/struct '("p_machine"))) (field "net_ifs" (f/vector (f/struct '("p_interface")))) (field "net_rt_tbls" (f/vector (f/struct '("p_route_tables")))) ) </code></pre> <p>)</p> <p>```</p> </body></html>