From b238d00c9ce1b72a7e2969b8f6d5ce147c271889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Mon, 9 Dec 2024 01:16:37 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Use=20`BTreeMap`=20ensures=20the?= =?UTF-8?q?=20result=20is=20sorted.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/macros/src/lib.rs | 4 ++-- .../macros/src/template/current_version_struct.rs | 4 ++-- packages/macros/src/template/impl_migration.rs | 10 +++++----- packages/macros/src/template/old_version_structs.rs | 12 ++++++------ 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/macros/src/lib.rs b/packages/macros/src/lib.rs index 4bfc1f3..df11843 100644 --- a/packages/macros/src/lib.rs +++ b/packages/macros/src/lib.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use proc_macro::TokenStream; use quote::quote; -use std::collections::HashMap; +use std::collections::BTreeMap; use syn::parse_macro_input; mod template; @@ -35,7 +35,7 @@ pub fn version(attr: TokenStream, input: TokenStream) -> TokenStream { .collect::>>() .expect("Failed to get field ident") .into_iter() - .collect::>(); + .collect::>(); let versions = { let mut temp_version = attr.version.clone(); let mut ret = vec![]; diff --git a/packages/macros/src/template/current_version_struct.rs b/packages/macros/src/template/current_version_struct.rs index 297648a..fb8354c 100644 --- a/packages/macros/src/template/current_version_struct.rs +++ b/packages/macros/src/template/current_version_struct.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::BTreeMap; use anyhow::Result; use proc_macro2::{Span, TokenStream}; @@ -19,7 +19,7 @@ pub(crate) fn generate_current_version_struct( input: Migration, ident: Ident, final_version: String, - final_struct_fields: HashMap, + final_struct_fields: BTreeMap, versions: Vec<(String, Vec, String)>, ) -> Result { let Migration { diff --git a/packages/macros/src/template/impl_migration.rs b/packages/macros/src/template/impl_migration.rs index fef61ea..e2ee023 100644 --- a/packages/macros/src/template/impl_migration.rs +++ b/packages/macros/src/template/impl_migration.rs @@ -1,7 +1,7 @@ use anyhow::{anyhow, Result}; use proc_macro2::TokenStream; use quote::quote; -use std::collections::HashMap; +use std::collections::BTreeMap; use syn::{Ident, Type}; use crate::{ @@ -12,9 +12,9 @@ use crate::{ use super::old_version_structs::infer_older_version_struct; fn generate_older_version_impl( - old_struct_fields: HashMap, + old_struct_fields: BTreeMap, convert_rules: Vec, -) -> Result> { +) -> Result> { let mut struct_fields = old_struct_fields .keys() .map(|key| { @@ -25,7 +25,7 @@ fn generate_older_version_impl( }, ) }) - .collect::>(); + .collect::>(); for rule in convert_rules.iter() { match rule { @@ -188,7 +188,7 @@ fn generate_older_version_impl( pub(crate) fn generate_impl_froms( ident: Ident, final_version: String, - final_struct_fields: HashMap, + final_struct_fields: BTreeMap, versions: Vec, ) -> Result { let mut temp_struct_fields = final_struct_fields.to_owned(); diff --git a/packages/macros/src/template/old_version_structs.rs b/packages/macros/src/template/old_version_structs.rs index ef6187b..67d0150 100644 --- a/packages/macros/src/template/old_version_structs.rs +++ b/packages/macros/src/template/old_version_structs.rs @@ -1,15 +1,15 @@ use anyhow::Result; use proc_macro2::TokenStream; use quote::quote; -use std::collections::HashMap; +use std::collections::BTreeMap; use syn::{Ident, Type}; use crate::{tools::MigrationField, utils::generate_ident}; pub(crate) fn infer_older_version_struct( - newer_struct_fields: HashMap, + newer_struct_fields: BTreeMap, convert_rules: Vec, -) -> Result> { +) -> Result> { let mut struct_fields = newer_struct_fields.clone(); for rule in convert_rules.iter() { @@ -46,9 +46,9 @@ pub(crate) fn infer_older_version_struct( pub(crate) fn generate_old_versions( final_version: String, - final_struct_fields: HashMap, + final_struct_fields: BTreeMap, versions: Vec<(String, Vec, String)>, -) -> Result)>> { +) -> Result)>> { let mut temp_struct_fields = final_struct_fields.to_owned(); let mut old_version_structs = vec![(final_version, temp_struct_fields.clone())]; @@ -64,7 +64,7 @@ pub(crate) fn generate_old_versions( pub(crate) fn generate_old_version_structs( ident: Ident, final_version: String, - final_struct_fields: HashMap, + final_struct_fields: BTreeMap, extra_macros: Vec, versions: Vec<(String, Vec, String)>, ) -> Result {