Skip to content

Commit

Permalink
Propagate errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sleitnick committed Jul 26, 2022
1 parent 42c47d8 commit e488f3b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["Stephen Leitnick"]
[dependencies]
anyhow = "1.0.58"
clap = { version = "3.2.14", features = ["derive"] }
log = "0.4.17"
reqwest = { version = "0.11.11", features = ["json"] }
serde = { version = "1.0.140", features = ["derive"] }
serde_json = "1.0.82"
Expand Down
9 changes: 4 additions & 5 deletions src/cli/experience_cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Subcommand, ValueEnum, Args};

use crate::{rbx::{RbxCloud, PublishVersionType}, util::{print_success, print_error}};
use crate::rbx::{RbxCloud, PublishVersionType};

#[derive(Debug, Subcommand)]
pub enum ExperienceCommands {
Expand Down Expand Up @@ -40,7 +40,7 @@ pub enum VersionType {
}

impl Experience {
pub async fn run(self) -> anyhow::Result<()> {
pub async fn run(self) -> anyhow::Result<Option<String>> {
match self.command {
ExperienceCommands::Publish {place_id,universe_id,version_type,api_key, filename } => {
let rbx_cloud = RbxCloud::new(api_key, universe_id);
Expand All @@ -51,14 +51,13 @@ impl Experience {
let res = rbx_cloud.experience(place_id).publish(&filename, publish_version_type).await;
match res {
Ok(body) => {
print_success(format!("{:?} {}/{} with version number {}", version_type, universe_id, place_id, body.version_number).to_lowercase());
Ok(Some(format!("{:?} {}/{} with version number {}", version_type, universe_id, place_id, body.version_number).to_lowercase()))
}
Err(err) => {
print_error(format!("{}", err.to_string()));
Err(err)
}
}
}
}
Ok(())
}
}
9 changes: 4 additions & 5 deletions src/cli/messaging_cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Subcommand, Args};

use crate::{rbx::RbxCloud, util::{print_success, print_error}};
use crate::rbx::RbxCloud;

#[derive(Debug, Subcommand)]
pub enum MessagingCommands {
Expand Down Expand Up @@ -30,22 +30,21 @@ pub struct Messaging {
}

impl Messaging {
pub async fn run(self) -> anyhow::Result<()> {
pub async fn run(self) -> anyhow::Result<Option<String>> {
match self.command {
MessagingCommands::Publish { topic, message, universe_id, api_key } => {
let rbx_cloud = RbxCloud::new(api_key, universe_id);
let messaging = rbx_cloud.messaging(&topic);
let res = messaging.publish(message).await;
match res {
Ok(()) => {
print_success(format!("published message to topic {}", topic));
Ok(Some(format!("published message to topic {}", topic)))
}
Err(err) => {
print_error(format!("{}", err.to_string()));
Err(err)
}
}
}
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct Cli {
}

impl Cli {
pub async fn run(self) -> anyhow::Result<()> {
pub async fn run(self) -> anyhow::Result<Option<String>> {
match self.command {
Command::Experience(command) => command.run().await,
Command::Messaging(command) => command.run().await,
Expand Down
15 changes: 13 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
mod rbx;
mod cli;
mod util;

use std::process;

use clap::Parser;
use cli::Cli;

#[tokio::main]
async fn main() {
let args = Cli::parse();
args.run().await.unwrap();
match args.run().await {
Ok(str) => {
if let Some(s) = str {
log::info!("{}", s);
}
}
Err(err) => {
log::error!("{:?}", err);
process::exit(1);
}
}
}
18 changes: 0 additions & 18 deletions src/util.rs

This file was deleted.

0 comments on commit e488f3b

Please sign in to comment.