rust-cli / rust-cli/config-rs

How to write a configuration file for `.env` with the above structure `NotiferConfig`?

Open
#658 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
3.2k
Forks
265
Avg merge
2h 42m
Merged PRs (30d)
4

Description

rust code:

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WebhookConfig {
    pub endpoint: String,
    pub auth_token: Option<String>,
    // pub custom_headers: Option<HashMap<String, String>>,
    pub max_retries: u32,
    pub timeout: u64,
}

// Configuration for the Kafka adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KafkaConfig {
    pub brokers: String,
    pub topic: String,
    pub max_retries: u32,
    pub timeout: u64,
}

/// Configuration for the MQTT adapter.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqttConfig {
    pub broker: String,
    pub port: u16,
    pub client_id: String,
    pub topic: String,
    pub max_retries: u32,
}

/// Configuration for the notification system.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum AdapterConfig {
    Webhook(WebhookConfig),
    Kafka(KafkaConfig),
    Mqtt(MqttConfig),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NotifierConfig {
    #[serde(default = "default_store_path")]
    pub store_path: String,
    #[serde(default = "default_channel_capacity")]
    pub channel_capacity: usize,
    pub adapters: Vec<AdapterConfig>,
}

toml file:

# config.toml
store_path = "/var/log/event-notifier"
channel_capacity = 5000

[[adapters]]
type = "Webhook"
endpoint = "https://api.example.com/webhook/toml"
auth_token = "your-auth-token"
max_retries = 3
timeout = 50

[adapters.0.custom_headers]
X-Custom = "value"

[[adapters]]
type = "Kafka"
brokers = "localhost:9092"
topic = "notifications"
max_retries = 3
timeout = 60

[[adapters]]
type = "Mqtt"
broker = "mqtt.example.com"
port = 1883
client_id = "event-notifier"
topic = "events"
max_retries = 3

env file:

# Global config
NOTIFIER_STORE_PATH=/var/log/event-notification
NOTIFIER_CHANNEL_CAPACITY=5000

# Adapters (array)
NOTIFIER_ADAPTERS_0_TYPE=Webhook
NOTIFIER_ADAPTERS_0_ENDPOINT=https://api.example.com/webhook
NOTIFIER_ADAPTERS_0_AUTH_TOKEN=your-token
NOTIFIER_ADAPTERS_0_MAX_RETRIES=3
NOTIFIER_ADAPTERS_0_TIMEOUT=50

NOTIFIER_ADAPTERS_1_TYPE=Kafka
NOTIFIER_ADAPTERS_1_BROKERS=localhost:9092
NOTIFIER_ADAPTERS_1_TOPIC=notifications
NOTIFIER_ADAPTERS_1_MAX_RETRIES=3
NOTIFIER_ADAPTERS_1_TIMEOUT=60

NOTIFIER_ADAPTERS_2_TYPE=Mqtt
NOTIFIER_ADAPTERS_2_BROKER=mqtt.example.com
NOTIFIER_ADAPTERS_2_PORT=1883
NOTIFIER_ADAPTERS_2_CLIENT_ID=event-notifier
NOTIFIER_ADAPTERS_2_TOPIC=events
NOTIFIER_ADAPTERS_2_MAX_RETRIES=3

read .env file error:

Failed to deserialize config: invalid type: map, expected a sequence for key `adapters`
# Global config
NOTIFIER_STORE_PATH=/var/log/event-notification
NOTIFIER_CHANNEL_CAPACITY=5000

# Adapters (array)
NOTIFIER_ADAPTERS_0__TYPE=Webhook
NOTIFIER_ADAPTERS_0__ENDPOINT=https://api.example.com/webhook
NOTIFIER_ADAPTERS_0__AUTH_TOKEN=your-token
NOTIFIER_ADAPTERS_0__MAX_RETRIES=3
NOTIFIER_ADAPTERS_0__TIMEOUT=50

NOTIFIER_ADAPTERS_1__TYPE=Kafka
NOTIFIER_ADAPTERS_1__BROKERS=localhost:9092
NOTIFIER_ADAPTERS_1__TOPIC=notifications
NOTIFIER_ADAPTERS_1__MAX_RETRIES=3
NOTIFIER_ADAPTERS_1__TIMEOUT=60

NOTIFIER_ADAPTERS_2__TYPE=Mqtt
NOTIFIER_ADAPTERS_2__BROKER=mqtt.example.com
NOTIFIER_ADAPTERS_2__PORT=1883
NOTIFIER_ADAPTERS_2__CLIENT_ID=event-notifier
NOTIFIER_ADAPTERS_2__TOPIC=events
NOTIFIER_ADAPTERS_2__MAX_RETRIES=3

read .env file error:

Failed to deserialize config: missing field `adapters`

#631 #536 @epage

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named. Start by tracing environment-source deserialization for the NotifierConfig adapters Vec and compare it with the TOML representation. Done means the expected environment-variable format is documented or supported consistently, with a regression test if the behavior is intended.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.