Skip to content

Commit cf283ea

Browse files
authored
[3/n] rename Target to TargetMap, use in config (#71)
In the future, we're going to use these target maps in more spots in the config. Implement `Deserialize` on them.
1 parent d0ecc79 commit cf283ea

File tree

4 files changed

+40
-35
lines changed

4 files changed

+40
-35
lines changed

src/config/imp.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Configuration for a package.
66
77
use crate::package::{Package, PackageOutput, PackageSource};
8-
use crate::target::Target;
8+
use crate::target::TargetMap;
99
use serde_derive::Deserialize;
1010
use std::collections::BTreeMap;
1111
use std::path::Path;
@@ -110,7 +110,7 @@ pub struct Config {
110110

111111
impl Config {
112112
/// Returns target packages to be assembled on the builder machine.
113-
pub fn packages_to_build(&self, target: &Target) -> PackageMap<'_> {
113+
pub fn packages_to_build(&self, target: &TargetMap) -> PackageMap<'_> {
114114
PackageMap(
115115
self.packages
116116
.iter()
@@ -120,7 +120,7 @@ impl Config {
120120
}
121121

122122
/// Returns target packages which should execute on the deployment machine.
123-
pub fn packages_to_deploy(&self, target: &Target) -> PackageMap<'_> {
123+
pub fn packages_to_deploy(&self, target: &TargetMap) -> PackageMap<'_> {
124124
let all_packages = self.packages_to_build(target).0;
125125
PackageMap(
126126
all_packages
@@ -189,7 +189,7 @@ mod test {
189189
]),
190190
};
191191

192-
let mut order = cfg.packages_to_build(&Target::default()).build_order();
192+
let mut order = cfg.packages_to_build(&TargetMap::default()).build_order();
193193
// "pkg-a" comes first, because "pkg-b" depends on it.
194194
assert_eq!(order.next(), Some(vec![(&pkg_a_name, &pkg_a)]));
195195
assert_eq!(order.next(), Some(vec![(&pkg_b_name, &pkg_b)]));
@@ -230,7 +230,7 @@ mod test {
230230
]),
231231
};
232232

233-
let mut order = cfg.packages_to_build(&Target::default()).build_order();
233+
let mut order = cfg.packages_to_build(&TargetMap::default()).build_order();
234234
order.next();
235235
}
236236

@@ -255,7 +255,7 @@ mod test {
255255
packages: BTreeMap::from([(pkg_a_name.clone(), pkg_a.clone())]),
256256
};
257257

258-
let mut order = cfg.packages_to_build(&Target::default()).build_order();
258+
let mut order = cfg.packages_to_build(&TargetMap::default()).build_order();
259259
order.next();
260260
}
261261
}

src/package.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::cache::{Cache, CacheError};
1313
use crate::config::{PackageName, ServiceName};
1414
use crate::input::{BuildInput, BuildInputs, MappedPath, TargetDirectory, TargetPackage};
1515
use crate::progress::{NoProgress, Progress};
16-
use crate::target::Target;
16+
use crate::target::TargetMap;
1717
use crate::timer::BuildTimer;
1818

