Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INFRA - Config parsing #86

Merged
merged 3 commits into from
Feb 18, 2025
Merged

INFRA - Config parsing #86

merged 3 commits into from
Feb 18, 2025

Conversation

TomLonergan03
Copy link
Contributor

An unwise man has produced a yaml to rust transpiler because lmao. It is on crates.io (source here) should you ever need it.

what it does:
turns this

parsing: "working"
age: 22
enabled: true
time: 22.34
recurse:
  thing: 1
  recurse:
    thing: 3
array: ["a", "b", "c"]
empty_array: []
array_of_arrays:
  - ["a", "b", "c"]
  - ["d", "e", "f"]
array_of_objects:
  - name: "a"
    age: 1
  - name: "b"
    age: 2
  - name: "c"
    age: 3

into

struct Config {
    pub parsing: &'static str,
    pub age: i64,
    pub enabled: bool,
    pub time: f64,
    pub recurse: ConfigRecurse,
    pub array: [&'static str; 3usize],
    pub empty_array: [(); 0usize],
    pub array_of_arrays: [[&'static str; 3usize]; 2usize],
    pub array_of_objects: [ConfigArrayOfObjects; 3usize],
}

struct ConfigRecurse {
    pub thing: i64,
    pub recurse: ConfigRecurseRecurse,
}

struct ConfigRecurseRecurse {
    pub thing: i64,
}

struct ConfigArrayOfObjects {
    pub name: &'static str,
    pub age: i64,
}

pub const CONFIG: Config = Config {
    parsing: "working",
    age: 22i64,
    enabled: true,
    time: 22.34f64,
    recurse: ConfigRecurse {
        thing: 1i64,
        recurse: ConfigRecurseRecurse { thing: 3i64 },
    },
    array: ["a", "b", "c"],
    empty_array: [],
    array_of_arrays: [["a", "b", "c"], ["d", "e", "f"]],
    array_of_objects: [
        ConfigArrayOfObjects {
            name: "a",
            age: 1i64,
        },
        ConfigArrayOfObjects {
            name: "b",
            age: 2i64,
        },
        ConfigArrayOfObjects {
            name: "c",
            age: 3i64,
        },
    ],
};

you can now access config values by doing

// items in a hashmap
assert_eq!(CONFIG.parsing, "working");

// items in a list
assert_eq!(CONFIG.array[1], "b");

// nested hashmaps
assert_eq!(CONFIG.recurse.recurse.thing, 3);

all your favourite rust types are here (if those are i64/f64/&'static str/bool)! and because of const, it all gets inlined at compile time so your billion line yaml file gets optimised down to only the exact values you care about (i think)

two restrictions:

  • arrays of things: everything in an array must be the same type
  • keys: names of entries must not use rust reserved words, most likely no type is the main restriction here but also for, struct, etc.

also i 100% broke telemetry by changing the field names lol

@TomLonergan03
Copy link
Contributor Author

TomLonergan03 commented Feb 16, 2025

huh i gotta figure out how to do the config file path for the nested boards. will push a new version tomorrow maybe

@davidbeechey davidbeechey mentioned this pull request Feb 17, 2025
Copy link
Collaborator

@davidbeechey davidbeechey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very epic, can merge once the board code is fixed

@TomLonergan03 TomLonergan03 self-assigned this Feb 18, 2025
Copy link
Contributor

@arjunnaha arjunnaha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The non-Rust contingent of the Edinburgh Hyperloop Team, otherwise known as the telemetry team, approves :)

My kindest ever regards,
Mr. Arjun Naha
(Soon to be) Bachelors of Engineering from The University of Edinburgh, Influencing the world since 1583

@TomLonergan03
Copy link
Contributor Author

Am setting an env var in build.rs, which is used to set the config path.

The other option i have working is using nightly to use Span if you enable the feature relative-to-macro, which then will use a path relative to the macro invocation, which means it works with the board builds

@davidbeechey davidbeechey merged commit 9822e28 into main Feb 18, 2025
14 of 15 checks passed
@davidbeechey davidbeechey deleted the toml/config branch February 18, 2025 16:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants