Skip to content

Commit

Permalink
models: Add Strength enum and Setting-Generator struct
Browse files Browse the repository at this point in the history
We need to add these to match changes in Bottlerocket-core-kit
Refer commit:
bottlerocket-os/bottlerocket-core-kit@a72f6bd

PR: bottlerocket-os/bottlerocket-core-kit#294
  • Loading branch information
vyaghras committed Dec 5, 2024
1 parent 10d536e commit dd26889
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions sources/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sources/models/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bottlerocket-release.workspace = true
libc.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_plain.workspace = true
toml.workspace = true

# settings plugins
Expand Down
54 changes: 54 additions & 0 deletions sources/models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use bottlerocket_release::BottlerocketRelease;
use bottlerocket_settings_models::model_derive::model;
use bottlerocket_settings_plugin::BottlerocketSettings;
use serde::{Deserialize, Serialize};
use serde_plain::derive_fromstr_from_deserialize;
use std::collections::HashMap;

use bottlerocket_settings_models::modeled_types::SingleLineString;
Expand Down Expand Up @@ -87,3 +88,56 @@ struct Report {
name: String,
description: String,
}

/// Strength represents whether the settings can be overridden by a setting generator or not.
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq)]
#[serde(rename_all = "kebab-case")]
pub enum Strength {
Strong,
Weak,
}

impl std::fmt::Display for Strength {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Strength::Strong => write!(f, "strong"),
Strength::Weak => write!(f, "weak"),
}
}
}

derive_fromstr_from_deserialize!(Strength);

/// Struct to hold the setting generator definition containing
/// command, strength, skip-if-populated
#[derive(Deserialize, Serialize, std::fmt::Debug, PartialEq)]

pub struct SettingsGenerator {
pub command: String,
pub strength: Strength,
#[serde(rename = "skip-if-populated")]
pub skip_if_populated: bool,
}

impl SettingsGenerator {
pub fn is_weak(&self) -> bool {
self.strength == Strength::Weak
}

pub fn with_command(command: String) -> Self {
SettingsGenerator {
command,
..SettingsGenerator::default()
}
}
}

impl Default for SettingsGenerator {
fn default() -> Self {
SettingsGenerator {
command: String::new(),
strength: Strength::Strong,
skip_if_populated: true,
}
}
}

0 comments on commit dd26889

Please sign in to comment.