Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
🎨 Move export_votes to the separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
eigenein committed Jun 11, 2023
1 parent 9c0c4fe commit 3a16cc2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod export;

use std::{net::SocketAddr, path::PathBuf};

use clap::{Args, Parser, Subcommand};
Expand All @@ -20,8 +22,8 @@ pub enum Command {
/// Run the web application.
Web(WebArgs),

/// List all the votes in JSONL format.
ListVotes(ListVotesArgs),
/// Export all the votes in JSONL format.
ExportVotes(ExportVotes),
}

#[derive(Args)]
Expand Down Expand Up @@ -78,7 +80,7 @@ impl DbArgs {
}

#[derive(Args)]
pub struct ListVotesArgs {
pub struct ExportVotes {
#[clap(flatten)]
pub db: DbArgs,
}
12 changes: 12 additions & 0 deletions src/cli/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use serde_json::json;

use crate::{cli::ExportVotes, prelude::*};

pub async fn export_votes(args: ExportVotes) -> Result {
let manager = args.db.open()?.vote_manager()?;
for result in manager.iter_all() {
let (account_id, tank_id, vote) = result?;
println!("{}", json!({ "account_id": account_id, "tank_id": tank_id, "vote": vote }));
}
Ok(())
}
14 changes: 2 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ mod web;
mod weegee;

use clap::Parser;
use serde_json::json;

use crate::{
cli::{Cli, Command, ListVotesArgs},
cli::{Cli, Command},
prelude::*,
tracing::trace,
};
Expand All @@ -33,15 +32,6 @@ async fn main() -> Result {

match args.command {
Command::Web(args) => trace(web::run(args).await),
Command::ListVotes(args) => trace(list_votes(args).await),
Command::ExportVotes(args) => trace(cli::export::export_votes(args).await),
}
}

async fn list_votes(args: ListVotesArgs) -> Result {
let manager = args.db.open()?.vote_manager()?;
for result in manager.iter_all() {
let (account_id, tank_id, vote) = result?;
println!("{}", json!({ "account_id": account_id, "tank_id": tank_id, "vote": vote }));
}
Ok(())
}

0 comments on commit 3a16cc2

Please sign in to comment.