Skip to content

Commit

Permalink
refactor: avoid using expensive .clone()
Browse files Browse the repository at this point in the history
  • Loading branch information
azzamsa committed Feb 6, 2024
1 parent 52f3088 commit 0b97340
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/domain/meta/service/get_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ impl Service {
pub async fn get_meta(&self) -> Result<entities::Meta, crate::Error> {
let config = entities::Config {
env: self.config.env.to_string(),
base_url: self.config.base_url.clone(),
base_url: self.config.base_url.to_string(),
port: self.config.http.port,
};
let meta = entities::Meta {
Expand Down
4 changes: 2 additions & 2 deletions src/domain/user/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ impl UserConnection {
.user_service
.find_page_info(
self.first,
self.after.clone(),
self.after.as_deref(),
self.last,
self.before.clone(),
self.before.as_deref(),
)
.await?;
Ok(page_info.transmogrify())
Expand Down
2 changes: 1 addition & 1 deletion src/domain/user/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl UserQuery {
let server_ctx = ctx.data::<Arc<ServerContext>>()?;
let user_edges = server_ctx
.user_service
.find_users(first, after.clone(), last, before.clone())
.find_users(first, after.as_deref(), last, before.as_deref())
.await?;
let edges: Vec<model::UserEdge> = user_edges
.into_iter()
Expand Down
8 changes: 4 additions & 4 deletions src/domain/user/service/find_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ impl Service {
pub async fn find_users(
&self,
first: Option<i32>,
after: Option<String>,
after: Option<&str>,
last: Option<i32>,
before: Option<String>,
before: Option<&str>,
) -> Result<Vec<entities::UserEdge>, crate::Error> {
validate_params(first, last)?;
let (after_uuid, before_uuid) = convert_params(after, before)?;
Expand All @@ -27,9 +27,9 @@ impl Service {
pub async fn find_page_info(
&self,
first: Option<i32>,
after: Option<String>,
after: Option<&str>,
last: Option<i32>,
before: Option<String>,
before: Option<&str>,
) -> Result<entities::PageInfo, crate::Error> {
let (after_uuid, before_uuid) = convert_params(after, before)?;

Expand Down
12 changes: 6 additions & 6 deletions src/relay/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use crate::{

/// Parse `after` and `before` to cursor
pub fn convert_params(
after: Option<String>,
before: Option<String>,
after: Option<&str>,
before: Option<&str>,
) -> Result<(Option<Uuid>, Option<Uuid>), crate::Error> {
let (after_uuid, before_uuid) = match (after, before) {
(None, None) => (None, None),
(Some(after), Some(before)) => (
Some(Base64Cursor::decode(&after)?.into()),
Some(Base64Cursor::decode(&before)?.into()),
Some(Base64Cursor::decode(after)?.into()),
Some(Base64Cursor::decode(before)?.into()),
),
(Some(after), None) => (Some(Base64Cursor::decode(&after)?.into()), None),
(None, Some(before)) => (None, Some(Base64Cursor::decode(&before)?.into())),
(Some(after), None) => (Some(Base64Cursor::decode(after)?.into()), None),
(None, Some(before)) => (None, Some(Base64Cursor::decode(before)?.into())),
};
Ok((after_uuid, before_uuid))
}
Expand Down
2 changes: 1 addition & 1 deletion src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn app() -> Result<Router, Error> {
let health_service = Arc::new(health::Service::new());
let meta_service = Arc::new(meta::Service::new(Arc::clone(&config)));
let mailer_service = Mailer::new();
let user_service = Arc::new(user::Service::new(db.clone(), mailer_service));
let user_service = Arc::new(user::Service::new(db, mailer_service));

let server_context = Arc::new(ServerContext {
user_service,
Expand Down
8 changes: 4 additions & 4 deletions tests/user/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,13 @@ async fn find_paginated_user() -> Result<()> {
assert_eq!(users_response.data.users.edges.len(), 1);
assert_eq!(users_response.data.users.edges[0].node.name, "one");

let one_cursor = users_response.data.users.edges[0].cursor.clone();
let one_cursor = &users_response.data.users.edges[0].cursor;
//
// after
//
let args = ReadUsersArguments {
first: Some(1),
after: Some(one_cursor),
after: Some(one_cursor.to_string()),
last: None,
before: None,
};
Expand All @@ -193,15 +193,15 @@ async fn find_paginated_user() -> Result<()> {
let users_response: UsersResponse = json::from_slice(&body)?;
assert_eq!(users_response.data.users.edges[0].node.name, "two");

let two_cursor = users_response.data.users.edges[0].cursor.clone();
let two_cursor = &users_response.data.users.edges[0].cursor;
//
// before
//
let args = ReadUsersArguments {
first: Some(1),
after: None,
last: None,
before: Some(two_cursor),
before: Some(two_cursor.to_string()),
};
let query = UsersQuery::build(args);
let request = Request::builder()
Expand Down

0 comments on commit 0b97340

Please sign in to comment.