Skip to content

Commit

Permalink
Fix formatting of code generated by cargo project new (#3002)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
jvff authored Dec 3, 2024
1 parent d104aec commit d55bd04
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 37 deletions.
10 changes: 5 additions & 5 deletions linera-service/template/contract.rs.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod state;

use linera_sdk::{{
base::WithContractAbi,
views::{{RootView, View, ViewStorageContext}},
views::{{RootView, View}},
Contract, ContractRuntime,
}};

Expand Down Expand Up @@ -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() {{
Expand Down Expand Up @@ -97,5 +97,5 @@ mod tests {{
assert_eq!(*contract.state.value.get(), initial_value);

contract
}}
}}
}}
12 changes: 5 additions & 7 deletions linera-service/template/lib.rs.template
Original file line number Diff line number Diff line change
@@ -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 {{
Expand All @@ -18,8 +19,5 @@ impl ServiceAbi for {project_name}Abi {{

#[derive(Debug, Deserialize, Serialize, GraphQLMutationRoot)]
pub enum Operation {{
Increment {{
value: u64
}}
Increment {{ value: u64 }},
}}

38 changes: 16 additions & 22 deletions linera-service/template/service.rs.template
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {{
Expand Down Expand Up @@ -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)
}}
}}





2 changes: 1 addition & 1 deletion linera-service/template/tests/single_chain.rs.template
Original file line number Diff line number Diff line change
Expand Up @@ -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 }});
}})
Expand Down
10 changes: 8 additions & 2 deletions linera-service/tests/local_net_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d55bd04

Please sign in to comment.