From d55bd04ec02de29b03e1900d2c0ed8d42035a64a Mon Sep 17 00:00:00 2001 From: Janito Vaqueiro Ferreira Filho Date: Tue, 3 Dec 2024 17:24:36 +0000 Subject: [PATCH] Fix formatting of code generated by `cargo project new` (#3002) * Tweak newline locations Remove trailing newline at the end of the file, and separate the imports from the type declarations. * Fix formatting in import Ensure the indentiation follows the convention used in the other files. * Fix import order Order the imports alphabetically so that `cargo clippy` accepts it. * Fix import order in contract template Fix formatting now that the state type has a `State` suffix. * Remove trailing newlines from service template Ensure all files only have one newline at the end of the file. * Add trailing comma to import Ensure it follows proper formatting rules. * Fix whitespace and indentation Remove trailing newlines and spaces, and ensure the code follows the correct indentation standard. * Fix import order Ensure imports are either grouped in alphabetical order or placed in separate paragraphs. * Refactor `handle_query` to remove binding Use a more flow-control style. * Remove unused import The import looks like it's no longer necessary. * Test if template formatting is correct Run `cargo fmt --check` in CI. --- linera-service/template/contract.rs.template | 10 ++--- linera-service/template/lib.rs.template | 12 +++--- linera-service/template/service.rs.template | 38 ++++++++----------- .../template/tests/single_chain.rs.template | 2 +- linera-service/tests/local_net_tests.rs | 10 ++++- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/linera-service/template/contract.rs.template b/linera-service/template/contract.rs.template index 2c49504f646..871402e616d 100644 --- a/linera-service/template/contract.rs.template +++ b/linera-service/template/contract.rs.template @@ -4,7 +4,7 @@ mod state; use linera_sdk::{{ base::WithContractAbi, - views::{{RootView, View, ViewStorageContext}}, + views::{{RootView, View}}, Contract, ContractRuntime, }}; @@ -58,12 +58,12 @@ impl Contract for {project_name}Contract {{ #[cfg(test)] mod tests {{ - use {module_name}::Operation; use futures::FutureExt as _; - use linera_sdk::{{util::BlockingWait, views::View, Contract, ContractRuntime}}; - use super::{{{project_name}State, {project_name}Contract}}; + use {module_name}::Operation; + + use super::{{{project_name}Contract, {project_name}State}}; #[test] fn operation() {{ @@ -97,5 +97,5 @@ mod tests {{ assert_eq!(*contract.state.value.get(), initial_value); contract - }} + }} }} diff --git a/linera-service/template/lib.rs.template b/linera-service/template/lib.rs.template index 46d3239cbbc..b835c75484f 100644 --- a/linera-service/template/lib.rs.template +++ b/linera-service/template/lib.rs.template @@ -1,9 +1,10 @@ +use async_graphql::{{Request, Response}}; use linera_sdk::{{ - base::{{ContractAbi, ServiceAbi}}, - graphql::GraphQLMutationRoot + base::{{ContractAbi, ServiceAbi}}, + graphql::GraphQLMutationRoot, }}; use serde::{{Deserialize, Serialize}}; -use async_graphql::{{Request, Response}}; + pub struct {project_name}Abi; impl ContractAbi for {project_name}Abi {{ @@ -18,8 +19,5 @@ impl ServiceAbi for {project_name}Abi {{ #[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)] pub enum Operation {{ - Increment {{ - value: u64 - }} + Increment {{ value: u64 }}, }} - diff --git a/linera-service/template/service.rs.template b/linera-service/template/service.rs.template index f51afeb8294..6a3fa2d2db9 100644 --- a/linera-service/template/service.rs.template +++ b/linera-service/template/service.rs.template @@ -2,16 +2,14 @@ mod state; -use self::state::{project_name}State; +use async_graphql::{{EmptySubscription, Object, Schema}}; use linera_sdk::{{ - base::WithServiceAbi, - views::{{View, ViewStorageContext}}, - Service, ServiceRuntime, - graphql::GraphQLMutationRoot, + base::WithServiceAbi, graphql::GraphQLMutationRoot, views::View, Service, ServiceRuntime, }}; + use {module_name}::Operation; -use async_graphql::{{EmptySubscription, Object, Schema}}; +use self::state::{project_name}State; pub struct {project_name}Service {{ state: {project_name}State, @@ -35,22 +33,23 @@ impl Service for {project_name}Service {{ }} async fn handle_query(&self, query: Self::Query) -> Self::QueryResponse {{ - let schema = Schema::build( - QueryRoot {{ - value: *self.state.value.get(), - }}, - Operation::mutation_root(), - EmptySubscription, - ).finish(); - schema.execute(query).await + Schema::build( + QueryRoot {{ + value: *self.state.value.get(), + }}, + Operation::mutation_root(), + EmptySubscription, + ) + .finish() + .execute(query) + .await }} }} struct QueryRoot {{ - value: u64, + value: u64, }} - #[Object] impl QueryRoot {{ async fn value(&self) -> &u64 {{ @@ -83,14 +82,9 @@ mod tests {{ .handle_query(request) .now_or_never() .expect("Query should not await anything"); - + let expected = Response::new(Value::from_json(json!({{"value": 60}})).unwrap()); assert_eq!(response, expected) }} }} - - - - - diff --git a/linera-service/template/tests/single_chain.rs.template b/linera-service/template/tests/single_chain.rs.template index 38bba3481b8..ef28cc07040 100644 --- a/linera-service/template/tests/single_chain.rs.template +++ b/linera-service/template/tests/single_chain.rs.template @@ -24,7 +24,7 @@ async fn single_chain_test() {{ .await; let increment = 10u64; - chain + chain .add_block(|block| {{ block.with_operation(application_id, Operation::Increment {{ value: increment }}); }}) diff --git a/linera-service/tests/local_net_tests.rs b/linera-service/tests/local_net_tests.rs index 7395893ffb9..690f09f25d2 100644 --- a/linera-service/tests/local_net_tests.rs +++ b/linera-service/tests/local_net_tests.rs @@ -10,7 +10,7 @@ mod common; -use std::{env, path::PathBuf, time::Duration}; +use std::{env, path::PathBuf, process::Command, time::Duration}; use anyhow::Result; use common::INTEGRATION_TEST_GUARD; @@ -660,6 +660,12 @@ async fn test_project_new() -> Result<()> { .build_application(project_dir.as_path(), "init-test", false) .await?; + let mut child = Command::new("cargo") + .args(["fmt", "--check"]) + .current_dir(project_dir.as_path()) + .spawn()?; + assert!(child.wait()?.success()); + Ok(()) } @@ -714,7 +720,7 @@ async fn test_storage_service_wallet_lock() -> Result<()> { async fn test_storage_service_linera_net_up_simple() -> Result<()> { use std::{ io::{BufRead, BufReader}, - process::{Command, Stdio}, + process::Stdio, }; let _guard = INTEGRATION_TEST_GUARD.lock().await;