Skip to content

Commit 9b8d59b

Browse files
committed
feat(ci): add clippy check to publish workflow and fix lint warnings
1 parent 31578a7 commit 9b8d59b

File tree

11 files changed

+105
-95
lines changed

11 files changed

+105
-95
lines changed

.github/workflows/publish.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
name: Publish Crate
22
on:
33
push:
4+
branches:
5+
- main
46
tags:
57
- 'v[0-9]+.[0-9]+.[0-9]+*'
68

@@ -20,6 +22,7 @@ jobs:
2022
- name: Lint
2123
run: |
2224
cargo fmt -- --check
25+
cargo clippy --all-features -- -D warnings
2326
2427
- name: Test
2528
run: cargo test --all-features

src/cli.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub struct DeployCommand {
6363

6464
impl Cli {
6565
pub fn new() -> Result<Self, Error> {
66-
EnvConfig::load().map_err(|e| Error::ConfigError(e.to_string()))?;
66+
EnvConfig::load().map_err(|e| Error::Config(e.to_string()))?;
6767

6868
let cli = Self::parse();
6969
Ok(cli)
@@ -75,7 +75,7 @@ impl Cli {
7575
Some(InitMode::Rust(args)) => RustCommand::init(args),
7676
None => legacy_init()
7777
.await
78-
.map_err(|e| Error::InitializationError(e.to_string())),
78+
.map_err(|e| Error::Initialization(e.to_string())),
7979
},
8080
Commands::Build(cmd) => match &cmd.mode {
8181
BuildMode::Rust(args) => RustCommand::build(args),

src/commands/rust/build.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clap::Args;
44
use std::{
55
fs,
66
io::{self, BufRead},
7-
path::PathBuf,
7+
path::{Path, PathBuf},
88
process::{Command, Stdio},
99
time::Instant,
1010
};
@@ -89,7 +89,7 @@ fn build_project(path: &PathBuf, release: bool, verbose: bool) -> Result<BuildRe
8989
.read_dir()?
9090
.filter_map(|entry| entry.ok())
9191
.find(|entry| entry.path().extension() == Some("wasm".as_ref()))
92-
.ok_or_else(|| Error::BuildError("No .wasm file found in target directory".to_string()))?;
92+
.ok_or_else(|| Error::Build("No .wasm file found in target directory".to_string()))?;
9393

9494
// Copy the .wasm file to `lib.wasm`
9595
let final_wasm_path = path.join("lib.wasm");
@@ -146,43 +146,39 @@ fn run_cargo_build(path: &PathBuf, release: bool, verbose: bool) -> Result<(), E
146146
.stdout(Stdio::piped())
147147
.stderr(Stdio::piped())
148148
.spawn()
149-
.map_err(|e| Error::BuildError(format!("Failed to start build process: {}", e)))?;
149+
.map_err(|e| Error::Build(format!("Failed to start build process: {}", e)))?;
150150

151151
// Stream output line by line in verbose mode
152152
if verbose {
153153
if let Some(stdout) = cmd.stdout.take() {
154154
let stdout_reader = io::BufReader::new(stdout);
155-
for line in stdout_reader.lines() {
156-
if let Ok(line) = line {
157-
println!("{}", line);
158-
}
155+
for line in stdout_reader.lines().map_while(Result::ok) {
156+
println!("{}", line);
159157
}
160158
}
161159

162160
if let Some(stderr) = cmd.stderr.take() {
163161
let stderr_reader = io::BufReader::new(stderr);
164-
for line in stderr_reader.lines() {
165-
if let Ok(line) = line {
166-
eprintln!("{}", line);
167-
}
162+
for line in stderr_reader.lines().map_while(Result::ok) {
163+
eprintln!("{}", line);
168164
}
169165
}
170166
}
171167

172168
// Wait for the command to finish and check if it was successful
173169
let output = cmd
174170
.wait_with_output()
175-
.map_err(|e| Error::BuildError(format!("Build process failed: {}", e)))?;
171+
.map_err(|e| Error::Build(format!("Build process failed: {}", e)))?;
176172

177173
if !output.status.success() {
178174
let error_msg = String::from_utf8_lossy(&output.stderr);
179-
return Err(Error::BuildError(error_msg.to_string()));
175+
return Err(Error::Build(error_msg.to_string()));
180176
}
181177

182178
Ok(())
183179
}
184180

185-
fn validate_project_structure(path: &PathBuf) -> Result<(), Error> {
181+
fn validate_project_structure(path: &Path) -> Result<(), Error> {
186182
// Check if Cargo.toml exists
187183
let cargo_toml = path.join("Cargo.toml");
188184
if !cargo_toml.exists() {
@@ -207,7 +203,7 @@ fn ensure_wasm_target() -> Result<(), Error> {
207203
let output = Command::new("rustup")
208204
.args(["target", "list", "--installed"])
209205
.output()
210-
.map_err(|e| Error::BuildError(format!("Failed to check installed targets: {}", e)))?;
206+
.map_err(|e| Error::Build(format!("Failed to check installed targets: {}", e)))?;
211207

212208
let installed_targets = String::from_utf8_lossy(&output.stdout);
213209

@@ -217,10 +213,10 @@ fn ensure_wasm_target() -> Result<(), Error> {
217213
let install_output = Command::new("rustup")
218214
.args(["target", "add", "wasm32-unknown-unknown"])
219215
.output()
220-
.map_err(|e| Error::BuildError(format!("Failed to add wasm target: {}", e)))?;
216+
.map_err(|e| Error::Build(format!("Failed to add wasm target: {}", e)))?;
221217

222218
if !install_output.status.success() {
223-
return Err(Error::BuildError(
219+
return Err(Error::Build(
224220
"Failed to install wasm32-unknown-unknown target".to_string(),
225221
));
226222
}
@@ -233,7 +229,7 @@ fn get_compiler_version() -> Result<String, Error> {
233229
let rustc_version = Command::new("rustc")
234230
.arg("--version")
235231
.output()
236-
.map_err(|e| Error::BuildError(e.to_string()))?;
232+
.map_err(|e| Error::Build(e.to_string()))?;
237233

238234
Ok(String::from_utf8_lossy(&rustc_version.stdout)
239235
.trim()

src/commands/rust/deploy.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ use ethers::{
88
signers::{LocalWallet, Signer},
99
types::{TransactionReceipt, H256, U64},
1010
};
11-
use std::{path::PathBuf, str::FromStr, sync::Arc, time::Duration};
11+
use std::{
12+
path::{Path, PathBuf},
13+
str::FromStr,
14+
sync::Arc,
15+
time::Duration,
16+
};
1217

1318
const DEFAULT_GAS_LIMIT: u64 = 30_000_000;
1419
const DEFAULT_GAS_PRICE: u64 = 0;
@@ -91,16 +96,16 @@ pub(super) async fn execute(args: &DeployArgs) -> Result<(), Error> {
9196

9297
fn validate_wasm_file(wasm_file: &PathBuf) -> Result<(), Error> {
9398
if !wasm_file.exists() {
94-
return Err(Error::DeploymentError(format!(
99+
return Err(Error::Deployment(format!(
95100
"WASM file not found: {}",
96101
wasm_file.display()
97102
)));
98103
}
99104

100105
let wasm_bytes = std::fs::read(wasm_file)
101-
.map_err(|e| Error::DeploymentError(format!("Failed to read WASM file: {}", e)))?;
102-
if wasm_bytes.len() < 4 || &wasm_bytes[0..4] != &[0x00, 0x61, 0x73, 0x6d] {
103-
return Err(Error::DeploymentError(
106+
.map_err(|e| Error::Deployment(format!("Failed to read WASM file: {}", e)))?;
107+
if wasm_bytes.len() < 4 || wasm_bytes[0..4] != [0x00, 0x61, 0x73, 0x6d] {
108+
return Err(Error::Deployment(
104109
"Invalid WASM file: missing magic number".to_string(),
105110
));
106111
}
@@ -110,13 +115,13 @@ fn validate_wasm_file(wasm_file: &PathBuf) -> Result<(), Error> {
110115
fn create_wallet(private_key: &str, chain_id: u64) -> Result<LocalWallet, Error> {
111116
let clean_key = private_key.trim_start_matches("0x");
112117
if clean_key.len() != 64 {
113-
return Err(Error::DeploymentError(
118+
return Err(Error::Deployment(
114119
"Private key must be 64 hex characters.".to_string(),
115120
));
116121
}
117122

118123
LocalWallet::from_str(clean_key)
119-
.map_err(|e| Error::DeploymentError(format!("Invalid private key: {}", e)))
124+
.map_err(|e| Error::Deployment(format!("Invalid private key: {}", e)))
120125
.map(|wallet| wallet.with_chain_id(chain_id))
121126
}
122127

@@ -127,18 +132,18 @@ async fn prepare_deploy_transaction(
127132
gas_price: u64,
128133
) -> Result<TransactionRequest, Error> {
129134
let provider = Provider::<Http>::try_from(&network_config.endpoint)
130-
.map_err(|e| Error::NetworkError(format!("Failed to create provider: {}", e)))?;
135+
.map_err(|e| Error::Network(format!("Failed to create provider: {}", e)))?;
131136

132137
let wasm_bytes = std::fs::read(wasm_file)
133-
.map_err(|e| Error::DeploymentError(format!("Failed to read WASM file: {}", e)))?;
138+
.map_err(|e| Error::Deployment(format!("Failed to read WASM file: {}", e)))?;
134139
println!("📦 WASM file size: {} bytes", wasm_bytes.len());
135140

136141
let gas_price = if gas_price == 0 {
137142
println!("⛽ Estimating gas price...");
138143
provider
139144
.get_gas_price()
140145
.await
141-
.map_err(|e| Error::NetworkError(format!("Failed to fetch gas price: {}", e)))?
146+
.map_err(|e| Error::Network(format!("Failed to fetch gas price: {}", e)))?
142147
} else {
143148
U256::from(gas_price)
144149
};
@@ -161,23 +166,23 @@ async fn send_tx(
161166
) -> Result<TransactionReceipt, Error> {
162167
let gas_limit = tx.gas;
163168
let provider = Provider::<Http>::try_from(&network_config.endpoint)
164-
.map_err(|e| Error::NetworkError(format!("Failed to create provider: {}", e)))?;
169+
.map_err(|e| Error::Network(format!("Failed to create provider: {}", e)))?;
165170
let client = Arc::new(SignerMiddleware::new(provider.clone(), wallet));
166171

167172
println!("🚀 Sending transaction...");
168173
let pending_tx = client
169174
.send_transaction(tx, None)
170175
.await
171-
.map_err(|e| Error::DeploymentError(format!("Failed to send transaction: {}", e)))?;
176+
.map_err(|e| Error::Deployment(format!("Failed to send transaction: {}", e)))?;
172177

173178
let receipt = pending_tx
174179
.await
175-
.map_err(|e| Error::DeploymentError(format!("Transaction failed: {}", e)))?
176-
.ok_or_else(|| Error::DeploymentError("Transaction receipt not found".to_string()))?;
180+
.map_err(|e| Error::Deployment(format!("Transaction failed: {}", e)))?
181+
.ok_or_else(|| Error::Deployment("Transaction receipt not found".to_string()))?;
177182

178183
if receipt.status != Some(U64::from(1)) {
179184
print_deployment_result(&receipt, gas_limit);
180-
return Err(Error::DeploymentError("Transaction failed".to_string()));
185+
return Err(Error::Deployment("Transaction failed".to_string()));
181186
}
182187

183188
if confirmations > 0 {
@@ -197,12 +202,10 @@ async fn wait_for_confirmations(
197202
if let Some(receipt) = provider
198203
.get_transaction_receipt(tx_hash)
199204
.await
200-
.map_err(|e| {
201-
Error::DeploymentError(format!("Failed to get transaction receipt: {}", e))
202-
})?
205+
.map_err(|e| Error::Deployment(format!("Failed to get transaction receipt: {}", e)))?
203206
{
204207
let current_block = provider.get_block_number().await.map_err(|e| {
205-
Error::DeploymentError(format!("Failed to get current block number: {}", e))
208+
Error::Deployment(format!("Failed to get current block number: {}", e))
206209
})?;
207210

208211
if let Some(block_number) = receipt.block_number {
@@ -219,7 +222,7 @@ async fn wait_for_confirmations(
219222
fn print_deployment_start(
220223
wallet: &LocalWallet,
221224
network: &NetworkConfig,
222-
wasm_file: &PathBuf,
225+
wasm_file: &Path,
223226
) -> Result<(), Error> {
224227
println!("\n🚀 Starting Deployment");
225228
println!("====================");
@@ -305,7 +308,7 @@ impl NetworkConfig {
305308
chain_id,
306309
})
307310
} else {
308-
Err(Error::NetworkError(
311+
Err(Error::Network(
309312
"Please specify either --local, --dev, or both --rpc and --chain-id.".to_string(),
310313
))
311314
}

src/commands/rust/init.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
use super::{
2-
constants::{BASIC_TEMPLATE_CARGO_TOML, BASIC_TEMPLATE_LIB_RS},
2+
constants::{
3+
BASIC_TEMPLATE_CARGO_TOML,
4+
BASIC_TEMPLATE_LIB_RS,
5+
BASIC_TEMPLATE_MAKEFILE,
6+
BASIC_TEMPLATE_RUST_TOOLCHAIN,
7+
},
38
template_manager::TemplateManager,
49
utils::Tool,
510
};
@@ -8,7 +13,10 @@ use crate::{
813
utils::fs::{self, create_dir_if_not_exists},
914
};
1015
use clap::Args;
11-
use std::{path::PathBuf, process::Command};
16+
use std::{
17+
path::{Path, PathBuf},
18+
process::Command,
19+
};
1220

1321
const DEFAULT_TEMPLATE: &str = "greeting";
1422

@@ -86,11 +94,12 @@ fn init_project(
8694
Ok(())
8795
}
8896

89-
fn create_default_template(project_path: &PathBuf) -> Result<(), Error> {
97+
fn create_default_template(project_path: &Path) -> Result<(), Error> {
9098
std::fs::write(project_path.join("Cargo.toml"), BASIC_TEMPLATE_CARGO_TOML)
91-
.map_err(|e| Error::InitializationError(format!("Failed to create Cargo.toml: {}", e)))?;
99+
.map_err(|e| Error::Initialization(format!("Failed to create Cargo.toml: {}", e)))?;
92100

93101
std::fs::write(project_path.join("lib.rs"), BASIC_TEMPLATE_LIB_RS)
102+
.map_err(|e| Error::Initialization(format!("Failed to create lib.rs: {}", e)))?;
94103

95104
std::fs::write(project_path.join("Makefile"), BASIC_TEMPLATE_MAKEFILE)
96105
.map_err(|e| Error::Initialization(format!("Failed to create Makefile: {}", e)))?;
@@ -104,12 +113,12 @@ fn create_default_template(project_path: &PathBuf) -> Result<(), Error> {
104113
}
105114

106115
fn create_from_template(
107-
project_path: &PathBuf,
116+
project_path: &Path,
108117
args: &InitArgs,
109118
template_manager: &TemplateManager,
110119
) -> Result<(), Error> {
111120
let template = template_manager.get(&args.template).ok_or_else(|| {
112-
Error::InitializationError(format!(
121+
Error::Initialization(format!(
113122
"Template '{}' not found. Use --list to see available templates",
114123
args.template
115124
))
@@ -121,7 +130,7 @@ fn create_from_template(
121130
Ok(())
122131
}
123132

124-
fn init_git_repository(project_path: &PathBuf) {
133+
fn init_git_repository(project_path: &Path) {
125134
if !project_path.join(".git").exists() {
126135
println!("🔧 Initializing git repository...");
127136
let _ = Command::new("git")
@@ -131,7 +140,7 @@ fn init_git_repository(project_path: &PathBuf) {
131140
}
132141
}
133142

134-
fn print_next_steps(template_name: &str, project_path: &PathBuf) {
143+
fn print_next_steps(template_name: &str, project_path: &Path) {
135144
println!("✅ Project initialized successfully!");
136145
println!("📂 Project directory: {}", project_path.display());
137146
println!("📝 Next steps:");
@@ -152,7 +161,7 @@ mod tests {
152161
#[test]
153162
fn test_create_default_template() -> Result<(), Box<dyn std::error::Error>> {
154163
let temp = assert_fs::TempDir::new()?;
155-
create_default_template(&temp.path().to_path_buf())?;
164+
create_default_template(temp.path())?;
156165

157166
temp.child("Cargo.toml").assert(BASIC_TEMPLATE_CARGO_TOML);
158167
temp.child("lib.rs").assert(BASIC_TEMPLATE_LIB_RS);

0 commit comments

Comments
 (0)