Skip to content

Commit

Permalink
Use name in the code when generating new linera project (#2578)
Browse files Browse the repository at this point in the history
* Convert Linera project name to Pascal case when creating contract/service files.

* Use convert_case dep
  • Loading branch information
deuszx authored Oct 7, 2024
1 parent cf21868 commit d529f7b
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 37 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions linera-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ clap.workspace = true
clap-markdown.workspace = true
colored.workspace = true
comfy-table.workspace = true
convert_case.workspace = true
current_platform = "0.2.0"
fs-err = { workspace = true, features = ["tokio"] }
fs_extra = { workspace = true, optional = true }
Expand Down
33 changes: 23 additions & 10 deletions linera-service/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{

use anyhow::{ensure, Context, Result};
use cargo_toml::Manifest;
use convert_case::{Case, Casing};
use current_platform::CURRENT_PLATFORM;
use fs_err::File;
use tracing::debug;
Expand Down Expand Up @@ -49,10 +50,10 @@ impl Project {
Self::create_rust_toolchain(&root)?;

debug!("writing state.rs");
Self::create_state_file(&source_directory)?;
Self::create_state_file(&source_directory, name)?;

debug!("writing lib.rs");
Self::create_lib_file(&source_directory)?;
Self::create_lib_file(&source_directory, name)?;

debug!("writing contract.rs");
Self::create_contract_file(&source_directory, name)?;
Expand Down Expand Up @@ -159,31 +160,43 @@ impl Project {
)
}

fn create_state_file(source_directory: &Path) -> Result<()> {
fn create_state_file(source_directory: &Path, project_name: &str) -> Result<()> {
let project_name = project_name.to_case(Case::Pascal);
let state_path = source_directory.join("state.rs");
Self::write_string_to_file(&state_path, include_str!("../template/state.rs.template"))
let file_content = format!(
include_str!("../template/state.rs.template"),
project_name = project_name
);
Self::write_string_to_file(&state_path, &file_content)
}

fn create_lib_file(source_directory: &Path) -> Result<()> {
fn create_lib_file(source_directory: &Path, project_name: &str) -> Result<()> {
let project_name = project_name.to_case(Case::Pascal);
let state_path = source_directory.join("lib.rs");
Self::write_string_to_file(&state_path, include_str!("../template/lib.rs.template"))
let file_content = format!(
include_str!("../template/lib.rs.template"),
project_name = project_name
);
Self::write_string_to_file(&state_path, &file_content)
}

fn create_contract_file(source_directory: &Path, project_name: &str) -> Result<()> {
let project_name = project_name.replace('-', "_");
fn create_contract_file(source_directory: &Path, name: &str) -> Result<()> {
let project_name = name.to_case(Case::Pascal);
let contract_path = source_directory.join("contract.rs");
let contract_contents = format!(
include_str!("../template/contract.rs.template"),
module_name = name.replace('-', "_"),
project_name = project_name
);
Self::write_string_to_file(&contract_path, &contract_contents)
}

fn create_service_file(source_directory: &Path, project_name: &str) -> Result<()> {
let project_name = project_name.replace('-', "_");
fn create_service_file(source_directory: &Path, name: &str) -> Result<()> {
let project_name = name.to_case(Case::Pascal);
let service_path = source_directory.join("service.rs");
let service_contents = format!(
include_str!("../template/service.rs.template"),
module_name = name.replace('-', "_"),
project_name = project_name
);
Self::write_string_to_file(&service_path, &service_contents)
Expand Down
18 changes: 9 additions & 9 deletions linera-service/template/contract.rs.template
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ use linera_sdk::{{
Contract, ContractRuntime,
}};

use self::state::Application;
use self::state::{project_name};

pub struct ApplicationContract {{
state: Application,
pub struct {project_name}Contract {{
state: {project_name},
runtime: ContractRuntime<Self>,
}}

linera_sdk::contract!(ApplicationContract);
linera_sdk::contract!({project_name}Contract);

impl WithContractAbi for ApplicationContract {{
type Abi = {project_name}::ApplicationAbi;
impl WithContractAbi for {project_name}Contract {{
type Abi = {module_name}::{project_name}Abi;
}}

impl Contract for ApplicationContract {{
impl Contract for {project_name}Contract {{
type Message = ();
type Parameters = ();
type InstantiationArgument = ();

async fn load(runtime: ContractRuntime<Self>) -> Self {{
let state = Application::load(runtime.root_view_storage_context())
let state = {project_name}::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
ApplicationContract {{ state, runtime }}
{project_name}Contract {{ state, runtime }}
}}

async fn instantiate(&mut self, _argument: Self::InstantiationArgument) {{}}
Expand Down
12 changes: 6 additions & 6 deletions linera-service/template/lib.rs.template
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use linera_sdk::base::{ContractAbi, ServiceAbi};
use linera_sdk::base::{{ContractAbi, ServiceAbi}};

pub struct ApplicationAbi;
pub struct {project_name}Abi;

impl ContractAbi for ApplicationAbi {
impl ContractAbi for {project_name}Abi {{
type Operation = ();
type Response = ();
}
}}

impl ServiceAbi for ApplicationAbi {
impl ServiceAbi for {project_name}Abi {{
type Query = ();
type QueryResponse = ();
}
}}
18 changes: 9 additions & 9 deletions linera-service/template/service.rs.template
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@

mod state;

use self::state::Application;
use self::state::{project_name};
use linera_sdk::{{
base::WithServiceAbi,
views::{{View, ViewStorageContext}},
Service, ServiceRuntime,
}};

pub struct ApplicationService {{
state: Application,
pub struct {project_name}Service {{
state: {project_name},
runtime: ServiceRuntime<Self>,
}}

linera_sdk::service!(ApplicationService);
linera_sdk::service!({project_name}Service);

impl WithServiceAbi for ApplicationService {{
type Abi = {project_name}::ApplicationAbi;
impl WithServiceAbi for {project_name}Service {{
type Abi = {module_name}::{project_name}Abi;
}}

impl Service for ApplicationService {{
impl Service for {project_name}Service {{
type Parameters = ();

async fn new(runtime: ServiceRuntime<Self>) -> Self {{
let state = Application::load(runtime.root_view_storage_context())
let state = {project_name}::load(runtime.root_view_storage_context())
.await
.expect("Failed to load state");
ApplicationService {{ state, runtime }}
{project_name}Service {{ state, runtime }}
}}

async fn handle_query(&self, _query: Self::Query) -> Self::QueryResponse {{
Expand Down
6 changes: 3 additions & 3 deletions linera-service/template/state.rs.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use linera_sdk::views::{linera_views, RegisterView, RootView, ViewStorageContext};
use linera_sdk::views::{{linera_views, RegisterView, RootView, ViewStorageContext}};

#[derive(RootView, async_graphql::SimpleObject)]
#[view(context = "ViewStorageContext")]
pub struct Application {
pub struct {project_name} {{
pub value: RegisterView<u64>,
// Add fields here.
}
}}

0 comments on commit d529f7b

Please sign in to comment.