Skip to content

Commit

Permalink
Cleaning up some mistakes in rust code (#1396)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeSiFive authored Aug 23, 2023
1 parent 0e3aac5 commit ae653ab
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ Cargo.lock
/rust/migration/target/*
/rust/entity/target/*
/rust/log_viewer/target/*
/rust/gsc/target/*
/rust/rsc/target/*
4 changes: 2 additions & 2 deletions rust/log_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2021"

[dependencies]
chrono = { version = "0.4.26", features = ["serde"] }
color-eyre = "0.6.2"
clap = { version = "4.3.23", features = ["derive"] }
crossterm = "0.26.1"
gumdrop = "0.8.1"
miette = { version = "5.10.0", features = ["fancy"] }
palette = "0.7.2"
serde = { version = "1.0.171", features = ["std", "derive"] }
serde-aux = "4.2.0"
Expand Down
35 changes: 15 additions & 20 deletions rust/log_viewer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use chrono::NaiveDateTime;
use color_eyre::Report;
use clap::Parser;
use crossterm::style::{Color, StyledContent, Stylize};
use gumdrop::Options;
use miette::Error;
use miette::IntoDiagnostic;
use palette::{convert::FromColorUnclamped, encoding::Srgb, rgb::Rgb, Lab};
use serde::{Deserialize, Serialize};
use serde_aux::prelude::*;
Expand Down Expand Up @@ -119,27 +120,21 @@ fn get_log_level_color(level: &Option<String>) -> Color {
}
}

fn read_lines_from(file: &str) -> Result<Vec<Event>, Report> {
let file = File::open(file)?;
fn read_lines_from(file: &str) -> Result<Vec<Event>, Error> {
let file = File::open(file).into_diagnostic()?;
let file = BufReader::new(file);
let mut out = vec![];
for line in file.lines() {
let line = line?;
let event: Event = serde_json::from_str(&line).map_err(|error| {
let message = format!("json(len = {}): {}", line.len(), line);
Report::new(error).wrap_err(message)
})?;
let line = line.into_diagnostic()?;
let event: Event = serde_json::from_str(&line).into_diagnostic()?;
out.push(event);
}
Ok(out)
}

#[derive(Debug, Options)]
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
struct LogViewOptions {
#[options(help_flag, help = "print help message")]
help: bool,

#[options(free)]
files: Vec<String>,
}

Expand All @@ -149,7 +144,7 @@ struct RenderConfig {
color_map: HashMap<i32, Color>,
}

fn render_line(config: &RenderConfig, event: &Event) -> Result<(), Report> {
fn render_line(config: &RenderConfig, event: &Event) -> Result<(), Error> {
let mut to_render: Vec<StyledContent<String>> = vec![];
let color = *config.color_map.get(&event.pid).unwrap_or(&Color::White);

Expand Down Expand Up @@ -199,8 +194,8 @@ fn render_line(config: &RenderConfig, event: &Event) -> Result<(), Report> {
Ok(())
}

fn render(color_map: HashMap<i32, Color>, events: Vec<Event>) -> Result<(), Report> {
let (max_width, _height) = crossterm::terminal::size()?;
fn render(color_map: HashMap<i32, Color>, events: Vec<Event>) -> Result<(), Error> {
let (max_width, _height) = crossterm::terminal::size().into_diagnostic()?;
let config = RenderConfig {
max_width,
color_map,
Expand All @@ -211,10 +206,10 @@ fn render(color_map: HashMap<i32, Color>, events: Vec<Event>) -> Result<(), Repo
Ok(())
}

fn main() -> Result<(), Report> {
color_eyre::install()?;
fn main() -> Result<(), Error> {
//color_eyre::install()?;
let mut colors = generate_distinct_colors();
let opts = LogViewOptions::parse_args_default_or_exit();
let opts = LogViewOptions::parse();
let mut events: Vec<Event> = vec![];

// Parse all the files
Expand Down
File renamed without changes.
13 changes: 7 additions & 6 deletions rust/gsc/Cargo.toml → rust/rsc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[package]
name = "gsc"
name = "rsc"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "gsc"
path = "src/gsc/main.rs"
name = "rsc"
path = "src/rsc/main.rs"

[[bin]]
name = "gsc_tool"
path = "src/gsc_tool/main.rs"
name = "rsc_tool"
path = "src/rsc_tool/main.rs"

[dependencies]
entity = { path = "../entity" }
Expand All @@ -22,7 +22,6 @@ tracing-subscriber = "0.3"
axum = "0.6.18"
serde_bytes = "0.11.9"
blake3 = "1.4.0"
gumdrop = "0.8.1"
data-encoding = "2.4.0"
rand_core = "0.6.4"
inquire = { version = "0.6.2", features = [] }
Expand All @@ -32,3 +31,5 @@ textwrap = "0.16.0"
chrono = "0.4.26"
config = "0.13.3"
serde_json = "1.0.100"
clap = { version = "4.3.23", features = ["derive"] }
is-terminal = "0.4.9"
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 11 additions & 14 deletions rust/gsc/src/gsc/main.rs → rust/rsc/src/rsc/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use axum::{routing::post, Router};
use gumdrop::Options;
use clap::Parser;
use migration::{Migrator, MigratorTrait};
use std::io::{Error, ErrorKind};
use std::sync::Arc;
Expand All @@ -13,28 +13,25 @@ mod types;
#[path = "../common/config.rs"]
mod config;

#[derive(Debug, Options)]
#[derive(Debug, Parser)]
struct ServerOptions {
#[options(help_flag, help = "print help message")]
help: bool,

#[options(help = "Specify a config override file", meta = "CONFIG", no_short)]
#[arg(help = "Specify a config override file", value_name = "CONFIG", long)]
config_override: Option<String>,

#[options(help = "Show's the config and then exits", no_short)]
#[arg(help = "Show's the config and then exits", long)]
show_config: bool,

#[options(
#[arg(
help = "Specify an override for the bind address",
meta = "SERVER_IP[:SERVER_PORT]",
no_short
value_name = "SERVER_IP[:SERVER_PORT]",
long
)]
server_addr: Option<String>,

#[options(
#[arg(
help = "Specify an override for the database url",
meta = "DATABASE_URL",
no_short
value_name = "DATABASE_URL",
long
)]
database_url: Option<String>,
}
Expand All @@ -46,7 +43,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing::subscriber::set_global_default(subscriber)?;

// Parse our arguments
let args = ServerOptions::parse_args_default_or_exit();
let args = ServerOptions::parse();

// Get our configuration
let config = config::GSCConfig::new(config::GSCConfigOverride {
Expand Down
File renamed without changes.
File renamed without changes.
69 changes: 30 additions & 39 deletions rust/gsc/src/gsc_tool/main.rs → rust/rsc/src/rsc_tool/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use atty::Stream;
use clap::{Parser, Subcommand};
use data_encoding::BASE64;
use entity::api_key;
use gumdrop::Options;
use inquire::Confirm;
use is_terminal::IsTerminal;
use migration::{DbErr, Migrator, MigratorTrait};
use rand_core::{OsRng, RngCore};
use sea_orm::{
Expand Down Expand Up @@ -91,7 +91,7 @@ async fn remove_api_key(
};

// We only want to prompt the user if the user can type into the terminal
if atty::is(Stream::Stdin) {
if std::io::stdin().is_terminal() {
let should_delete = Confirm::new("Are you sure you want to delete this key?")
.with_default(false)
.with_help_message(format!("key = {}, desc = {:?}", key, result.desc).as_str())
Expand All @@ -117,82 +117,73 @@ async fn remove_api_key(
}

// Define all of our top level commands
#[derive(Debug, Options)]
#[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)]
struct TopLevel {
// Options here can be accepted with any command (or none at all),
// but they must come before the command name.
#[options(help_flag, help = "print help message")]
help: bool,

#[options(help = "Specify a config override file", meta = "CONFIG", no_short)]
#[arg(help = "Specify a config override file", value_name = "CONFIG", long)]
config_override: Option<String>,

#[options(
#[arg(
help = "Specify and override for the database url",
meta = "DATABASE_URL",
no_short
value_name = "DATABASE_URL",
long
)]
database_url: Option<String>,

#[options(help = "Show's the config and then exits", no_short)]
#[arg(help = "Show's the config and then exits", long)]
show_config: bool,

// The `command` option will delegate option parsing to the command type,
// starting at the first free argument.
#[options(command)]
#[command(subcommand)]
api_key_command: Option<ApiKey>,
}

#[derive(Debug, Options)]
#[derive(Debug, Subcommand)]
enum ApiKey {
#[options(help = "list all api keys in the database")]
//#[options(help = "list all api keys in the database")]
List(ListKeysOpts),

#[options(help = "add an api key to the database")]
//#[options(help = "add an api key to the database")]
AddKey(AddKeyOpts),

#[options(help = "remove an api key from the database")]
//#[options(help = "remove an api key from the database")]
RemoveKey(RemoveKeyOpts),
}

#[derive(Debug, Options)]
#[derive(Debug, Parser)]
struct AddKeyOpts {
#[options(help_flag, help = "print help message")]
help: bool,

#[options(
#[arg(
help = "If specified this is the key that will be used, otherwise one will be generated",
meta = "KEY"
value_name = "KEY"
)]
key: Option<String>,

#[options(
required,
#[arg(
required = true,
help = "The description of the key. This might tell you where the key is meant to be used",
meta = "DESC"
value_name = "DESC"
)]
desc: String,
}

#[derive(Debug, Options)]
struct ListKeysOpts {
#[options(help_flag, help = "print help message")]
help: bool,
}
#[derive(Debug, Parser)]
struct ListKeysOpts {}

#[derive(Debug, Options)]
#[derive(Debug, Parser)]
struct RemoveKeyOpts {
#[options(help_flag, help = "print help message")]
help: bool,

#[options(required, help = "The key you want to remove", meta = "KEY")]
#[arg(
required = true,
help = "The key you want to remove",
value_name = "KEY"
)]
key: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Parse our arguments
let args = TopLevel::parse_args_default_or_exit();
let args = TopLevel::parse();

// Gather our config
let config = config::GSCConfig::new(config::GSCConfigOverride {
Expand Down
File renamed without changes.

0 comments on commit ae653ab

Please sign in to comment.