Skip to content

Commit

Permalink
🐛 Use BTreeMap ensures the result is sorted.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Dec 8, 2024
1 parent 4f8d66d commit b238d00
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
4 changes: 2 additions & 2 deletions packages/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn version(attr: TokenStream, input: TokenStream) -> TokenStream {
.collect::<Result<Vec<_>>>()
.expect("Failed to get field ident")
.into_iter()
.collect::<HashMap<_, _>>();
.collect::<BTreeMap<_, _>>();
let versions = {
let mut temp_version = attr.version.clone();
let mut ret = vec![];
Expand Down
4 changes: 2 additions & 2 deletions packages/macros/src/template/current_version_struct.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::BTreeMap;

use anyhow::Result;
use proc_macro2::{Span, TokenStream};
Expand All @@ -19,7 +19,7 @@ pub(crate) fn generate_current_version_struct(
input: Migration,
ident: Ident,
final_version: String,
final_struct_fields: HashMap<Ident, Type>,
final_struct_fields: BTreeMap<Ident, Type>,
versions: Vec<(String, Vec<MigrationField>, String)>,
) -> Result<TokenStream> {
let Migration {
Expand Down
10 changes: 5 additions & 5 deletions packages/macros/src/template/impl_migration.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -12,9 +12,9 @@ use crate::{
use super::old_version_structs::infer_older_version_struct;

fn generate_older_version_impl(
old_struct_fields: HashMap<Ident, Type>,
old_struct_fields: BTreeMap<Ident, Type>,
convert_rules: Vec<MigrationField>,
) -> Result<HashMap<Ident, TokenStream>> {
) -> Result<BTreeMap<Ident, TokenStream>> {
let mut struct_fields = old_struct_fields
.keys()
.map(|key| {
Expand All @@ -25,7 +25,7 @@ fn generate_older_version_impl(
},
)
})
.collect::<HashMap<_, _>>();
.collect::<BTreeMap<_, _>>();

for rule in convert_rules.iter() {
match rule {
Expand Down Expand Up @@ -188,7 +188,7 @@ fn generate_older_version_impl(
pub(crate) fn generate_impl_froms(
ident: Ident,
final_version: String,
final_struct_fields: HashMap<Ident, Type>,
final_struct_fields: BTreeMap<Ident, Type>,
versions: Vec<MigrationComment>,
) -> Result<TokenStream> {
let mut temp_struct_fields = final_struct_fields.to_owned();
Expand Down
12 changes: 6 additions & 6 deletions packages/macros/src/template/old_version_structs.rs
Original file line number Diff line number Diff line change
@@ -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<Ident, Type>,
newer_struct_fields: BTreeMap<Ident, Type>,
convert_rules: Vec<MigrationField>,
) -> Result<HashMap<Ident, Type>> {
) -> Result<BTreeMap<Ident, Type>> {
let mut struct_fields = newer_struct_fields.clone();

for rule in convert_rules.iter() {
Expand Down Expand Up @@ -46,9 +46,9 @@ pub(crate) fn infer_older_version_struct(

pub(crate) fn generate_old_versions(
final_version: String,
final_struct_fields: HashMap<Ident, Type>,
final_struct_fields: BTreeMap<Ident, Type>,
versions: Vec<(String, Vec<MigrationField>, String)>,
) -> Result<Vec<(String, HashMap<Ident, Type>)>> {
) -> Result<Vec<(String, BTreeMap<Ident, Type>)>> {
let mut temp_struct_fields = final_struct_fields.to_owned();
let mut old_version_structs = vec![(final_version, temp_struct_fields.clone())];

Expand All @@ -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<Ident, Type>,
final_struct_fields: BTreeMap<Ident, Type>,
extra_macros: Vec<TokenStream>,
versions: Vec<(String, Vec<MigrationField>, String)>,
) -> Result<TokenStream> {
Expand Down

0 comments on commit b238d00

Please sign in to comment.