1919
use anyhow::{anyhow, bail, Context, Result};
@@ -183,7 +183,7 @@ pub struct Package {
183183
/// Identifies the targets for which the package should be included.
184184
///
185185
/// If ommitted, the package is assumed to be included for all targets.
186-
pub only_for_targets: Option<BTreeMap<String, String>>,
186+
pub only_for_targets: Option<TargetMap>,
187187

188188
/// A human-readable string with suggestions for setup if packaging fails.
189189
#[serde(default)]
@@ -204,7 +204,7 @@ async fn new_zone_archive_builder(
204204
/// Configuration that can modify how a package is built.
205205
pub struct BuildConfig<'a> {
206206
/// Describes the [Target] to build the package for.
207-
pub target: &'a Target,
207+
pub target: &'a TargetMap,
208208

209209
/// Describes how progress will be communicated back to the caller.
210210
pub progress: &'a dyn Progress,
@@ -213,7 +213,7 @@ pub struct BuildConfig<'a> {
213213
pub cache_disabled: bool,
214214
}
215215

216-
static DEFAULT_TARGET: Target = Target(BTreeMap::new());
216+
static DEFAULT_TARGET: TargetMap = TargetMap(BTreeMap::new());
217217
static DEFAULT_PROGRESS: NoProgress = NoProgress::new();
218218

219219
impl Default for BuildConfig<'_> {
@@ -266,7 +266,7 @@ impl Package {
266266
#[deprecated = "Use 'Package::create', which now takes a 'BuildConfig', and implements 'Default'"]
267267
pub async fn create_for_target(
268268
&self,
269-
target: &Target,
269+
target: &TargetMap,
270270
name: &PackageName,
271271
output_directory: &Utf8Path,
272272
) -> Result<File> {
@@ -362,7 +362,7 @@ impl Package {
362362
pub async fn create_with_progress_for_target(
363363
&self,
364364
progress: &impl Progress,
365-
target: &Target,
365+
target: &TargetMap,
366366
name: &PackageName,
367367
output_directory: &Utf8Path,
368368
) -> Result<File> {
@@ -444,7 +444,7 @@ impl Package {
444444

445445
fn get_paths_inputs(
446446
&self,
447-
target: &Target,
447+
target: &TargetMap,
448448
paths: &Vec<InterpolatedMappedPath>,
449449
) -> Result<BuildInputs> {
450450
let mut inputs = BuildInputs::new();
@@ -532,7 +532,7 @@ impl Package {
532532
fn get_all_inputs(
533533
&self,
534534
package_name: &PackageName,
535-
target: &Target,
535+
target: &TargetMap,
536536
output_directory: &Utf8Path,
537537
zoned: bool,
538538
version: Option<&semver::Version>,
@@ -870,7 +870,7 @@ pub struct InterpolatedString(String);
870870
impl InterpolatedString {
871871
// Interpret the string for the specified target.
872872
// Substitutes key/value pairs as necessary.
873-
pub fn interpolate(&self, target: &Target) -> Result<String> {
873+
pub fn interpolate(&self, target: &TargetMap) -> Result<String> {
874874
let mut input = self.0.as_str();
875875
let mut output = String::new();
876876

@@ -912,7 +912,7 @@ pub struct InterpolatedMappedPath {
912912
}
913913

914914
impl InterpolatedMappedPath {
915-
fn interpolate(&self, target: &Target) -> Result<MappedPath> {
915+
fn interpolate(&self, target: &TargetMap) -> Result<MappedPath> {
916916
Ok(MappedPath {
917917
from: Utf8PathBuf::from(self.from.interpolate(target)?),
918918
to: Utf8PathBuf::from(self.to.interpolate(target)?),
@@ -926,7 +926,7 @@ mod test {
926926

927927
#[test]
928928
fn interpolate_noop() {
929-
let target = Target(BTreeMap::new());
929+
let target = TargetMap(BTreeMap::new());
930930
let is = InterpolatedString(String::from("nothing to change"));
931931

932932
let s = is.interpolate(&target).unwrap();
@@ -935,7 +935,7 @@ mod test {
935935

936936
#[test]
937937
fn interpolate_single() {
938-
let mut target = Target(BTreeMap::new());
938+
let mut target = TargetMap(BTreeMap::new());
939939
target.0.insert("key1".to_string(), "value1".to_string());
940940
let is = InterpolatedString(String::from("{{key1}}"));
941941

@@ -945,7 +945,7 @@ mod test {
945945

946946
#[test]
947947
fn interpolate_single_with_prefix() {
948-
let mut target = Target(BTreeMap::new());
948+
let mut target = TargetMap(BTreeMap::new());
949949
target.0.insert("key1".to_string(), "value1".to_string());
950950
let is = InterpolatedString(String::from("prefix-{{key1}}"));
951951

@@ -955,7 +955,7 @@ mod test {
955955

956956
#[test]
957957
fn interpolate_single_with_suffix() {
958-
let mut target = Target(BTreeMap::new());
958+
let mut target = TargetMap(BTreeMap::new());
959959
target.0.insert("key1".to_string(), "value1".to_string());
960960
let is = InterpolatedString(String::from("{{key1}}-suffix"));
961961

@@ -965,7 +965,7 @@ mod test {
965965

966966
#[test]
967967
fn interpolate_multiple() {
968-
let mut target = Target(BTreeMap::new());
968+
let mut target = TargetMap(BTreeMap::new());
969969
target.0.insert("key1".to_string(), "value1".to_string());
970970
target.0.insert("key2".to_string(), "value2".to_string());
971971
let is = InterpolatedString(String::from("{{key1}}-{{key2}}"));
@@ -976,7 +976,7 @@ mod test {
976976

977977
#[test]
978978
fn interpolate_missing_key() {
979-
let mut target = Target(BTreeMap::new());
979+
let mut target = TargetMap(BTreeMap::new());
980980
target.0.insert("key1".to_string(), "value1".to_string());
981981
let is = InterpolatedString(String::from("{{key3}}"));
982982

@@ -991,7 +991,7 @@ mod test {
991991

992992
#[test]
993993
fn interpolate_missing_closing() {
994-
let mut target = Target(BTreeMap::new());
994+
let mut target = TargetMap(BTreeMap::new());
995995
target.0.insert("key1".to_string(), "value1".to_string());
996996
let is = InterpolatedString(String::from("{{key1"));
997997

@@ -1011,7 +1011,7 @@ mod test {
10111011
// as part of they key -- INCLUDING other "{{" characters.
10121012
#[test]
10131013
fn interpolate_key_as_literal() {
1014-
let mut target = Target(BTreeMap::new());
1014+
let mut target = TargetMap(BTreeMap::new());
10151015
target.0.insert("oh{{no".to_string(), "value".to_string());
10161016
let is = InterpolatedString(String::from("{{oh{{no}}"));
10171017

src/target.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

55
use crate::package::Package;
6+
use serde::Deserialize;
67
use std::collections::BTreeMap;
78

8-
/// A target describes what platform and configuration we're trying
9-
/// to deploy on.
10-
#[derive(Clone, Debug, Default)]
11-
pub struct Target(pub BTreeMap<String, String>);
9+
/// Describes what platform and configuration we're trying to deploy on.
10+
///
11+
/// For flexibility, this is an arbitrary key-value map without any attached
12+
/// semantics to particular keys. Those semantics are provided by the consumers
13+
/// of this tooling within omicron.
14+
#[derive(Clone, Debug, Default, PartialEq, Deserialize)]
15+
#[serde(transparent)]
16+
pub struct TargetMap(pub BTreeMap<String, String>);
1217

13-
impl Target {
18+
impl TargetMap {
1419
// Returns true if this target should include the package.
1520
pub(crate) fn includes_package(&self, pkg: &Package) -> bool {
1621
let valid_targets = if let Some(targets) = &pkg.only_for_targets {
@@ -24,7 +29,7 @@ impl Target {
2429

2530
// For each of the targets permitted by the package, check if
2631
// the current target matches.
27-
for (k, v) in valid_targets {
32+
for (k, v) in &valid_targets.0 {
2833
let target_value = if let Some(target_value) = self.0.get(k) {
2934
target_value
3035
} else {
@@ -39,7 +44,7 @@ impl Target {
3944
}
4045
}
4146

42-
impl std::fmt::Display for Target {
47+
impl std::fmt::Display for TargetMap {
4348
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
4449
for (key, value) in &self.0 {
4550
write!(f, "{}={} ", key, value)?;
@@ -54,7 +59,7 @@ pub enum TargetParseError {
5459
MissingEquals(String),
5560
}
5661

57-
impl std::str::FromStr for Target {
62+
impl std::str::FromStr for TargetMap {
5863
type Err = TargetParseError;
5964

6065
fn from_str(s: &str) -> Result<Self, Self::Err> {
@@ -66,6 +71,6 @@ impl std::str::FromStr for Target {
6671
.map(|(k, v)| (k.to_string(), v.to_string()))
6772
})
6873
.collect::<Result<BTreeMap<String, String>, _>>()?;
69-
Ok(Target(kvs))
74+
Ok(TargetMap(kvs))
7075
}
7176
}

tests/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod test {
1515
use omicron_zone_package::config::{self, PackageName, ServiceName};
1616
use omicron_zone_package::package::BuildConfig;
1717
use omicron_zone_package::progress::NoProgress;
18-
use omicron_zone_package::target::Target;
18+
use omicron_zone_package::target::TargetMap;
1919

2020
const MY_PACKAGE: PackageName = PackageName::new_const("my-package");
2121

@@ -224,7 +224,7 @@ mod test {
224224
let out = camino_tempfile::tempdir().unwrap();
225225

226226
// Ask for the order of packages to-be-built
227-
let packages = cfg.packages_to_build(&Target::default());
227+
let packages = cfg.packages_to_build(&TargetMap::default());
228228
let mut build_order = packages.build_order();
229229

230230
// Build the dependencies first.

0 commit comments

Comments
 (0)