Skip to content

Commit

Permalink
fix(agent): remove unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoreiradj committed Apr 30, 2024
1 parent a7e411e commit 0c01482
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
11 changes: 7 additions & 4 deletions src/agent/src/agents/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::AgentResult;
use crate::{AgentError, AgentResult};
use serde::Deserialize;

#[cfg(feature = "debug-agent")]
Expand Down Expand Up @@ -36,14 +36,17 @@ impl std::fmt::Display for Language {
}

impl TryFrom<&str> for Language {
type Error = String;
type Error = AgentError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
fn try_from(value: &str) -> Result<Self, AgentError> {
match value {
"rust" => Ok(Language::Rust),
#[cfg(feature = "debug-agent")]
"debug" => Ok(Language::Debug),
_ => Err(format!("Invalid language: {}", value)),
_ => Err(AgentError::InvalidLanguage(format!(
"Invalid language: {}",
value
))),
}
}
}
2 changes: 2 additions & 0 deletions src/agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod workload;
pub enum AgentError {
OpenConfigFileError(std::io::Error),
ParseConfigError(toml::de::Error),
InvalidLanguage(String),
BuildFailed(AgentOutput),
}

Expand All @@ -17,6 +18,7 @@ impl fmt::Display for AgentError {
AgentError::OpenConfigFileError(e) => write!(f, "Failed to open config file: {}", e),
AgentError::ParseConfigError(e) => write!(f, "Failed to parse config file: {}", e),
AgentError::BuildFailed(output) => write!(f, "Build failed: {:?}", output),
AgentError::InvalidLanguage(e) => write!(f, "Invalid language: {}", e),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/agent/src/workload/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ impl Config {
Ok(config)
}

pub fn new_from_execute_request(execute_request: ExecuteRequest) -> Self {
Self {
pub fn new_from_execute_request(execute_request: ExecuteRequest) -> Result<Self, AgentError> {
Ok(Self {
workload_name: execute_request.workload_name.clone(),
// TODO: Fix this unwrap
language: Language::try_from(execute_request.language.clone().as_str()).unwrap(),
language: Language::try_from(execute_request.language.clone().as_str())?,
action: execute_request.action().into(),
config_string: execute_request.config_str,
code: execute_request.code,
}
})
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/agent/src/workload/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
agent::ExecuteRequest,
agents::{rust, Agent, AgentOutput, Language},
workload::config::Action,
AgentResult,
AgentError, AgentResult,
};

#[cfg(feature = "debug-agent")]
Expand All @@ -28,9 +28,9 @@ impl Runner {
Runner { config, agent }
}

pub fn new_from_execute_request(execute_request: ExecuteRequest) -> Self {
let config = Config::new_from_execute_request(execute_request);
Self::new(config)
pub fn new_from_execute_request(execute_request: ExecuteRequest) -> Result<Self, AgentError> {
let config = Config::new_from_execute_request(execute_request)?;
Ok(Self::new(config))
}

pub fn run(&self) -> AgentResult<AgentOutput> {
Expand Down
3 changes: 2 additions & 1 deletion src/agent/src/workload/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ impl WorkloadRunner for WorkloadRunnerService {

let execute_request = req.into_inner();

let runner = Runner::new_from_execute_request(execute_request);
let runner = Runner::new_from_execute_request(execute_request)
.map_err(|e| tonic::Status::internal(e.to_string()))?;

let res = runner
.run()
Expand Down

0 comments on commit 0c01482

Please sign in to comment.