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

Reimplement DSL #934

Merged
merged 12 commits into from
Jan 7, 2025
Merged

Reimplement DSL #934

merged 12 commits into from
Jan 7, 2025

Conversation

squell
Copy link
Member

@squell squell commented Dec 16, 2024

Closes #933

This also isolates all the 'Defaults'-behaviour in the defaults crate, making the exact behaviour of each individual Defaults setting irrelevant to the way the parser and semantic analysis handles them. This simplifies those parts.

The DSL definition itself also feels a little less magical (although there's some syntax-rules hacks there, it's slightly more "regular").

E.g. it now generates:

   struct Settings {
        always_query_group_plugin: bool,
        always_set_home: bool,
        env_reset: bool,
        mail_badpass: bool,
        match_group_by_gid: bool,
        use_pty: bool,
        visiblepw: bool,
        env_editor: bool,
        passwd_tries: i64,
        secure_path: Option<Box<str>>,
        verifypw: crate::defaults::enums::verifypw,
        timestamp_timeout: i64,
        env_keep: std::collections::HashSet<String>,
        env_check: std::collections::HashSet<String>,
        env_delete: std::collections::HashSet<String>,
    }

And impls Default for this struct, as well as define a fn negate and fn set that construct an object of the type pub type SettingsModifier = Box<dyn FnOnce(&mut Settings)>;. E.g.,

            "use_pty" => {
                Some(
                    crate::defaults::SettingKind::Flag(
                        Box::new(move |obj: &mut Settings| obj.use_pty = true),
                    ),
                )
            }

Note also that the enum-like sudoers settings are turned into an actual enum (albeit necessarily with uncommon name casing, but it's tucked away in defaults::enums)

    #[allow(non_camel_case_types)]
    mod enums {
        pub enum verifypw {
            all,
            always,
            any,
            never,
        }

Also, the DSL is built so you get compiler warnings if you define variabels that you do not use:

warning: multiple methods are never used
   --> src/defaults/mod.rs:24:5
    |
24  |     always_query_group_plugin = false
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^
25  |     always_set_home           = false
    |     ^^^^^^^^^^^^^^^
26  |     env_reset                 = true
    |     ^^^^^^^^^
27  |     mail_badpass              = true
    |     ^^^^^^^^^^^^
28  |     match_group_by_gid        = false
    |     ^^^^^^^^^^^^^^^^^^
29  |     use_pty                   = true
30  |     visiblepw                 = false
    |     ^^^^^^^^^
...
37  |     verifypw                  = all (!= never) [all, always, any, never]
    |     ^^^^^^^^
...
46  |     env_delete                = ["IFS", "CDPATH", "LOCALDOMAIN", "RES_OPTIONS", "HOSTALIASES",
    |     ^^^^^^^^^^
    |
   ::: src/defaults/settings_dsl.rs:172:9
    |
172 |         impl Settings {
    |         ------------- methods in this implementation
    |
    = note: `#[warn(dead_code)]` on by default
    = note: this warning originates in the macro `defaults` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `sudo-rs` (lib) generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.61s

@squell squell force-pushed the reimplement-dsl branch 6 times, most recently from d38696e to 4ec4c1a Compare December 16, 2024 17:06
This is more 'statically' typed, e.g. no more need for 'late unwraps'
whose success can only be determined by careful programming, i.e. a
defaults::Settings behaves more like Rust-struct whereas the curent
sudoers::Settings behaves like a Python structs.
@squell squell marked this pull request as ready for review December 17, 2024 21:23
@squell squell added C-checker Permission checking logic C-parser Parser/AST suggestion and removed C-checker Permission checking logic suggestion labels Dec 18, 2024
src/defaults/settings_dsl.rs Outdated Show resolved Hide resolved
src/defaults/settings_dsl.rs Outdated Show resolved Hide resolved
@squell squell enabled auto-merge January 7, 2025 11:18
@squell squell merged commit e0fff40 into main Jan 7, 2025
13 checks passed
@squell squell deleted the reimplement-dsl branch January 7, 2025 11:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-parser Parser/AST
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove 'late unwraps' in DSL
2 participants