Skip to main content

openjd_model/
capabilities.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5//! Standard capability definitions for the Open Job Description specification.
6//!
7//! Capability lookup functions take `(SpecificationRevision, &Extensions)` and
8//! return `Result` because the set of standard capabilities may vary by revision,
9//! and future extensions may add or modify capabilities that are only valid for
10//! certain revision+extension combinations.
11
12use crate::error::ModelError;
13use crate::types::{Extensions, SpecificationRevision};
14
15/// Standard amount capabilities and their names.
16const STANDARD_AMOUNT_CAPABILITIES_V2023_09: &[&str] = &[
17    "amount.worker.vcpu",
18    "amount.worker.memory",
19    "amount.worker.gpu",
20    "amount.worker.gpu.memory",
21    "amount.worker.disk.scratch",
22];
23
24/// Standard attribute capabilities, their names, and allowed values.
25const STANDARD_ATTRIBUTE_CAPABILITIES_V2023_09: &[(&str, &[&str])] = &[
26    ("attr.worker.os.family", &["linux", "windows", "macos"]),
27    ("attr.worker.cpu.arch", &["x86_64", "arm64"]),
28];
29
30fn unsupported_revision(revision: SpecificationRevision) -> ModelError {
31    ModelError::DecodeValidation(format!("Unsupported specification revision: {revision}"))
32}
33
34/// Return the standard amount capability names for the given revision and extensions.
35pub fn standard_amount_capability_names(
36    revision: SpecificationRevision,
37    _extensions: &Extensions,
38) -> Result<&'static [&'static str], ModelError> {
39    #[allow(unreachable_patterns)] // wildcard needed for forward compat with new revisions
40    match revision {
41        SpecificationRevision::V2023_09 => Ok(STANDARD_AMOUNT_CAPABILITIES_V2023_09),
42        _ => Err(unsupported_revision(revision)),
43    }
44}
45
46/// Return the standard attribute capability names for the given revision and extensions.
47pub fn standard_attribute_capability_names(
48    revision: SpecificationRevision,
49    _extensions: &Extensions,
50) -> Result<Vec<&'static str>, ModelError> {
51    #[allow(unreachable_patterns)]
52    match revision {
53        SpecificationRevision::V2023_09 => Ok(STANDARD_ATTRIBUTE_CAPABILITIES_V2023_09
54            .iter()
55            .map(|(name, _)| *name)
56            .collect()),
57        _ => Err(unsupported_revision(revision)),
58    }
59}
60
61/// Return the standard attribute capabilities (name + allowed values) for the given
62/// revision and extensions.
63pub fn standard_attribute_capabilities(
64    revision: SpecificationRevision,
65    _extensions: &Extensions,
66) -> Result<&'static [(&'static str, &'static [&'static str])], ModelError> {
67    #[allow(unreachable_patterns)]
68    match revision {
69        SpecificationRevision::V2023_09 => Ok(STANDARD_ATTRIBUTE_CAPABILITIES_V2023_09),
70        _ => Err(unsupported_revision(revision)),
71    }
72}
73
74/// Validate that a string is a valid amount capability name.
75pub fn validate_amount_capability_name(name: &str) -> Result<(), String> {
76    let re = &crate::template::validate_v2023_09::helpers::AMOUNT_CAP_RE;
77    if re.is_match(name) {
78        Ok(())
79    } else {
80        Err(format!("'{name}' is not a valid amount capability name"))
81    }
82}
83
84/// Validate that a string is a valid attribute capability name.
85pub fn validate_attribute_capability_name(name: &str) -> Result<(), String> {
86    let re = &crate::template::validate_v2023_09::helpers::ATTR_CAP_RE;
87    if re.is_match(name) {
88        Ok(())
89    } else {
90        Err(format!("'{name}' is not a valid attribute capability name"))
91    }
92}