| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright © 2022-2024 Intel Corporation |
| 3 |
|
| 4 |
"""Type definitions for cargo manifest files.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
import typing as T |
| 8 |
|
| 9 |
from typing_extensions import Literal, TypedDict, Required |
| 10 |
|
| 11 |
EDITION = Literal['2015', '2018', '2021'] |
| 12 |
CRATE_TYPE = Literal['bin', 'lib', 'dylib', 'staticlib', 'cdylib', 'rlib', 'proc-macro'] |
| 13 |
LINT_LEVEL = Literal['allow', 'deny', 'forbid', 'warn'] |
| 14 |
|
| 15 |
|
| 16 |
class FromWorkspace(TypedDict): |
| 17 |
|
| 18 |
"""An entry or section that is copied from the workspace.""" |
| 19 |
|
| 20 |
workspace: bool |
| 21 |
|
| 22 |
|
| 23 |
Package = TypedDict( |
| 24 |
'Package', |
| 25 |
{ |
| 26 |
'name': Required[str], |
| 27 |
'version': Required[T.Union[FromWorkspace, str]], |
| 28 |
'authors': T.Union[FromWorkspace, T.List[str]], |
| 29 |
'edition': T.Union[FromWorkspace, EDITION], |
| 30 |
'rust-version': T.Union[FromWorkspace, str], |
| 31 |
'description': T.Union[FromWorkspace, str], |
| 32 |
'readme': T.Union[FromWorkspace, str], |
| 33 |
'license': T.Union[FromWorkspace, str], |
| 34 |
'license-file': T.Union[FromWorkspace, str], |
| 35 |
'keywords': T.Union[FromWorkspace, T.List[str]], |
| 36 |
'categories': T.Union[FromWorkspace, T.List[str]], |
| 37 |
'homepage': T.Union[FromWorkspace, str], |
| 38 |
'repository': T.Union[FromWorkspace, str], |
| 39 |
'documentation': T.Union[FromWorkspace, str], |
| 40 |
'workspace': str, |
| 41 |
'build': str, |
| 42 |
'links': str, |
| 43 |
'include': T.Union[FromWorkspace, T.List[str]], |
| 44 |
'exclude': T.Union[FromWorkspace, T.List[str]], |
| 45 |
'publish': T.Union[FromWorkspace, bool], |
| 46 |
'metadata': T.Dict[str, T.Dict[str, str]], |
| 47 |
'default-run': str, |
| 48 |
'autolib': bool, |
| 49 |
'autobins': bool, |
| 50 |
'autoexamples': bool, |
| 51 |
'autotests': bool, |
| 52 |
'autobenches': bool, |
| 53 |
}, |
| 54 |
total=False, |
| 55 |
) |
| 56 |
"""A description of the Package Dictionary.""" |
| 57 |
|
| 58 |
class Badge(TypedDict): |
| 59 |
|
| 60 |
"""An entry in the badge section.""" |
| 61 |
|
| 62 |
status: Literal['actively-developed', 'passively-developed', 'as-is', 'experimental', 'deprecated', 'none'] |
| 63 |
repository: str |
| 64 |
|
| 65 |
|
| 66 |
Dependency = TypedDict( |
| 67 |
'Dependency', |
| 68 |
{ |
| 69 |
'version': str, |
| 70 |
'registry': str, |
| 71 |
'git': str, |
| 72 |
'branch': str, |
| 73 |
'rev': str, |
| 74 |
'path': str, |
| 75 |
'optional': bool, |
| 76 |
'package': str, |
| 77 |
'default-features': bool, |
| 78 |
'features': T.List[str], |
| 79 |
}, |
| 80 |
total=False, |
| 81 |
) |
| 82 |
"""An entry in the *dependencies sections.""" |
| 83 |
|
| 84 |
|
| 85 |
DependencyV = T.Union[Dependency, str] |
| 86 |
"""A Dependency entry, either a string or a Dependency Dict.""" |
| 87 |
|
| 88 |
|
| 89 |
_BaseBuildTarget = TypedDict( |
| 90 |
'_BaseBuildTarget', |
| 91 |
{ |
| 92 |
'path': str, |
| 93 |
'test': bool, |
| 94 |
'doctest': bool, |
| 95 |
'bench': bool, |
| 96 |
'doc': bool, |
| 97 |
'plugin': bool, |
| 98 |
'proc-macro': bool, |
| 99 |
'harness': bool, |
| 100 |
'edition': EDITION, |
| 101 |
'crate-type': T.List[CRATE_TYPE], |
| 102 |
'required-features': T.List[str], |
| 103 |
}, |
| 104 |
total=False, |
| 105 |
) |
| 106 |
|
| 107 |
|
| 108 |
class BuildTarget(_BaseBuildTarget, total=False): |
| 109 |
|
| 110 |
name: Required[str] |
| 111 |
|
| 112 |
|
| 113 |
class LibTarget(_BaseBuildTarget, total=False): |
| 114 |
|
| 115 |
name: str |
| 116 |
|
| 117 |
|
| 118 |
class Target(TypedDict): |
| 119 |
|
| 120 |
"""Target entry in the Manifest File.""" |
| 121 |
|
| 122 |
dependencies: T.Dict[str, T.Union[FromWorkspace, DependencyV]] |
| 123 |
|
| 124 |
|
| 125 |
Lint = TypedDict( |
| 126 |
'Lint', |
| 127 |
{ |
| 128 |
'level': Required[LINT_LEVEL], |
| 129 |
'priority': int, |
| 130 |
'check-cfg': T.List[str], |
| 131 |
}, |
| 132 |
total=True, |
| 133 |
) |
| 134 |
"""The representation of a linter setting. |
| 135 |
|
| 136 |
This does not include the name or tool, since those are the keys of the |
| 137 |
dictionaries that point to Lint. |
| 138 |
""" |
| 139 |
|
| 140 |
|
| 141 |
LintV = T.Union[Lint, str] |
| 142 |
"""A Lint entry, either a string or a Lint Dict.""" |
| 143 |
|
| 144 |
|
| 145 |
class Workspace(TypedDict): |
| 146 |
|
| 147 |
"""The representation of a workspace. |
| 148 |
|
| 149 |
In a vritual manifest the :attribute:`members` is always present, but in a |
| 150 |
project manifest, an empty workspace may be provided, in which case the |
| 151 |
workspace is implicitly filled in by values from the path based dependencies. |
| 152 |
|
| 153 |
the :attribute:`exclude` is always optional |
| 154 |
""" |
| 155 |
|
| 156 |
members: T.List[str] |
| 157 |
exclude: T.List[str] |
| 158 |
package: Package |
| 159 |
dependencies: T.Dict[str, DependencyV] |
| 160 |
|
| 161 |
|
| 162 |
Profile = TypedDict( |
| 163 |
'Profile', |
| 164 |
{ |
| 165 |
'opt-level': T.Union[int, str], |
| 166 |
'debug': T.Union[bool, int, str], |
| 167 |
'split-debuginfo': str, |
| 168 |
'strip': T.Union[bool, str], |
| 169 |
'debug-assertions': bool, |
| 170 |
'overflow-checks': bool, |
| 171 |
'lto': T.Union[bool, str], |
| 172 |
'panic': str, |
| 173 |
'incremental': bool, |
| 174 |
'codegen-units': int, |
| 175 |
'rpath': bool, |
| 176 |
'inherits': str, |
| 177 |
'package': T.Dict[str, 'Profile'], |
| 178 |
'build-override': 'Profile', |
| 179 |
}, |
| 180 |
total=False, |
| 181 |
) |
| 182 |
"""An entry in the [profile] section. |
| 183 |
|
| 184 |
See https://doc.rust-lang.org/cargo/reference/profiles.html |
| 185 |
""" |
| 186 |
|
| 187 |
|
| 188 |
Manifest = TypedDict( |
| 189 |
'Manifest', |
| 190 |
{ |
| 191 |
'package': Package, |
| 192 |
'badges': T.Dict[str, Badge], |
| 193 |
'dependencies': T.Dict[str, T.Union[FromWorkspace, DependencyV]], |
| 194 |
'dev-dependencies': T.Dict[str, T.Union[FromWorkspace, DependencyV]], |
| 195 |
'build-dependencies': T.Dict[str, T.Union[FromWorkspace, DependencyV]], |
| 196 |
'lib': LibTarget, |
| 197 |
'bin': T.List[BuildTarget], |
| 198 |
'test': T.List[BuildTarget], |
| 199 |
'bench': T.List[BuildTarget], |
| 200 |
'example': T.List[BuildTarget], |
| 201 |
'features': T.Dict[str, T.List[str]], |
| 202 |
'target': T.Dict[str, Target], |
| 203 |
'workspace': Workspace, |
| 204 |
'lints': T.Union[FromWorkspace, T.Dict[str, T.Dict[str, LintV]]], |
| 205 |
'patch': T.Dict[str, object], |
| 206 |
'profile': T.Dict[str, Profile], |
| 207 |
|
| 208 |
# TODO: replace? |
| 209 |
}, |
| 210 |
total=False, |
| 211 |
) |
| 212 |
"""The Cargo Manifest format.""" |
| 213 |
|
| 214 |
|
| 215 |
class CargoLockPackage(TypedDict, total=False): |
| 216 |
|
| 217 |
"""A description of a package in the Cargo.lock file format.""" |
| 218 |
|
| 219 |
name: str |
| 220 |
version: str |
| 221 |
source: str |
| 222 |
checksum: str |
| 223 |
|
| 224 |
|
| 225 |
class CargoLock(TypedDict, total=False): |
| 226 |
|
| 227 |
"""A description of the Cargo.lock file format.""" |
| 228 |
|
| 229 |
version: int |
| 230 |
package: T.List[CargoLockPackage] |
| 231 |
metadata: T.Dict[str, str] |