Skip to content

Commit 2b885c5

Browse files
committed
refactor: cleanup, use clap over argparse & cleanup
Signed-off-by: Rachel Powers <[email protected]>
1 parent 6d7b074 commit 2b885c5

File tree

7 files changed

+30
-36
lines changed

7 files changed

+30
-36
lines changed

libmcmeta/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ edition = "2021"
77

88
[dependencies]
99
chrono = { version = "0.4.24", features = ["serde"] }
10-
custom_error = "1.9.2"
1110
lazy_static = "1.4.0"
1211
merge = "0.1.0"
1312
serde = { version = "1.0.160", features = ["derive"] }
1413
serde_json = "1.0.95"
1514
serde_valid = "0.15.0"
1615
serde_with = "2.3.2"
16+
thiserror = "1.0.40"
1717
time = { version = "0.3.20", features = ["serde-human-readable"] }
1818
tracing = "0.1.37"
1919

libmcmeta/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
#[macro_use]
2-
extern crate custom_error;
3-
// #[macro_use]
4-
// extern crate lazy_static;
5-
61
pub mod models;

libmcmeta/src/models/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ use serde::{Deserialize, Serialize};
33
use serde_valid::Validate;
44
use std::collections::HashMap;
55
use std::{fmt::Display, str::FromStr};
6+
use thiserror::Error;
67

78
pub mod forge;
89
pub mod mojang;
910

10-
custom_error! { pub ModelError
11-
InvalidGradleSpecifier { specifier: String } = "Invalid Gradle specifier '{specifier}'",
11+
#[derive(Error, Debug)]
12+
pub enum ModelError {
13+
#[error("Invalid Gradle specifier '{specifier}'")]
14+
InvalidGradleSpecifier { specifier: String },
1215
}
1316

1417
static META_FORMAT_VERSION: i32 = 1;

mcmeta/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22
name = "mcmeta"
33
version = "0.1.0"
44
edition = "2021"
5+
description = "A Metadata server for Minecraft launchers and others"
56

67
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
78

89
[dependencies]
910
anyhow = { version = "1.0.70", features = ["backtrace"] }
10-
argparse = "0.2.2"
1111
axum = "0.6.15"
12-
clap = "4.2.1"
12+
clap = { version = "4.2.1", features = ["derive"] }
1313
config = "0.13.3"
14-
data-encoding = "2.3.3"
1514
dotenv = "0.15.0"
1615
futures = "0.3.28"
1716
git2 = "0.17.0"

mcmeta/src/download/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use thiserror::Error;
33

44
#[derive(Error, Debug)]
55
pub enum MetadataError {
6-
#[error("Unable to deserialise object at {line}:{column}. Context `{ctx}` \n\nCaused by:\n\t{source}")]
7-
BadData {
6+
#[error("Unable to deserialise json object at {line}:{column}. Context `{ctx}` \n\nCaused by:\n\t{source}")]
7+
BadJsonData {
88
ctx: String,
99
line: usize,
1010
column: usize,
@@ -14,7 +14,7 @@ pub enum MetadataError {
1414

1515
impl MetadataError {
1616
pub fn from_json_err(err: serde_json::Error, body: &str) -> Self {
17-
Self::BadData {
17+
Self::BadJsonData {
1818
ctx: get_json_context_back(&err, body, 200),
1919
line: err.line(),
2020
column: err.column(),

mcmeta/src/errors.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

mcmeta/src/main.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,43 @@ use axum::{routing::get, Extension, Router};
66
use tracing::{debug, info};
77

88
use anyhow::Result;
9-
use argparse::{ArgumentParser, Store, StoreTrue};
109
use dotenv::dotenv;
1110
use tracing_subscriber::{filter, prelude::*};
1211

1312
mod app_config;
1413
mod download;
15-
mod errors;
1614
mod routes;
1715
mod storage;
1816
mod utils;
1917

2018
#[macro_use]
2119
extern crate lazy_static;
2220

21+
use clap::Parser;
22+
23+
#[derive(Parser, Debug)]
24+
#[command(author, version, about, long_about = None)]
25+
struct CliArgs {
26+
#[arg(short, long, value_name = "FILE")]
27+
config: Option<String>,
28+
#[arg(long)]
29+
use_dotenv: bool,
30+
}
31+
2332
#[tokio::main]
2433
async fn main() -> Result<()> {
25-
let mut config_path = "".to_string();
26-
let mut use_dotenv = false;
27-
{
28-
// limit scope of argparse borrows
29-
let mut ap = ArgumentParser::new();
30-
ap.set_description("A Minecraft metadata api server for Mojang and Modloader metadata.");
31-
ap.refer(&mut config_path).add_option(
32-
&["-c", "--config"],
33-
Store,
34-
"Path to a json config file.",
35-
);
36-
ap.refer(&mut use_dotenv).add_option(
37-
&["--use-dotenv"],
38-
StoreTrue,
39-
"Load environment variables from a local .env",
40-
);
41-
ap.parse_args_or_exit();
42-
}
34+
let mut config_path = String::new();
35+
36+
let args = CliArgs::parse();
4337

44-
if use_dotenv {
38+
if args.use_dotenv {
4539
dotenv().ok();
4640
}
4741

42+
if let Some(path) = args.config {
43+
config_path = path;
44+
}
45+
4846
let config = Arc::new(ServerConfig::from_config(&config_path)?);
4947

5048
let file_appender =

0 commit comments

Comments
 (0)