From d290e84c2c7c0e589724ae0e60f824ea4179833c Mon Sep 17 00:00:00 2001 From: aianov Date: Tue, 15 Jul 2025 20:52:46 +0500 Subject: [PATCH 1/2] fix: fixed end gen --- src/rust.rs | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/rust.rs b/src/rust.rs index e86afd4..cc92186 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -283,11 +283,18 @@ impl From for ErrorCode {{ )?; } + let mut all_endpoints = vec![]; for s in &data.services { for endpoint in &s.endpoints { - write!( - &mut f, - " + all_endpoints.push(endpoint); + } + } + all_endpoints.sort_by(|a, b| a.code.cmp(&b.code)); + + for endpoint in all_endpoints { + write!( + &mut f, + " impl WsRequest for {end_name2}Request {{ type Response = {end_name2}Response; const METHOD_ID: u32 = {code}; @@ -297,11 +304,10 @@ impl WsResponse for {end_name2}Response {{ type Request = {end_name2}Request; }} ", - end_name2 = endpoint.name.to_case(Case::Pascal), - code = endpoint.code, - schema = serde_json::to_string_pretty(&endpoint).unwrap() - )?; - } + end_name2 = endpoint.name.to_case(Case::Pascal), + code = endpoint.code, + schema = serde_json::to_string_pretty(&endpoint).unwrap() + )?; } f.flush()?; drop(f); @@ -330,21 +336,32 @@ pub fn check_endpoint_codes(data: &Data, mut writer: impl Write) -> eyre::Result variants.push(EnumVariant::new(e.name.clone(), e.code as _)); } } + + variants.sort_by(|a, b| a.value.cmp(&b.value)); + let enum_ = Type::enum_("Endpoint", variants); writeln!(writer, "{}", enum_.to_rust_decl(false))?; // if it compiles, there're no duplicate codes or names Ok(()) } pub fn dump_endpoint_schema(data: &Data, mut writer: impl Write) -> eyre::Result<()> { - let mut cases = vec![]; + let mut endpoint_cases = vec![]; for s in &data.services { for e in &s.endpoints { - cases.push(format!( - "Self::{name} => {name}Request::SCHEMA,", - name = e.name.to_case(Case::Pascal), + endpoint_cases.push(( + e.code, + format!( + "Self::{name} => {name}Request::SCHEMA,", + name = e.name.to_case(Case::Pascal), + ), )); } } + + endpoint_cases.sort_by(|a, b| a.0.cmp(&b.0)); + + let cases: Vec = endpoint_cases.into_iter().map(|(_, case)| case).collect(); + let code = format!( r#" impl EnumEndpoint {{ From 4d2107ecf922415a879e6d28fc1f2d62cfb35406 Mon Sep 17 00:00:00 2001 From: aianov Date: Tue, 15 Jul 2025 21:28:23 +0500 Subject: [PATCH 2/2] fix: clippy --- src/docs.rs | 13 +++++++------ src/main.rs | 18 ++++++++---------- src/rust.rs | 6 +++--- src/service.rs | 1 + src/sql.rs | 9 +++------ 5 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/docs.rs b/src/docs.rs index 77b29ff..34e1035 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -70,12 +70,12 @@ pub fn gen_systemd_services(data: &Data, app_name: &str, user: &str) -> eyre::Re for srv in &data.services { let service_filename = data .project_root - .join(format!("etc")) - .join(format!("systemd")) + .join("etc") + .join("systemd") .join(format!("{}_{}.service", app_name, srv.name)); let mut service_file = File::create(&service_filename)?; let v = get_systemd_service(app_name, &srv.name, user); - write!(&mut service_file, "{}", v)?; + write!(&mut service_file, "{v}")?; } Ok(()) } @@ -92,8 +92,9 @@ pub fn get_error_messages(root: &Path) -> eyre::Result { } if !def_filename.exists() { - let mut file = OpenOptions::new() + let _file = OpenOptions::new() .create(true) + .truncate(true) .write(true) .open(&def_filename)?; } @@ -102,7 +103,7 @@ pub fn get_error_messages(root: &Path) -> eyre::Result { let def_file = std::fs::read(&def_filename)?; if def_file.is_empty() { - return Ok(ErrorMessages { + Ok(ErrorMessages { language: String::from("TODO"), codes: vec![ErrorMessage { code: 0, @@ -110,7 +111,7 @@ pub fn get_error_messages(root: &Path) -> eyre::Result { message: String::from("Please populate error_codes.json"), source: String::from("None"), }], - }); + }) } else { let definitions: ErrorMessages = serde_json::from_slice(&def_file)?; Ok(definitions) diff --git a/src/main.rs b/src/main.rs index f1c9dda..039e5e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ fn process_file(file_path: &Path) -> eyre::Result { let file_string = std::fs::read_to_string(file_path)?; let config_file: Config = from_str(&file_string)?; - return Ok(config_file.definition); + Ok(config_file.definition) } _ => Err(eyre!( "Non RON file OR file without extension in config dir " @@ -128,13 +128,13 @@ fn build_object_lists(dir: PathBuf) -> eyre::Result { Definition::EndpointSchema(service_name, service_id, endpoint_schema) => { service_schema_map .entry((service_name, service_id)) - .or_insert(vec![]) + .or_default() .push(endpoint_schema) } Definition::EndpointSchemaList(service_name, service_id, endpoint_schemas) => { service_schema_map .entry((service_name, service_id)) - .or_insert(vec![]) + .or_default() .extend(endpoint_schemas) } Definition::Enum(enum_type) => enums.push(enum_type), @@ -191,10 +191,8 @@ fn read_version_file(path: &Path) -> eyre::Result { Ok(version_config) } -fn check_compatibility( - version_config: VersionConfig -) -> eyre::Result<()> { - let current_crate_version = Version::parse(&get_crate_version()).unwrap(); +fn check_compatibility(version_config: VersionConfig) -> eyre::Result<()> { + let current_crate_version = Version::parse(get_crate_version()).unwrap(); let binary_version_req = VersionReq::parse(&version_config.binary.version).unwrap(); @@ -206,8 +204,8 @@ fn check_compatibility( let caller_libs_version = Version::parse(&version_config.libs.version).unwrap(); if !binary_version_req.matches(¤t_crate_version) { - return Err(eyre!("Binary version constraint not satisfied. Version: {} is specified in version.toml. Current binary version is: {}", - &version_config.binary.version, &get_crate_version())); + Err(eyre!("Binary version constraint not satisfied. Version: {} is specified in version.toml. Current binary version is: {}", + &version_config.binary.version, &get_crate_version())) } else if !libs_version_req.matches(&caller_libs_version) { return Err(eyre!("endpoint-libs version constraint not satisfied. Version: {} is specified in version.toml. This version of endpoint-gen requires: {}", caller_libs_version, libs_version_requirement)); @@ -234,7 +232,7 @@ fn main() -> Result<()> { let config_dir = { if let Some(config_dir) = &args.config_dir { - PathBuf::from_str(&config_dir)? + PathBuf::from_str(config_dir)? } else { env::current_dir()? } diff --git a/src/rust.rs b/src/rust.rs index cc92186..e4ecd07 100644 --- a/src/rust.rs +++ b/src/rust.rs @@ -25,7 +25,7 @@ impl ToRust for Type { Type::Struct { name, .. } => name.clone(), Type::StructRef(name) => name.clone(), Type::Object => "serde_json::Value".to_owned(), - Type::DataTable { name, .. } => format!("Vec<{}>", name), + Type::DataTable { name, .. } => format!("Vec<{name}>"), Type::Vec(ele) => { format!("Vec<{}>", ele.to_rust_ref(serde_with)) } @@ -77,7 +77,7 @@ impl ToRust for Type { if serde_with_opt.is_empty() { "".to_string() } else { - format!("#[serde(with = \"{}\")]", serde_with_opt) + format!("#[serde(with = \"{serde_with_opt}\")]") }, x.name, x.ty.to_rust_ref(serde_with) @@ -375,6 +375,6 @@ pub fn dump_endpoint_schema(data: &Data, mut writer: impl Write) -> eyre::Result "#, cases = cases.join("\n") ); - writeln!(writer, "{}", code)?; + writeln!(writer, "{code}")?; Ok(()) } diff --git a/src/service.rs b/src/service.rs index 7b4ab29..8d151e4 100644 --- a/src/service.rs +++ b/src/service.rs @@ -1,3 +1,4 @@ +#[allow(clippy::all)] pub fn get_systemd_service(app_name: &str, service_name: &str, user: &str) -> String { format!( r#"[Unit] diff --git a/src/sql.rs b/src/sql.rs index 4fbb20f..beeaf1e 100644 --- a/src/sql.rs +++ b/src/sql.rs @@ -1,8 +1,5 @@ -use crate::Data; use endpoint_libs::model::{ProceduralFunction, Type}; use itertools::Itertools; -use std::fs::File; -use std::io::Write; pub const PARAM_PREFIX: &str = "a_"; @@ -24,7 +21,7 @@ impl ToSql for Type { .map(|x| format!("\"{}\" {}", x.name, x.ty.to_sql())); format!( "table (\n{}\n)", - fields.map(|x| format!(" {}", x)).join(",\n") + fields.map(|x| format!(" {x}")).join(",\n") ) } Type::StructRef(_name) => "jsonb".to_owned(), @@ -42,8 +39,8 @@ impl ToSql for Type { Type::Bytea => "bytea".to_owned(), Type::UUID => "uuid".to_owned(), Type::Inet => "inet".to_owned(), - Type::Enum { name, .. } => format!("enum_{}", name), - Type::EnumRef(name) => format!("enum_{}", name), + Type::Enum { name, .. } => format!("enum_{name}"), + Type::EnumRef(name) => format!("enum_{name}"), // 38 digits in total, with 4-18 decimal digits. So to be exact we need 38+18 digits Type::BlockchainDecimal => "decimal(56, 18)".to_owned(), Type::BlockchainAddress => "varchar".to_owned(),