Skip to content

Commit

Permalink
removed unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Uewotm90 committed Dec 7, 2023
1 parent 15df9f2 commit dc82b92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
7 changes: 4 additions & 3 deletions src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tonic::{
pub fn validation_interceptor(mut req: Request<()>) -> Result<Request<()>, Status> {
let token = match req
.token_string()
.map_err(|err| Status::internal("failed to get token string"))?
.map_err(|_err| Status::internal("failed to get token string"))?
{
Some(token) => Token::from_str(TokenType::AccessToken, &token),
None => return Err(Status::unauthenticated("Token not found")),
Expand Down Expand Up @@ -57,6 +57,7 @@ impl TokenType {
///
/// # Panics
/// This method will panic if the token secret environment variable is not set.
#[allow(clippy::expect_used)]
fn secret(&self) -> String {
match self {
TokenType::AccessToken => env::var("ACCESS_TOKEN_HS512_SECRET")
Expand Down Expand Up @@ -104,7 +105,7 @@ impl Token {
let now = Utc::now();
let expiration = now
.checked_add_signed(token_type.duration())
.ok_or_else(|| TokenError::InvalidSignature)?
.ok_or(TokenError::InvalidSignature)?
// .expect("valid timestamp")
.timestamp();

Expand Down Expand Up @@ -301,7 +302,7 @@ impl<T> RequestExt for Request<T> {
Err(_) => return None,
};
//TODO better error handling
Some(uid.parse().ok()?)
uid.parse().ok()
}
}

Expand Down
42 changes: 23 additions & 19 deletions src/api/ecdar_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::database::{session_context::SessionContextTrait, user_context::UserCo
use crate::entities::{access, in_use, model, query, session, user};
use chrono::{Duration, Utc};
use regex::Regex;
use sea_orm::{DbErr, SqlErr};
use sea_orm::SqlErr;
use serde_json;
use std::sync::Arc;
use tonic::{Code, Request, Response, Status};
Expand Down Expand Up @@ -96,7 +96,7 @@ pub async fn handle_session(
TokenType::RefreshToken,
request
.token_string()
.map_err(|err| Status::internal("failed to get token from request metadata"))?
.map_err(|_err| Status::internal("failed to get token from request metadata"))?
.ok_or(Status::internal(
"failed to get token from request metadata",
))?,
Expand Down Expand Up @@ -203,7 +203,7 @@ impl EcdarApi for ConcreteEcdarApi {
TokenType::AccessToken,
request
.token_string()
.map_err(|err| {
.map_err(|_err| {
Status::internal(
"failed to get token from request metadata",
)
Expand Down Expand Up @@ -248,20 +248,24 @@ impl EcdarApi for ConcreteEcdarApi {

let queries = queries
.into_iter()
.map(|query| Query {
id: query.id,
model_id: query.model_id,
query: query.string,
result: match query.result {
Some(result) => {
serde_json::from_value(result).expect("failed to parse message")
//TODO better error handling
}
None => "".to_owned(),
},
outdated: query.outdated,
.map(|query| {
let result = serde_json::from_value(query.result.unwrap_or_else(|| "".into()))?;

Ok(Query {
id: query.id,
model_id: query.model_id,
query: query.string,
result: result,

Check warning on line 258 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

redundant field names in struct initialization

warning: redundant field names in struct initialization --> src/api/ecdar_api.rs:258:21 | 258 | result: result, | ^^^^^^^^^^^^^^ help: replace it with: `result` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names = note: `#[warn(clippy::redundant_field_names)]` on by default

Check warning on line 258 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

redundant field names in struct initialization

warning: redundant field names in struct initialization --> src/api/ecdar_api.rs:258:21 | 258 | result: result, | ^^^^^^^^^^^^^^ help: replace it with: `result` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names = note: `#[warn(clippy::redundant_field_names)]` on by default
outdated: query.outdated,
})
})
.collect::<Vec<Query>>();
.collect::<Result<Vec<Query>, serde_json::Error>>()
.map_err(|err| {
Status::internal(format!(
"failed to parse json result, inner error: {}",
err.to_string()

Check failure on line 266 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

`to_string` applied to a type that implements `Display` in `format!` args

error: `to_string` applied to a type that implements `Display` in `format!` args --> src/api/ecdar_api.rs:266:24 | 266 | err.to_string() | ^^^^^^^^^^^^ help: remove this | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args = note: `-D clippy::to-string-in-format-args` implied by `-D clippy::perf` = help: to override `-D clippy::perf` add `#[allow(clippy::to_string_in_format_args)]`

Check failure on line 266 in src/api/ecdar_api.rs

View workflow job for this annotation

GitHub Actions / Clippy lint and check

`to_string` applied to a type that implements `Display` in `format!` args

error: `to_string` applied to a type that implements `Display` in `format!` args --> src/api/ecdar_api.rs:266:24 | 266 | err.to_string() | ^^^^^^^^^^^^ help: remove this | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_format_args = note: `-D clippy::to-string-in-format-args` implied by `-D clippy::perf` = help: to override `-D clippy::perf` add `#[allow(clippy::to_string_in_format_args)]`
))
})?;

Ok(Response::new(GetModelResponse {
model: Some(model),
Expand Down Expand Up @@ -331,7 +335,7 @@ impl EcdarApi for ConcreteEcdarApi {
TokenType::AccessToken,
request
.token_string()
.map_err(|err| Status::internal("failed to get token from request metadata"))?
.map_err(|_err| Status::internal("failed to get token from request metadata"))?
.ok_or(Status::internal(
"Failed to get token from request metadata",
))?,
Expand Down Expand Up @@ -416,7 +420,7 @@ impl EcdarApi for ConcreteEcdarApi {
TokenType::AccessToken,
request
.token_string()
.map_err(|err| Status::internal("failed to get token from request metadata"))?
.map_err(|_err| Status::internal("failed to get token from request metadata"))?
.ok_or(Status::new(
Code::Internal,
"Failed to get token from request metadata",
Expand Down Expand Up @@ -864,7 +868,7 @@ impl EcdarApiAuth for ConcreteEcdarApi {
TokenType::RefreshToken,
request
.token_str()
.map_err(|err| Status::internal("failed to get token from request metadata"))?
.map_err(|_err| Status::internal("failed to get token from request metadata"))?
.ok_or(Status::unauthenticated("No refresh token provided"))?,
);
let token_data = refresh_token.validate()?;
Expand Down

0 comments on commit dc82b92

Please sign in to comment.