@@ -13,6 +13,7 @@ use indexmap::IndexMap;
1313use itertools:: Itertools ;
1414use serde:: de:: { Deserialize , DeserializeOwned , Deserializer } ;
1515use std:: collections:: HashMap ;
16+ use std:: fmt:: { self , Write } ;
1617use std:: fs;
1718use std:: hash:: Hash ;
1819use std:: path:: Path ;
@@ -158,25 +159,37 @@ where
158159/// If the key already exists, it returns an error with a message indicating the key's existence.
159160pub fn try_insert < K , V > ( map : & mut HashMap < K , V > , key : & K , value : V ) -> Result < ( ) >
160161where
161- K : Eq + Hash + Clone + std :: fmt:: Debug ,
162+ K : Eq + Hash + Clone + fmt:: Debug ,
162163{
163164 let existing = map. insert ( key. clone ( ) , value) . is_some ( ) ;
164165 ensure ! ( !existing, "Key {key:?} already exists in the map" ) ;
165166 Ok ( ( ) )
166167}
167168
168169/// Format a list of items with a cap on display count for error messages
169- pub fn format_items_with_cap < T : std:: fmt:: Debug > ( items : & [ T ] ) -> String {
170+ pub fn format_items_with_cap < I , J , T > ( items : I ) -> String
171+ where
172+ I : IntoIterator < Item = T , IntoIter = J > ,
173+ J : ExactSizeIterator < Item = T > ,
174+ T : fmt:: Debug ,
175+ {
170176 const MAX_DISPLAY : usize = 10 ;
171- if items. len ( ) <= MAX_DISPLAY {
172- format ! ( "{items:?}" )
173- } else {
174- format ! (
175- "{:?} and {} more" ,
176- & items[ ..MAX_DISPLAY ] ,
177- items. len( ) - MAX_DISPLAY
178- )
177+
178+ let items = items. into_iter ( ) ;
179+ let total_count = items. len ( ) ;
180+
181+ // Format items with fmt::Debug::fmt() and separate with commas
182+ let formatted_items = items
183+ . take ( MAX_DISPLAY )
184+ . format_with ( ", " , |items, f| f ( & format_args ! ( "{items:?}" ) ) ) ;
185+ let mut out = format ! ( "[{formatted_items}]" ) ;
186+
187+ // If there are remaining items, include the count
188+ if total_count > MAX_DISPLAY {
189+ write ! ( & mut out, " and {} more" , total_count - MAX_DISPLAY ) . unwrap ( ) ;
179190 }
191+
192+ out
180193}
181194
182195/// Read a model from the specified directory.
0 commit comments