11use crate :: ascii_art:: * ;
22
3- #[ derive( Debug ) ]
3+ /// Represents a GPU vendor.
4+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
45pub enum Vendor {
56 AMD = 0x1002 ,
67 ImgTec = 0x1010 ,
@@ -24,6 +25,7 @@ pub enum Vendor {
2425const BLOCK : & str = "\x1B [7m \x1B [0m" ;
2526
2627impl Vendor {
28+ /// Returns the vendor-specific ASCII art with color styling applied.
2729 pub fn get_ascii_art ( & self ) -> Vec < String > {
2830 let art: & [ & str ] = match self {
2931 Vendor :: AMD => AMD ,
@@ -34,8 +36,8 @@ impl Vendor {
3436 Vendor :: Nvidia => NVIDIA ,
3537 Vendor :: Microsoft => MICROSOFT ,
3638 Vendor :: Qualcomm => QUALCOMM ,
37- Vendor :: Mesa // => MESA ,
38- | Vendor :: Unknown
39+ Vendor :: Mesa => VULKAN ,
40+ Vendor :: Unknown
3941 | Vendor :: ImgTec
4042 | Vendor :: VIV
4143 | Vendor :: VSI
@@ -49,18 +51,19 @@ impl Vendor {
4951
5052 let mut modified_art: Vec < String > = art. iter ( ) . map ( |& line| line. to_string ( ) ) . collect ( ) ;
5153
52- for ( char , style) in CHARS . iter ( ) . zip ( styles. iter ( ) ) {
54+ for ( symbol , style) in CHARS . iter ( ) . zip ( styles. iter ( ) ) {
5355 if !style. is_empty ( ) {
5456 modified_art = modified_art
5557 . iter ( )
56- . map ( |line| line. replace ( * char , & format ! ( "{}{}" , style, BLOCK ) ) )
58+ . map ( |line| line. replace ( * symbol , & format ! ( "{}{}" , style, BLOCK ) ) )
5759 . collect ( ) ;
5860 }
5961 }
6062
6163 modified_art
6264 }
6365
66+ /// Returns an array of style strings associated with the vendor.
6467 pub const fn get_styles ( & self ) -> [ & str ; LUT_SIZE ] {
6568 match self {
6669 Vendor :: AMD => AMD_STYLE ,
@@ -71,8 +74,8 @@ impl Vendor {
7174 Vendor :: Nvidia => NVIDIA_STYLE ,
7275 Vendor :: Microsoft => MICROSOFT_STYLE ,
7376 Vendor :: Qualcomm => QUALCOMM_STYLE ,
74- Vendor :: Mesa // => MESA_STYLE ,
75- | Vendor :: Unknown
77+ Vendor :: Mesa => VULKAN_STYLE ,
78+ Vendor :: Unknown
7679 | Vendor :: ImgTec
7780 | Vendor :: VIV
7881 | Vendor :: VSI
@@ -83,6 +86,7 @@ impl Vendor {
8386 }
8487 }
8588
89+ /// Returns a human-readable name for the vendor.
8690 pub fn name ( & self ) -> & ' static str {
8791 match self {
8892 Vendor :: AMD => "AMD" ,
@@ -105,6 +109,7 @@ impl Vendor {
105109 }
106110 }
107111
112+ /// Constructs a Vendor from a vendor ID, if recognized.
108113 pub fn from_vendor_id ( id : u32 ) -> Option < Self > {
109114 match id {
110115 0x1002 => Some ( Vendor :: AMD ) ,
@@ -134,3 +139,59 @@ impl From<Vendor> for u32 {
134139 v as u32
135140 }
136141}
142+
143+ /// Allows a vendor to be printed using its human‑readable name.
144+ impl std:: fmt:: Display for Vendor {
145+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
146+ write ! ( f, "{}" , self . name( ) )
147+ }
148+ }
149+
150+ #[ cfg( test) ]
151+ mod tests {
152+ use super :: * ;
153+
154+ #[ test]
155+ fn test_from_vendor_id ( ) {
156+ assert_eq ! ( Vendor :: from_vendor_id( 0x1002 ) , Some ( Vendor :: AMD ) ) ;
157+ assert_eq ! ( Vendor :: from_vendor_id( 0x1010 ) , Some ( Vendor :: ImgTec ) ) ;
158+ assert_eq ! ( Vendor :: from_vendor_id( 0x10DE ) , Some ( Vendor :: Nvidia ) ) ;
159+ assert_eq ! ( Vendor :: from_vendor_id( 0x9999 ) , None ) ;
160+ }
161+
162+ #[ test]
163+ fn test_name ( ) {
164+ assert_eq ! ( Vendor :: AMD . name( ) , "AMD" ) ;
165+ assert_eq ! ( Vendor :: Apple . name( ) , "Apple" ) ;
166+ assert_eq ! ( Vendor :: Intel . name( ) , "Intel" ) ;
167+ assert_eq ! ( Vendor :: Unknown . name( ) , "Unknown" ) ;
168+ }
169+
170+ #[ test]
171+ fn test_display ( ) {
172+ let vendor = Vendor :: Nvidia ;
173+ let s = format ! ( "{}" , vendor) ;
174+ assert_eq ! ( s, vendor. name( ) ) ;
175+ }
176+
177+ #[ test]
178+ fn test_get_styles_length ( ) {
179+ let vendor = Vendor :: AMD ;
180+ let styles = vendor. get_styles ( ) ;
181+ assert_eq ! ( styles. len( ) , crate :: ascii_art:: LUT_SIZE ) ;
182+ }
183+
184+ #[ test]
185+ fn test_get_ascii_art ( ) {
186+ let vendor = Vendor :: AMD ;
187+ let art = vendor. get_ascii_art ( ) ;
188+ assert ! ( !art. is_empty( ) , "ASCII art should not be empty" ) ;
189+
190+ let styles = vendor. get_styles ( ) ;
191+ let non_empty_style = styles. iter ( ) . any ( |s| !s. is_empty ( ) ) ;
192+ if non_empty_style {
193+ let block_found = art. iter ( ) . any ( |line| line. contains ( BLOCK ) ) ;
194+ assert ! ( block_found, "Styled block should appear in ASCII art lines" ) ;
195+ }
196+ }
197+ }
0 commit comments