@@ -3,104 +3,119 @@ use serde::Deserialize;
3
3
4
4
use crate :: { Rect , Rgb } ;
5
5
6
- pub ( crate ) struct PhysicalLayoutKey {
7
- pub logical : ( u8 , u8 ) ,
8
- pub physical : Rect ,
9
- pub physical_name : String ,
10
- pub background_color : Rgb ,
6
+ pub ( crate ) struct PhysicalLayout {
7
+ pub meta : PhysicalLayoutMeta ,
8
+ pub keys : Vec < PhysicalLayoutKey > ,
11
9
}
12
10
13
- #[ derive( Debug , Deserialize ) ]
14
- pub struct PhysicalLayout ( pub Vec < PhysicalLayoutEntry > ) ;
15
-
16
11
impl PhysicalLayout {
17
- pub ( crate ) fn keys ( & self ) -> Vec < PhysicalLayoutKey > {
12
+ pub fn from_str ( physical_json : & str ) -> Self {
13
+ let json = serde_json:: from_str :: < PhysicalLayoutJson > ( physical_json) . unwrap ( ) ;
14
+
18
15
let mut keys = Vec :: new ( ) ;
19
16
20
17
let mut row_i = 0 ;
21
18
let mut col_i = 0 ;
22
19
let mut physical = Rect :: new ( 0.0 , 0.0 , 1.0 , 1.0 ) ;
23
20
let mut background_color = Rgb :: new ( 0xcc , 0xcc , 0xcc ) ;
21
+ let mut meta = None ;
24
22
25
- for entry in & self . 0 {
26
- if let PhysicalLayoutEntry :: Row ( row) = entry {
27
- for i in & row. 0 {
28
- match i {
29
- PhysicalKeyEnum :: Meta ( meta) => {
30
- debug ! ( "Key metadata {:?}" , meta) ;
31
- physical. x += meta. x ;
32
- physical. y -= meta. y ;
33
- physical. w = meta. w . unwrap_or ( physical. w ) ;
34
- physical. h = meta. h . unwrap_or ( physical. h ) ;
35
- background_color = meta
36
- . c
37
- . as_ref ( )
38
- . map ( |c| {
39
- let err = format ! ( "Failed to parse color {}" , c) ;
40
- Rgb :: parse ( & c[ 1 ..] ) . expect ( & err)
41
- } )
42
- . unwrap_or ( background_color) ;
43
- }
44
- PhysicalKeyEnum :: Name ( name) => {
45
- keys. push ( PhysicalLayoutKey {
46
- logical : ( row_i as u8 , col_i as u8 ) ,
47
- physical,
48
- physical_name : name. clone ( ) ,
49
- background_color,
50
- } ) ;
51
-
52
- physical. x += physical. w ;
53
-
54
- physical. w = 1.0 ;
55
- physical. h = 1.0 ;
56
-
57
- col_i += 1 ;
23
+ for entry in json. 0 {
24
+ match entry {
25
+ PhysicalLayoutEntry :: Meta ( data) => {
26
+ meta = Some ( data) ;
27
+ }
28
+ PhysicalLayoutEntry :: Row ( row) => {
29
+ for i in & row. 0 {
30
+ match i {
31
+ PhysicalKeyEnum :: Meta ( meta) => {
32
+ debug ! ( "Key metadata {:?}" , meta) ;
33
+ physical. x += meta. x ;
34
+ physical. y -= meta. y ;
35
+ physical. w = meta. w . unwrap_or ( physical. w ) ;
36
+ physical. h = meta. h . unwrap_or ( physical. h ) ;
37
+ background_color = meta
38
+ . c
39
+ . as_ref ( )
40
+ . map ( |c| {
41
+ let err = format ! ( "Failed to parse color {}" , c) ;
42
+ Rgb :: parse ( & c[ 1 ..] ) . expect ( & err)
43
+ } )
44
+ . unwrap_or ( background_color) ;
45
+ }
46
+ PhysicalKeyEnum :: Name ( name) => {
47
+ keys. push ( PhysicalLayoutKey {
48
+ logical : ( row_i as u8 , col_i as u8 ) ,
49
+ physical,
50
+ physical_name : name. clone ( ) ,
51
+ background_color,
52
+ } ) ;
53
+
54
+ physical. x += physical. w ;
55
+
56
+ physical. w = 1.0 ;
57
+ physical. h = 1.0 ;
58
+
59
+ col_i += 1 ;
60
+ }
58
61
}
59
62
}
60
- }
61
63
62
- physical. x = 0.0 ;
63
- physical. y -= 1.0 ;
64
+ physical. x = 0.0 ;
65
+ physical. y -= 1.0 ;
64
66
65
- col_i = 0 ;
66
- row_i += 1 ;
67
+ col_i = 0 ;
68
+ row_i += 1 ;
69
+ }
67
70
}
68
71
}
69
72
70
- keys
73
+ let meta = meta. expect ( "No layout meta" ) ;
74
+
75
+ Self { keys, meta }
71
76
}
72
77
}
73
78
79
+ pub ( crate ) struct PhysicalLayoutKey {
80
+ pub logical : ( u8 , u8 ) ,
81
+ pub physical : Rect ,
82
+ pub physical_name : String ,
83
+ pub background_color : Rgb ,
84
+ }
85
+
86
+ #[ derive( Debug , Deserialize ) ]
87
+ struct PhysicalLayoutJson ( Vec < PhysicalLayoutEntry > ) ;
88
+
74
89
#[ derive( Debug , Deserialize ) ]
75
90
#[ serde( untagged) ]
76
- pub enum PhysicalLayoutEntry {
91
+ enum PhysicalLayoutEntry {
77
92
Meta ( PhysicalLayoutMeta ) ,
78
93
Row ( PhysicalRow ) ,
79
94
}
80
95
81
96
#[ derive( Debug , Deserialize ) ]
82
- pub struct PhysicalLayoutMeta {
97
+ pub ( crate ) struct PhysicalLayoutMeta {
83
98
pub name : String ,
84
99
pub author : String ,
85
100
}
86
101
87
102
#[ derive( Debug , Deserialize ) ]
88
- pub struct PhysicalRow ( pub Vec < PhysicalKeyEnum > ) ;
103
+ struct PhysicalRow ( Vec < PhysicalKeyEnum > ) ;
89
104
90
105
#[ derive( Debug , Deserialize ) ]
91
106
#[ serde( untagged) ]
92
- pub enum PhysicalKeyEnum {
107
+ enum PhysicalKeyEnum {
93
108
Name ( String ) ,
94
109
Meta ( PhysicalKeyMeta ) ,
95
110
}
96
111
97
112
#[ derive( Debug , Deserialize ) ]
98
- pub struct PhysicalKeyMeta {
113
+ struct PhysicalKeyMeta {
99
114
#[ serde( default ) ]
100
- pub x : f64 ,
115
+ x : f64 ,
101
116
#[ serde( default ) ]
102
- pub y : f64 ,
103
- pub w : Option < f64 > ,
104
- pub h : Option < f64 > ,
105
- pub c : Option < String > ,
117
+ y : f64 ,
118
+ w : Option < f64 > ,
119
+ h : Option < f64 > ,
120
+ c : Option < String > ,
106
121
}
0 commit comments