File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed
Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -144,6 +144,31 @@ macro_rules! formats {
144144 ) *
145145 }
146146 }
147+
148+ /// Converts the full name of a file format into the enum representation
149+ ///
150+ /// # Examples
151+ ///
152+ /// Basic usage:
153+ ///
154+ /// ```
155+ /// use file_format::{FileFormat, Kind};
156+ ///
157+ /// let fmt = FileFormat::Mpeg12AudioLayer3;
158+ /// let name = fmt.name();
159+ /// assert_eq!(name, "MPEG-1/2 Audio Layer 3");
160+ /// assert_eq!(FileFormat::from_name(name), Some(fmt));
161+ /// ```
162+ pub fn from_name( name: & str ) -> Option <crate :: FileFormat > {
163+ static NAME : std:: sync:: OnceLock <std:: collections:: HashMap <& ' static str , crate :: FileFormat >> = std:: sync:: OnceLock :: new( ) ;
164+ NAME . get_or_init( || -> std:: collections:: HashMap <& ' static str , crate :: FileFormat > {
165+ let mut map = std:: collections:: HashMap :: new( ) ;
166+ $(
167+ map. insert( $name, Self :: $format) ;
168+ ) *
169+ map
170+ } ) . get( name) . cloned( )
171+ }
147172 }
148173 } ;
149174}
You can’t perform that action at this time.
0 commit comments