Skip to content

Commit

Permalink
feat(proto-build): cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
SpekalsG3 committed Aug 20, 2024
1 parent 802211b commit 2173d7f
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 82 deletions.
4 changes: 2 additions & 2 deletions proto-build/src/commands/apply_patches.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};
use glob::glob;
use crate::utils::patch_file::patch_file;
use glob::glob;
use std::path::{Path, PathBuf};

/// Fix clashing type names in prost-generated code.
fn apply_cosmos_staking_patches(out_dir: &Path) {
Expand Down
2 changes: 1 addition & 1 deletion proto-build/src/commands/cleanup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use glob::glob;
use std::fs;
use std::path::Path;
use glob::glob;

const EXCLUDED_PROTO_PACKAGES: &[&str] = &["amino", "gogoproto", "google", "tendermint"];

Expand Down
4 changes: 2 additions & 2 deletions proto-build/src/commands/export.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs;
use std::path::Path;
use crate::consts::{ARCHWAY_DIR, COSMOS_SDK_DIR, IBC_DIR, WASMD_DIR};
use crate::utils::run::run_buf_export;
use std::fs;
use std::path::Path;

pub fn export(submodules_dir: &Path, proto_dir: &Path) {
if proto_dir.exists() {
Expand Down
8 changes: 2 additions & 6 deletions proto-build/src/commands/generate.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::utils::run::run_cmd;
use std::fs;
use std::path::Path;
use crate::utils::run::run_cmd;

pub fn generate(
buf_gen_path: &Path,
proto_path: &Path,
out_dir: &Path,
) -> crate::Result<String> {
pub fn generate(buf_gen_path: &Path, proto_path: &Path, out_dir: &Path) -> crate::Result<String> {
println!("Generating proto...");

if out_dir.exists() {
Expand Down
2 changes: 1 addition & 1 deletion proto-build/src/commands/output_version.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::consts::{ARCHWAY_REV, COSMOS_SDK_REV, IBC_REV, WASMD_REV};
use std::fs;
use std::path::Path;
use crate::consts::{ARCHWAY_REV, COSMOS_SDK_REV, IBC_REV, WASMD_REV};

pub fn output_versions(out_dir: &Path) {
println!("Writing versions...");
Expand Down
4 changes: 2 additions & 2 deletions proto-build/src/commands/rustfmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::path::{Path, PathBuf};
use glob::glob;
use crate::utils::run::run_cmd;
use glob::glob;
use std::path::{Path, PathBuf};

fn collect_files(dir: &Path, pattern: &str) -> crate::Result<Vec<PathBuf>> {
// dir.join("**").join(pattern);
Expand Down
34 changes: 29 additions & 5 deletions proto-build/src/commands/update_submodules.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
use std::path::Path;
use crate::consts::{ARCHWAY_DIR, ARCHWAY_REV, COSMOS_SDK_DIR, COSMOS_SDK_REV, IBC_DIR, IBC_REV, WASMD_DIR, WASMD_REV};
use crate::consts::{
ARCHWAY_DIR, ARCHWAY_REV, COSMOS_SDK_DIR, COSMOS_SDK_REV, IBC_DIR, IBC_REV, WASMD_DIR,
WASMD_REV,
};
use crate::utils::run::run_git;
use std::path::Path;

pub fn update_submodules(submodules_dir: &Path) {
run_git(["submodule", "update", "--init"]).unwrap();
run_git(["submodule", "foreach", "git", "fetch"]).unwrap();

println!("Updating archway-network/archway submodule...");
let archway_dir = submodules_dir.join(ARCHWAY_DIR);
run_git(["-C", archway_dir.to_str().unwrap(), "reset", "--hard", ARCHWAY_REV]).unwrap();
run_git([
"-C",
archway_dir.to_str().unwrap(),
"reset",
"--hard",
ARCHWAY_REV,
])
.unwrap();

println!("Updating cosmos/cosmos-sdk submodule...");
let sdk_dir = submodules_dir.join(COSMOS_SDK_DIR);
run_git(["-C", sdk_dir.to_str().unwrap(), "reset", "--hard", COSMOS_SDK_REV]).unwrap();
run_git([
"-C",
sdk_dir.to_str().unwrap(),
"reset",
"--hard",
COSMOS_SDK_REV,
])
.unwrap();

println!("Updating cosmos/ibc-go submodule...");
let ibc_dir = submodules_dir.join(IBC_DIR);
run_git(["-C", ibc_dir.to_str().unwrap(), "reset", "--hard", IBC_REV]).unwrap();

println!("Updating wasmd submodule...");
let wasmd_dir = submodules_dir.join(WASMD_DIR);
run_git(["-C", wasmd_dir.to_str().unwrap(), "reset", "--hard", WASMD_REV]).unwrap();
run_git([
"-C",
wasmd_dir.to_str().unwrap(),
"reset",
"--hard",
WASMD_REV,
])
.unwrap();
}
10 changes: 5 additions & 5 deletions proto-build/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
mod parser;
mod utils;
mod commands;
mod consts;
mod parser;
mod utils;

use std::{io, path::Path, process};
use std::path::PathBuf;
use std::{io, path::Path, process};

Check failure on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

unused import: `process`

Check failure on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Lints (1.70.0)

unused import: `process`

Check failure on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

unused import: `process`

Check failure on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Lints (1.70.0)

unused import: `process`

Check warning on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

unused import: `process`

Check warning on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

unused import: `process`

Check warning on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (1.70.0)

unused import: `process`

Check warning on line 7 in proto-build/src/main.rs

View workflow job for this annotation

GitHub Actions / Test Suite (1.70.0)

unused import: `process`

use crate::parser::generate_advanced_struct;
use error_chain::error_chain;
use crate::commands::apply_patches::apply_patches;
use crate::commands::cleanup::cleanup;
use crate::commands::export::export;
Expand All @@ -16,7 +14,9 @@ use crate::commands::output_version::output_versions;
use crate::commands::rustfmt::rustfmt;
use crate::commands::update_submodules::update_submodules;
use crate::consts::{OUT_DIR, PROTO_DIR};
use crate::parser::generate_advanced_struct;
use crate::utils::run::run_cargo;
use error_chain::error_chain;

error_chain! {
foreign_links {
Expand Down
23 changes: 14 additions & 9 deletions proto-build/src/parser/commands/load_and_patch_any.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use crate::parser::consts::GENERICS;
use crate::parser::utils::common::{
create_punctuated, fields_as_named, gen_generic, item_as_struct,
};
use crate::parser::utils::gen_type_param::gen_type_param;
use crate::parser::utils::is_important::{is_important, FoundEnclosure};
use glob::glob;
use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream};
use quote::TokenStreamExt;
use syn::{Attribute, AttrStyle, File, GenericParam, Item, MacroDelimiter, Meta, MetaList};
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use syn::token::Paren;
use crate::parser::consts::GENERICS;
use crate::parser::utils::is_important::{FoundEnclosure, is_important};
use crate::parser::utils::common::{fields_as_named, item_as_struct, create_punctuated, gen_generic};
use crate::parser::utils::gen_type_param::gen_type_param;
use syn::{AttrStyle, Attribute, File, GenericParam, Item, MacroDelimiter, Meta, MetaList};

pub fn load_and_patch_any(out_dir: &Path) -> BTreeMap<String, (File, BTreeMap<String, usize>)> {
// Map all file ASTs
let mut project_tokens = BTreeMap::new();

// Get all generated files
let src_files_glob = out_dir.join("*.rs");
let src_files: Vec<PathBuf> = glob(src_files_glob.to_str().unwrap()).unwrap().flatten().collect();
let src_files: Vec<PathBuf> = glob(src_files_glob.to_str().unwrap())
.unwrap()
.flatten()
.collect();

for src in src_files {
let current_file = fs::read_to_string(&src).unwrap();
Expand Down
23 changes: 14 additions & 9 deletions proto-build/src/parser/commands/patch_generics.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use std::cmp::Ordering;
use std::collections::BTreeMap;
use crate::parser::consts::GENERICS;
use crate::parser::utils::common::{
create_punctuated, fields_as_named, gen_generic, item_as_struct,
};
use crate::parser::utils::gen_type_param::gen_type_param;
use crate::parser::utils::is_important::{is_important, FoundEnclosure};
use proc_macro2::{Ident, Literal, Punct, Spacing, Span, TokenStream};
use quote::TokenStreamExt;
use syn::{AngleBracketedGenericArguments, Attribute, AttrStyle, File, GenericArgument, GenericParam, ItemStruct, MacroDelimiter, Meta, MetaList, Path, PathArguments, PathSegment, Type, TypePath};
use std::cmp::Ordering;
use std::collections::BTreeMap;
use syn::punctuated::Punctuated;
use syn::token::Paren;
use crate::parser::consts::GENERICS;
use crate::parser::utils::is_important::{FoundEnclosure, is_important};
use crate::parser::utils::common::{fields_as_named, item_as_struct, create_punctuated, gen_generic};
use crate::parser::utils::gen_type_param::gen_type_param;
use syn::{
AngleBracketedGenericArguments, AttrStyle, Attribute, File, GenericArgument, GenericParam,
ItemStruct, MacroDelimiter, Meta, MetaList, Path, PathArguments, PathSegment, Type, TypePath,
};

pub fn patch_generics(files: &mut BTreeMap<String, (File, BTreeMap<String, usize>)>) {
let mut updated_files = BTreeMap::new();
Expand Down Expand Up @@ -87,7 +92,7 @@ pub fn patch_generics(files: &mut BTreeMap<String, (File, BTreeMap<String, usize
let ty_struct = item_as_struct(
key.items.get_mut(*s.get("GenesisState").unwrap()).unwrap(),
)
.unwrap();
.unwrap();
found_ty = Some(field_ty);
new_total_generics = push_generics(ty_struct, ty, new_total_generics);
} else if let Some(i) = structs.get(&ident_name) {
Expand All @@ -113,7 +118,7 @@ pub fn patch_generics(files: &mut BTreeMap<String, (File, BTreeMap<String, usize
.get_mut(*other_structs.get(&ident_name).unwrap())
.unwrap(),
)
.unwrap();
.unwrap();
found_ty = Some(field_ty);
new_total_generics = push_generics(ty_struct, ty, new_total_generics);
}
Expand Down
8 changes: 5 additions & 3 deletions proto-build/src/parser/commands/patch_impls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::parser::consts::GENERICS;
use crate::parser::utils::common::{gen_generic, item_as_struct, type_as_path};
use std::collections::BTreeMap;
use syn::{AngleBracketedGenericArguments, File, GenericArgument, Item, PathArguments, Type, TypePath};
use syn::punctuated::Punctuated;
use crate::parser::consts::GENERICS;
use crate::parser::utils::common::{type_as_path, item_as_struct, gen_generic};
use syn::{
AngleBracketedGenericArguments, File, GenericArgument, Item, PathArguments, Type, TypePath,
};

pub fn patch_impls(files: &mut BTreeMap<String, (File, BTreeMap<String, usize>)>) {
for (_, (ast, structs)) in files.iter_mut() {
Expand Down
14 changes: 5 additions & 9 deletions proto-build/src/parser/commands/save.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::utils::patch_file::patch_file;
use quote::quote;
use regex::Regex;
use std::collections::BTreeMap;
use std::fs;
use std::path::Path;
use quote::quote;
use regex::Regex;
use syn::File;
use crate::utils::patch_file::patch_file;

pub fn save(out_dir: &Path, files: &BTreeMap<String, (File, BTreeMap<String, usize>)>) {
for (file, (data, _)) in files.iter() {
Expand All @@ -30,13 +30,9 @@ pub fn save(out_dir: &Path, files: &BTreeMap<String, (File, BTreeMap<String, usi
),
)],
)
.unwrap();
.unwrap();

// Export the generated structure and save in file
fs::write(
out_dir.join(new_file),
quote!(#data).to_string(),
)
.unwrap();
fs::write(out_dir.join(new_file), quote!(#data).to_string()).unwrap();
}
}
10 changes: 4 additions & 6 deletions proto-build/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
mod commands;
mod utils;
mod consts;
mod utils;

use std::path::Path;
use crate::parser::commands::{
load_and_patch_any::load_and_patch_any,
patch_generics::patch_generics,
patch_impls::patch_impls,
save::save,
load_and_patch_any::load_and_patch_any, patch_generics::patch_generics,
patch_impls::patch_impls, save::save,
};
use std::path::Path;

pub fn generate_advanced_struct(out_dir: &Path) -> crate::Result<()> {
println!("Loading and patching all files containing Any");
Expand Down
18 changes: 2 additions & 16 deletions proto-build/src/parser/utils/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,8 @@ use proc_macro2::Span;
use syn::punctuated::Punctuated;
use syn::token::PathSep;
use syn::{
Field,
Fields,
FieldsNamed,
GenericArgument,
Ident,
Item,
ItemStruct,
Path,
PathArguments,
PathSegment,
TraitBound,
TraitBoundModifier,
Type,
TypeParam,
TypeParamBound,
TypePath,
Field, Fields, FieldsNamed, GenericArgument, Ident, Item, ItemStruct, Path, PathArguments,

Check failure on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, and `TypeParam`

Check failure on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Lints (1.70.0)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, `TypeParam`

Check failure on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, and `TypeParam`

Check failure on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Lints (1.70.0)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, `TypeParam`

Check warning on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, and `TypeParam`

Check warning on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Test Suite (stable)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, and `TypeParam`

Check warning on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Test Suite (1.70.0)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, `TypeParam`

Check warning on line 5 in proto-build/src/parser/utils/common.rs

View workflow job for this annotation

GitHub Actions / Test Suite (1.70.0)

unused imports: `Field`, `GenericArgument`, `TraitBoundModifier`, `TraitBound`, `TypeParamBound`, `TypeParam`
PathSegment, TraitBound, TraitBoundModifier, Type, TypeParam, TypeParamBound, TypePath,
};

pub fn item_as_struct(item: &mut Item) -> Option<&mut ItemStruct> {
Expand Down
2 changes: 1 addition & 1 deletion proto-build/src/parser/utils/gen_type_param.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parser::utils::common::create_punctuated;
use proc_macro2::{Ident, Span};
use syn::{Path, TraitBound, TraitBoundModifier, TypeParam, TypeParamBound};
use crate::parser::utils::common::create_punctuated;

fn trait_param_bound(path: Vec<&str>) -> TypeParamBound {
TypeParamBound::Trait(TraitBound {
Expand Down
2 changes: 1 addition & 1 deletion proto-build/src/parser/utils/is_important.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use syn::{Field, GenericArgument, PathArguments, TypePath};
use crate::parser::utils::common::type_as_path;
use syn::{Field, GenericArgument, PathArguments, TypePath};

pub enum FoundEnclosure {
Option,
Expand Down
2 changes: 1 addition & 1 deletion proto-build/src/parser/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod common;
pub mod gen_type_param;
pub mod is_important;
pub mod common;
4 changes: 2 additions & 2 deletions proto-build/src/utils/patch_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs, io};
use std::path::Path;
use regex::Regex;
use std::path::Path;
use std::{fs, io};

pub fn patch_file(path: &Path, replacements: &[(&str, &str)]) -> io::Result<()> {
let mut contents = fs::read_to_string(&path)?;

Check failure on line 6 in proto-build/src/utils/patch_file.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

the borrowed expression implements the required traits

Check failure on line 6 in proto-build/src/utils/patch_file.rs

View workflow job for this annotation

GitHub Actions / Lints (1.70.0)

the borrowed expression implements the required traits

Check failure on line 6 in proto-build/src/utils/patch_file.rs

View workflow job for this annotation

GitHub Actions / Lints (stable)

the borrowed expression implements the required traits

Check failure on line 6 in proto-build/src/utils/patch_file.rs

View workflow job for this annotation

GitHub Actions / Lints (1.70.0)

the borrowed expression implements the required traits
Expand Down
2 changes: 1 addition & 1 deletion proto-build/src/utils/run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ffi::OsStr;
use std::{io, process};
use std::path::Path;
use std::{io, process};

pub fn run_git(args: impl IntoIterator<Item = impl AsRef<OsStr>>) -> crate::Result<String> {
run_cmd("git", args)
Expand Down

0 comments on commit 2173d7f

Please sign in to comment.