Compare commits

..

5 Commits

Author SHA1 Message Date
Reed Krantz
95c9a3f192 feat(rust): add a manifest variable that contains the Cargo.toml values 2026-02-21 15:45:21 -06:00
Reed Krantz
c822e1d2eb style(rust): fixed formating in main 2026-02-21 15:44:22 -06:00
Reed Krantz
2c47074a45 feat(rust): implemented the config argument 2026-02-18 21:37:41 -06:00
Reed Krantz
2f6157b062 feat(rust): add configuration parsing and an error type 2026-02-18 19:37:30 -06:00
Reed Krantz
4365e9f739 feat(rust): add treefmt for rust 2026-02-18 18:14:36 -06:00
6 changed files with 132 additions and 14 deletions

View File

@@ -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"] }

View File

@@ -6,20 +6,35 @@
flake-parts.url = "github:hercules-ci/flake-parts"; flake-parts.url = "github:hercules-ci/flake-parts";
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
crane.url = "github:ipetkov/crane"; crane.url = "github:ipetkov/crane";
}; };
outputs = { outputs = {
flake-parts, flake-parts,
treefmt-nix,
crane, crane,
... ...
} @ inputs: } @ inputs:
flake-parts.lib.mkFlake {inherit inputs;} { flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
treefmt-nix.flakeModule
];
systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"]; systems = ["aarch64-linux" "x86_64-linux" "aarch64-darwin" "x86_64-darwin"];
perSystem = {pkgs, ...}: let perSystem = {
pkgs,
lib,
...
}: let
craneLib = crane.mkLib pkgs; craneLib = crane.mkLib pkgs;
src = craneLib.cleanCargoSource ./.; src = craneLib.cleanCargoSource ./.;
manifest = (lib.importTOML "${src}/Cargo.toml").package;
# runtime dependencies # runtime dependencies
buildInputs = with pkgs; [ buildInputs = with pkgs; [
@@ -52,7 +67,13 @@
}; };
# Formatter for nix files, available through 'nix fmt' # Formatter for nix files, available through 'nix fmt'
formatter = pkgs.alejandra; treefmt = {
programs.alejandra.enable = true;
programs.rustfmt = {
enable = true;
edition = manifest.edition;
};
};
# Your custom packages # Your custom packages
# Accessible through 'nix build', 'nix shell', 'nix run', etc # Accessible through 'nix build', 'nix shell', 'nix run', etc
@@ -67,10 +88,6 @@
inherit src buildInputs nativeBuildInputs cargoArtifacts; inherit src buildInputs nativeBuildInputs cargoArtifacts;
cargoClippyExtraArgs = "-- --deny warnings"; cargoClippyExtraArgs = "-- --deny warnings";
}; };
fmt = craneLib.cargoFmt {
inherit src buildInputs nativeBuildInputs;
};
}; };
}; };
}; };

View File

@@ -1,10 +1,11 @@
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use thiserror::Error;
use tracing::Level; use tracing::Level;
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[clap(version, about)] #[clap(version, about)]
pub struct CommandArgs { pub struct CommandArgs {
/// The path to the TOML configuration. This will override all other configuration sources /// The path to a TOML or JSON configuration to use. This will me merged with all other configuration sources, taking priority over other files but not environment variables
#[arg(short, long)] #[arg(short, long)]
pub config: Option<String>, pub config: Option<String>,
@@ -24,3 +25,9 @@ pub struct ServeAction {
#[arg(short, long, default_value_t = Level::WARN)] #[arg(short, long, default_value_t = Level::WARN)]
pub log_level: Level, pub log_level: Level,
} }
#[derive(Error, Debug)]
pub enum ArgsError {
#[error("the file extension of the provided config file is not one of .json or .toml")]
InvalidConfigExtension(Option<String>),
}

View File

@@ -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) }
}

26
rust/src/error.rs Normal file
View File

@@ -0,0 +1,26 @@
use thiserror::Error;
use crate::args;
#[derive(Error, Debug)]
pub enum CommandError {
#[error("the configuration is invalid")]
InvalidConfig(#[from] Box<figment::Error>),
#[error("a commandline argument is invalid")]
InvalidArgument(#[from] args::ArgsError),
#[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))
}
}

View File

@@ -1,15 +1,45 @@
use std::path::Path;
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;
use args::{ActionType, CommandArgs}; use crate::args::{ActionType, ArgsError, CommandArgs};
mod config; mod config;
mod error;
use crate::error::CommandError;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() -> Result<(), CommandError> {
let args = CommandArgs::parse(); let args = CommandArgs::parse();
let figment = Figment::new()
.admerge(Json::file("config.json"))
.admerge(Toml::file("config.toml"));
let config: config::CommandConfig = match &args.config {
None => figment,
Some(f) => {
let file = Path::new(&f);
match file.extension().and_then(|e| e.to_str()) {
Some("toml") => Ok(figment.admerge(Toml::file(file))),
Some("json") => Ok(figment.admerge(Json::file(file))),
_ => Err(ArgsError::InvalidConfigExtension(
file.extension().and_then(|e| e.to_str().map(String::from)),
)),
}
}?,
}
.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 +54,16 @@ async fn main() {
println!("{:#?}", args); println!("{:#?}", args);
// println!("{:#?}", config); println!("{:#?}", config);
println!("{:#?}", config.port.ports);
} }
} };
println!(
"{}",
CommandError::InvalidArgument(ArgsError::InvalidConfigExtension(Some(String::from("txt"))))
);
Ok(())
} }