@@ -22,112 +22,103 @@ mod build_spec_test;
22
22
// in use super::foundation::option_map
23
23
/// Set what level of cross-platform compatibility the built package
24
24
/// should have.
25
- #[ derive( Clone , Debug , Eq , Hash , Ord , PartialEq , PartialOrd , Deserialize , Serialize , Display ) ]
26
- pub enum HostOsCompatibility {
25
+ #[ derive(
26
+ Clone , Debug , Eq , Hash , Ord , PartialEq , PartialOrd , Deserialize , Serialize , Display , Default ,
27
+ ) ]
28
+ pub enum HostCompat {
27
29
/// Package can only be used on the same OS distribution
28
- #[ serde ( alias = "distro" ) ]
30
+ #[ default ]
29
31
Distro ,
30
32
/// Package can be used anywhere that has the same OS and cpu type
31
- #[ serde( alias = "arch" ) ]
32
33
Arch ,
33
34
/// Package can be used on the same OS with any cpu or distro
34
- #[ serde( alias = "os" , alias = "OS" ) ]
35
35
Os ,
36
36
/// Package can be used on any Os
37
- #[ serde( alias = "any" ) ]
38
37
Any ,
39
38
}
40
39
41
- impl Default for HostOsCompatibility {
42
- fn default ( ) -> Self {
43
- // TODO: possibly make the default configurable for sites, or
44
- // remove this and derive the default?
45
- Self :: Distro
46
- }
47
- }
40
+ // Each HostCompat value disallows certain var names when host_compat
41
+ // validation is enabled in the config file.
42
+ // TODO: move these to config
43
+ const DISTRO_DISALLOWS : & [ & OptName ] = & [ ] ;
44
+ const ARCH_DISALLOWS : & [ & OptName ] = & [ OptName :: distro ( ) ] ;
45
+ const OS_DISALLOWS : & [ & OptName ] = & [ OptName :: distro ( ) , OptName :: arch ( ) ] ;
46
+ const ANY_DISALLOWS : & [ & OptName ] = & [ OptName :: distro ( ) , OptName :: arch ( ) , OptName :: os ( ) ] ;
48
47
49
- impl HostOsCompatibility {
48
+ impl HostCompat {
50
49
pub fn is_default ( & self ) -> bool {
51
50
self == & Self :: default ( )
52
51
}
53
52
53
+ fn names_added ( & self ) -> HashSet < OptNameBuf > {
54
+ // TODO: move this to constants/config
55
+ let names = match self {
56
+ HostCompat :: Distro => vec ! [
57
+ OptName :: os( ) . to_owned( ) ,
58
+ OptName :: arch( ) . to_owned( ) ,
59
+ OptName :: distro( ) . to_owned( ) ,
60
+ ] ,
61
+ HostCompat :: Arch => vec ! [ OptName :: os( ) . to_owned( ) , OptName :: arch( ) . to_owned( ) ] ,
62
+ HostCompat :: Os => vec ! [ OptName :: os( ) . to_owned( ) ] ,
63
+ HostCompat :: Any => Vec :: new ( ) ,
64
+ } ;
65
+
66
+ names. into_iter ( ) . collect :: < HashSet < OptNameBuf > > ( )
67
+ }
68
+
54
69
/// Get host_options after filtering based on the cross Os
55
70
/// compatibility setting.
56
- pub fn host_options ( & self ) -> Result < OptionMap > {
71
+ pub fn host_options ( & self ) -> Result < Vec < Opt > > {
57
72
let all_host_options = spk_schema_foundation:: option_map:: host_options ( ) ?;
58
73
59
- // TODO: move these to constant/config
60
- let names_added = match self {
61
- HostOsCompatibility :: Distro => {
62
- let mut allows = vec ! [
63
- OptName :: os( ) . to_owned( ) ,
64
- OptName :: arch( ) . to_owned( ) ,
65
- OptName :: distro( ) . to_owned( ) ,
66
- ] ;
67
- // Also adds the <distroname> option
68
- match all_host_options. get ( OptName :: distro ( ) ) {
69
- Some ( distro_name) => match OptNameBuf :: try_from ( distro_name. clone ( ) ) {
70
- Ok ( name) => allows. push ( name. to_owned ( ) ) ,
71
- Err ( _err) => {
72
- return Err ( Error :: HostOptionNotValidDistroNameError (
73
- distro_name. to_string ( ) ,
74
- ) )
75
- }
76
- } ,
77
- None => return Err ( Error :: HostOptionNoDistroName ) ,
78
- } ;
79
- allows
80
- }
81
- HostOsCompatibility :: Arch => {
82
- vec ! [ OptName :: os( ) . to_owned( ) , OptName :: arch( ) . to_owned( ) ]
83
- }
84
- HostOsCompatibility :: Os => {
85
- vec ! [ OptName :: os( ) . to_owned( ) ]
74
+ let mut names_added = self . names_added ( ) ;
75
+ if HostCompat :: Distro == * self {
76
+ match all_host_options. get ( OptName :: distro ( ) ) {
77
+ Some ( distro_name) => match OptNameBuf :: try_from ( distro_name. clone ( ) ) {
78
+ Ok ( name) => _ = names_added. insert ( name) ,
79
+ Err ( err) => {
80
+ return Err ( Error :: HostOptionNotValidDistroNameError (
81
+ distro_name. to_string ( ) ,
82
+ err,
83
+ ) )
84
+ }
85
+ } ,
86
+ None => return Err ( Error :: HostOptionNoDistroName ) ,
86
87
}
87
- HostOsCompatibility :: Any => Vec :: new ( ) ,
88
- } ;
88
+ }
89
89
90
- let mut settings = OptionMap :: default ( ) ;
90
+ let mut settings = Vec :: new ( ) ;
91
91
for ( name, value) in all_host_options. iter ( ) {
92
92
if names_added. contains ( name) {
93
- settings. insert ( name. to_owned ( ) , value. clone ( ) ) ;
93
+ let mut opt = Opt :: Var ( VarOpt :: new ( name) ?) ;
94
+ opt. set_value ( value. to_string ( ) ) ?;
95
+ settings. push ( opt)
94
96
}
95
97
}
96
98
97
99
Ok ( settings)
98
100
}
99
101
100
- /// Check the given options are compatible with the
101
- /// HostOsCompatibility setting.
102
+ fn names_disallowed ( & self ) -> & [ & OptName ] {
103
+ match self {
104
+ HostCompat :: Distro => DISTRO_DISALLOWS ,
105
+ HostCompat :: Arch => ARCH_DISALLOWS ,
106
+ HostCompat :: Os => OS_DISALLOWS ,
107
+ HostCompat :: Any => ANY_DISALLOWS ,
108
+ }
109
+ }
110
+
111
+ /// Check the given options are compatible with the HostCompat
112
+ /// setting.
102
113
pub fn validate_host_opts ( & self , options : & [ Opt ] ) -> Result < ( ) > {
103
- let known = options
104
- . iter ( )
105
- . map ( Opt :: full_name)
106
- . map ( ToOwned :: to_owned)
107
- . collect :: < HashSet < _ > > ( ) ;
114
+ let known = options. iter ( ) . map ( Opt :: full_name) . collect :: < HashSet < _ > > ( ) ;
108
115
109
- // TODO: move these to constant/config setting
110
- let disallowed_names = match self {
111
- HostOsCompatibility :: Distro => Vec :: new ( ) ,
112
- HostOsCompatibility :: Arch => {
113
- vec ! [ OptName :: distro( ) . to_owned( ) ]
114
- }
115
- HostOsCompatibility :: Os => {
116
- vec ! [ OptName :: distro( ) . to_owned( ) , OptName :: arch( ) . to_owned( ) ]
117
- }
118
- HostOsCompatibility :: Any => {
119
- vec ! [
120
- OptName :: distro( ) . to_owned( ) ,
121
- OptName :: arch( ) . to_owned( ) ,
122
- OptName :: os( ) . to_owned( ) ,
123
- ]
124
- }
125
- } ;
116
+ let disallowed_names = self . names_disallowed ( ) ;
126
117
127
118
for name in disallowed_names {
128
119
// If a name that this setting would add is already in the
129
120
// given options then flag it as an error.
130
- if known. contains ( & name) {
121
+ if known. contains ( name) {
131
122
return Err ( Error :: HostOptionNotAllowedInVariantError (
132
123
name. to_string ( ) ,
133
124
self . to_string ( ) ,
@@ -150,7 +141,7 @@ pub struct BuildSpec {
150
141
#[ serde( default , skip_serializing_if = "ValidationSpec::is_default" ) ]
151
142
pub validation : ValidationSpec ,
152
143
#[ serde( default ) ]
153
- pub host_compatibility : HostOsCompatibility ,
144
+ pub host_compat : HostCompat ,
154
145
}
155
146
156
147
impl Default for BuildSpec {
@@ -160,7 +151,7 @@ impl Default for BuildSpec {
160
151
options : Vec :: new ( ) ,
161
152
variants : vec ! [ v0:: Variant :: default ( ) ] ,
162
153
validation : ValidationSpec :: default ( ) ,
163
- host_compatibility : HostOsCompatibility :: default ( ) ,
154
+ host_compat : HostCompat :: default ( ) ,
164
155
}
165
156
}
166
157
}
@@ -204,16 +195,16 @@ impl BuildSpec {
204
195
// controlled by the host compatibility setting.
205
196
let config = spk_config:: get_config ( ) ?;
206
197
if config. host_compat . validate {
207
- self . host_compatibility . validate_host_opts ( & opts) ?;
198
+ self . host_compat . validate_host_opts ( & opts) ?;
208
199
}
209
200
210
201
// Add any host options that are not already present.
211
- let host_opts = self . host_compatibility . host_options ( ) ?;
212
- for ( name , value ) in host_opts. iter ( ) {
213
- let mut opt = Opt :: Var ( VarOpt :: new ( name) ?) ;
214
- opt. set_value ( value. to_string ( ) ) ?;
202
+ let host_opts = self . host_compat . host_options ( ) ?;
203
+ for opt in host_opts. iter ( ) {
204
+ // let mut opt = Opt::Var(VarOpt::new(name)?);
205
+ // opt.set_value(value.to_string())?;
215
206
if known. insert ( opt. full_name ( ) . to_owned ( ) ) {
216
- opts. push ( opt) ;
207
+ opts. push ( opt. clone ( ) ) ;
217
208
}
218
209
}
219
210
@@ -343,10 +334,7 @@ impl<'de> Deserialize<'de> for UncheckedBuildSpec {
343
334
"validation" => {
344
335
unchecked. validation = map. next_value :: < ValidationSpec > ( ) ?
345
336
}
346
- "host_compat" | "host_compatibility" => {
347
- unchecked. host_compatibility =
348
- map. next_value :: < HostOsCompatibility > ( ) ?
349
- }
337
+ "host_compat" => unchecked. host_compat = map. next_value :: < HostCompat > ( ) ?,
350
338
_ => {
351
339
// for forwards compatibility we ignore any unrecognized
352
340
// field, but consume it just the same
0 commit comments