Skip to content

Commit d55bd04

Browse files
authored
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.
1 parent d104aec commit d55bd04

File tree

5 files changed

+35
-37
lines changed

5 files changed

+35
-37
lines changed

linera-service/template/contract.rs.template

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod state;
44

55
use linera_sdk::{{
66
base::WithContractAbi,
7-
views::{{RootView, View, ViewStorageContext}},
7+
views::{{RootView, View}},
88
Contract, ContractRuntime,
99
}};
1010

@@ -58,12 +58,12 @@ impl Contract for {project_name}Contract {{
5858

5959
#[cfg(test)]
6060
mod tests {{
61-
use {module_name}::Operation;
6261
use futures::FutureExt as _;
63-
6462
use linera_sdk::{{util::BlockingWait, views::View, Contract, ContractRuntime}};
6563

66-
use super::{{{project_name}State, {project_name}Contract}};
64+
use {module_name}::Operation;
65+
66+
use super::{{{project_name}Contract, {project_name}State}};
6767

6868
#[test]
6969
fn operation() {{
@@ -97,5 +97,5 @@ mod tests {{
9797
assert_eq!(*contract.state.value.get(), initial_value);
9898

9999
contract
100-
}}
100+
}}
101101
}}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use async_graphql::{{Request, Response}};
12
use linera_sdk::{{
2-
base::{{ContractAbi, ServiceAbi}},
3-
graphql::GraphQLMutationRoot
3+
base::{{ContractAbi, ServiceAbi}},
4+
graphql::GraphQLMutationRoot,
45
}};
56
use serde::{{Deserialize, Serialize}};
6-
use async_graphql::{{Request, Response}};
7+
78
pub struct {project_name}Abi;
89

910
impl ContractAbi for {project_name}Abi {{
@@ -18,8 +19,5 @@ impl ServiceAbi for {project_name}Abi {{
1819

1920
#[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)]
2021
pub enum Operation {{
21-
Increment {{
22-
value: u64
23-
}}
22+
Increment {{ value: u64 }},
2423
}}
25-

linera-service/template/service.rs.template

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22

33
mod state;
44

5-
use self::state::{project_name}State;
5+
use async_graphql::{{EmptySubscription, Object, Schema}};
66
use linera_sdk::{{
7-
base::WithServiceAbi,
8-
views::{{View, ViewStorageContext}},
9-
Service, ServiceRuntime,
10-
graphql::GraphQLMutationRoot,
7+
base::WithServiceAbi, graphql::GraphQLMutationRoot, views::View, Service, ServiceRuntime,
118
}};
9+
1210
use {module_name}::Operation;
1311

14-
use async_graphql::{{EmptySubscription, Object, Schema}};
12+
use self::state::{project_name}State;
1513

1614
pub struct {project_name}Service {{
1715
state: {project_name}State,
@@ -35,22 +33,23 @@ impl Service for {project_name}Service {{
3533
}}
3634

3735
async fn handle_query(&self, query: Self::Query) -> Self::QueryResponse {{
38-
let schema = Schema::build(
39-
QueryRoot {{
40-
value: *self.state.value.get(),
41-
}},
42-
Operation::mutation_root(),
43-
EmptySubscription,
44-
).finish();
45-
schema.execute(query).await
36+
Schema::build(
37+
QueryRoot {{
38+
value: *self.state.value.get(),
39+
}},
40+
Operation::mutation_root(),
41+
EmptySubscription,
42+
)
43+
.finish()
44+
.execute(query)
45+
.await
4646
}}
4747
}}
4848

4949
struct QueryRoot {{
50-
value: u64,
50+
value: u64,
5151
}}
5252

53-
5453
#[Object]
5554
impl QueryRoot {{
5655
async fn value(&self) -> &u64 {{
@@ -83,14 +82,9 @@ mod tests {{
8382
.handle_query(request)
8483
.now_or_never()
8584
.expect("Query should not await anything");
86-
85+
8786
let expected = Response::new(Value::from_json(json!({{"value": 60}})).unwrap());
8887

8988
assert_eq!(response, expected)
9089
}}
9190
}}
92-
93-
94-
95-
96-

linera-service/template/tests/single_chain.rs.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async fn single_chain_test() {{
2424
.await;
2525

2626
let increment = 10u64;
27-
chain
27+
chain
2828
.add_block(|block| {{
2929
block.with_operation(application_id, Operation::Increment {{ value: increment }});
3030
}})

linera-service/tests/local_net_tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
mod common;
1212

13-
use std::{env, path::PathBuf, time::Duration};
13+
use std::{env, path::PathBuf, process::Command, time::Duration};
1414

1515
use anyhow::Result;
1616
use common::INTEGRATION_TEST_GUARD;
@@ -660,6 +660,12 @@ async fn test_project_new() -> Result<()> {
660660
.build_application(project_dir.as_path(), "init-test", false)
661661
.await?;
662662

663+
let mut child = Command::new("cargo")
664+
.args(["fmt", "--check"])
665+
.current_dir(project_dir.as_path())
666+
.spawn()?;
667+
assert!(child.wait()?.success());
668+
663669
Ok(())
664670
}
665671

@@ -714,7 +720,7 @@ async fn test_storage_service_wallet_lock() -> Result<()> {
714720
async fn test_storage_service_linera_net_up_simple() -> Result<()> {
715721
use std::{
716722
io::{BufRead, BufReader},
717-
process::{Command, Stdio},
723+
process::Stdio,
718724
};
719725

720726
let _guard = INTEGRATION_TEST_GUARD.lock().await;

0 commit comments

Comments
 (0)