Environment Variable Configuration in Rust
This challenge focuses on reading and utilizing environment variables within a Rust program. Environment variables are a common way to configure applications without modifying the code itself, allowing for flexibility across different deployments. Your task is to create a Rust program that reads specific environment variables and uses their values to configure its behavior.
Problem Description
You need to write a Rust program that reads three environment variables: API_KEY, LOG_LEVEL, and PORT. The program should then print these values to the console in a formatted way. If any of the environment variables are not set, the program should print a helpful error message indicating which variable is missing and exit with a non-zero exit code. The LOG_LEVEL variable should be treated as an enum, with possible values "DEBUG", "INFO", and "ERROR". If the LOG_LEVEL is not one of these values, print an error and exit. The PORT variable should be parsed as an unsigned 16-bit integer. If parsing fails, print an error and exit.
Key Requirements:
- Error Handling: Gracefully handle missing environment variables and invalid
LOG_LEVELvalues. - Type Safety: Parse the
PORTvariable as anu16. - Formatted Output: Print the retrieved values in a clear and readable format.
- Exit Codes: Use a non-zero exit code (e.g., 1) to indicate an error.
- Enum Usage: Use an enum to represent the
LOG_LEVEL.
Expected Behavior:
- The program should attempt to read the
API_KEY,LOG_LEVEL, andPORTenvironment variables. - If all variables are found and valid, the program should print a message like: "API Key: <value>, Log Level: <value>, Port: <value>".
- If any variable is missing, the program should print an error message like: "Error: Missing environment variable API_KEY" and exit with code 1.
- If the
LOG_LEVELis invalid, the program should print an error message like: "Error: Invalid log level. Must be DEBUG, INFO, or ERROR" and exit with code 1. - If the
PORTcannot be parsed as au16, the program should print an error message like: "Error: Invalid port number" and exit with code 1.
Examples
Example 1:
Input:
API_KEY=my_secret_key
LOG_LEVEL=INFO
PORT=8080
Output:
API Key: my_secret_key, Log Level: INFO, Port: 8080
Explanation: All environment variables are set correctly.
Example 2:
Input:
LOG_LEVEL=DEBUG
PORT=8080
Output:
Error: Missing environment variable API_KEY
Explanation: The API_KEY environment variable is not set.
Example 3:
Input:
API_KEY=my_secret_key
LOG_LEVEL=WARNING
PORT=8080
Output:
Error: Invalid log level. Must be DEBUG, INFO, or ERROR
Explanation: The LOG_LEVEL is not a valid value.
Example 4:
Input:
API_KEY=my_secret_key
LOG_LEVEL=INFO
PORT=abc
Output:
Error: Invalid port number
Explanation: The PORT cannot be parsed as a u16.
Constraints
- The program must compile and run without external dependencies beyond the Rust standard library.
- The program should handle potential errors gracefully and provide informative error messages.
- The
PORTmust be parsed as anu16. - The
LOG_LEVELmust be one of the following values: "DEBUG", "INFO", or "ERROR". - The program should exit with a non-zero exit code (1) upon encountering an error.
Notes
- Consider using the
std::envmodule to access environment variables. - Use
Resultto handle potential errors during parsing and variable retrieval. - An
enumis a good way to represent the different possible values forLOG_LEVEL. - Remember to handle the
unwrap()orexpect()calls carefully to avoid panics. Usematchorif letfor safer error handling. - Think about how to best format the output for readability.