feat(rust): add configuration parsing and an error type
This commit is contained in:
@@ -6,7 +6,13 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
tokio = { version = "*", features = ["full"] }
|
tokio = { version = "*", features = ["full"] }
|
||||||
serde = { version = "*", features = ["derive"] }
|
serde = { version = "*", features = ["derive"] }
|
||||||
|
either = { version = "*", features = ["serde"] }
|
||||||
|
|
||||||
thiserror = "*"
|
thiserror = "*"
|
||||||
clap = { version = "*", features = ["derive"] }
|
anyhow = "*"
|
||||||
tracing = "*"
|
tracing = "*"
|
||||||
tracing-subscriber = "*"
|
tracing-subscriber = "*"
|
||||||
|
|
||||||
|
clap = { version = "*", features = ["derive"] }
|
||||||
|
figment = { version = "*", features = ["toml", "env", "json"] }
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,27 @@
|
|||||||
|
use either::{Either, Either::Left};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
struct CommandConfig {}
|
pub struct CommandConfig {
|
||||||
|
/// The port(s) to listen on.
|
||||||
|
///
|
||||||
|
/// If you are using Docker, don't change this, you'll need to map an
|
||||||
|
/// external port to this.
|
||||||
|
///
|
||||||
|
/// To listen on multiple ports, specify a vector e.g. [8080, 8448]
|
||||||
|
///
|
||||||
|
/// default: 8008
|
||||||
|
#[serde(default = "default_port")]
|
||||||
|
pub port: ListeningPort,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Clone, Debug)]
|
||||||
|
#[serde(transparent)]
|
||||||
|
pub struct ListeningPort {
|
||||||
|
#[serde(with = "either::serde_untagged")]
|
||||||
|
pub ports: Either<u16, Vec<u16>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_port() -> ListeningPort {
|
||||||
|
ListeningPort { ports: Left(8008) }
|
||||||
|
}
|
||||||
|
|||||||
21
rust/src/error.rs
Normal file
21
rust/src/error.rs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Error, Debug)]
|
||||||
|
pub enum CommandError {
|
||||||
|
#[error("the configuration is invalid")]
|
||||||
|
InvalidConfig(#[from] Box<figment::Error>),
|
||||||
|
|
||||||
|
#[error(transparent)]
|
||||||
|
Unknown(#[from] anyhow::Error),
|
||||||
|
// examples
|
||||||
|
// #[error("the data for key `{0}` is not available")]
|
||||||
|
// Redaction(String),
|
||||||
|
// #[error("invalid header (expected {expected:?}, found {found:?})")]
|
||||||
|
// InvalidHeader { expected: String, found: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<figment::Error> for CommandError {
|
||||||
|
fn from(error: figment::Error) -> Self {
|
||||||
|
CommandError::InvalidConfig(Box::new(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,8 @@
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use figment::{
|
||||||
|
Figment,
|
||||||
|
providers::{Env, Format, Json, Toml},
|
||||||
|
};
|
||||||
use tracing::{Level, event};
|
use tracing::{Level, event};
|
||||||
|
|
||||||
mod args;
|
mod args;
|
||||||
@@ -6,10 +10,19 @@ use args::{ActionType, CommandArgs};
|
|||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
use error::CommandError;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() -> Result<(), CommandError> {
|
||||||
let args = CommandArgs::parse();
|
let args = CommandArgs::parse();
|
||||||
|
|
||||||
|
let config: config::CommandConfig = Figment::new()
|
||||||
|
.admerge(Json::file("config.json"))
|
||||||
|
.admerge(Toml::file("config.toml"))
|
||||||
|
.admerge(Env::prefixed("APP_"))
|
||||||
|
.extract()?;
|
||||||
|
|
||||||
match &args.action {
|
match &args.action {
|
||||||
ActionType::Serve(serve_args) => {
|
ActionType::Serve(serve_args) => {
|
||||||
tracing_subscriber::fmt()
|
tracing_subscriber::fmt()
|
||||||
@@ -24,7 +37,11 @@ async fn main() {
|
|||||||
|
|
||||||
println!("{:#?}", args);
|
println!("{:#?}", args);
|
||||||
|
|
||||||
// println!("{:#?}", config);
|
println!("{:#?}", config);
|
||||||
|
|
||||||
|
println!("{:#?}", config.port.ports);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user