Debian Codegraph @

src/auditwheel/platform_tag.rs

raw
1 use crate::auditwheel::Policy;
2 use serde::{Deserialize, Deserializer, Serialize};
3 use std::fmt;
4 use std::str::FromStr;
5
6 /// Decides how to handle manylinux and musllinux compliance
7 #[derive(Serialize, Debug, Clone, Eq, PartialEq, Copy, Ord, PartialOrd)]
8 #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
9 pub enum PlatformTag {
10 /// Use the `manylinux_<major>_<minor>` tag
11 Manylinux {
12 /// GLIBC version major
13 major: u16,
14 /// GLIBC version minor
15 minor: u16,
16 },
17 /// Use the `musllinux_<major>_<minor>` tag
18 Musllinux {
19 /// musl libc version major
20 major: u16,
21 /// musl libc version minor
22 minor: u16,
23 },
24 /// Use the native linux tag
25 Linux,
26 /// Ensure that a PyPI-compatible tag is used, error if the target is not supported by PyPI.
27 Pypi,
28 }
29
30 impl PlatformTag {
31 /// `manylinux1` aka `manylinux_2_5`
32 pub fn manylinux1() -> Self {
33 Self::Manylinux { major: 2, minor: 5 }
34 }
35
36 /// `manylinux2010` aka `manylinux_2_12`
37 pub fn manylinux2010() -> Self {
38 Self::Manylinux {
39 major: 2,
40 minor: 12,
41 }
42 }
43
44 /// `manylinux2014` aka `manylinux_2_17`
45 pub fn manylinux2014() -> Self {
46 Self::Manylinux {
47 major: 2,
48 minor: 17,
49 }
50 }
51
52 /// manylinux aliases, namely `manylinux1`, `manylinux2010` and `manylinux2014`.
53 pub fn aliases(&self) -> Vec<String> {
54 Policy::from_tag(self)
55 .map(|policy| policy.aliases)
56 .unwrap_or_default()
57 }
58
59 /// Is this a portable linux platform tag
60 ///
61 /// Only manylinux and musllinux are portable
62 pub fn is_portable(&self) -> bool {
63 !matches!(self, PlatformTag::Linux)
64 }
65
66 /// Is this a manylinux platform tag
67 pub fn is_manylinux(&self) -> bool {
68 matches!(self, PlatformTag::Manylinux { .. })
69 }
70
71 /// Is this a musllinux platform tag
72 pub fn is_musllinux(&self) -> bool {
73 matches!(self, PlatformTag::Musllinux { .. })
74 }
75
76 /// Is this the PyPI compatibility option
77 pub fn is_pypi(&self) -> bool {
78 matches!(self, PlatformTag::Pypi)
79 }
80
81 /// Is it supported by Rust compiler and manylinux project
82 pub fn is_supported(&self) -> bool {
83 match self {
84 PlatformTag::Manylinux { major, minor } => (*major, *minor) >= (2, 17),
85 PlatformTag::Musllinux { .. } => true,
86 PlatformTag::Linux => true,
87 PlatformTag::Pypi => true,
88 }
89 }
90 }
91
92 impl fmt::Display for PlatformTag {
93 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94 match *self {
95 PlatformTag::Manylinux { major, minor } => write!(f, "manylinux_{major}_{minor}"),
96 PlatformTag::Musllinux { major, minor } => write!(f, "musllinux_{major}_{minor}"),
97 PlatformTag::Linux => write!(f, "linux"),
98 PlatformTag::Pypi => write!(f, "pypi"),
99 }
100 }
101 }
102
103 impl FromStr for PlatformTag {
104 type Err = &'static str;
105
106 fn from_str(value: &str) -> anyhow::Result<Self, Self::Err> {
107 let value = value.to_ascii_lowercase();
108 match value.as_str() {
109 "off" | "linux" => Ok(PlatformTag::Linux),
110 "pypi" => Ok(PlatformTag::Pypi),
111 "1" | "manylinux1" => Ok(PlatformTag::manylinux1()),
112 "2010" | "manylinux2010" => Ok(PlatformTag::manylinux2010()),
113 "2014" | "manylinux2014" => Ok(PlatformTag::manylinux2014()),
114 _ => {
115 if let Some(value) = value.strip_prefix("musllinux_") {
116 let mut parts = value.split('_');
117 let major = parts
118 .next()
119 .and_then(|major| major.parse::<u16>().ok())
120 .ok_or("invalid musllinux option")?;
121 let minor = parts
122 .next()
123 .and_then(|minor| minor.parse::<u16>().ok())
124 .ok_or("invalid musllinux option")?;
125 Ok(PlatformTag::Musllinux { major, minor })
126 } else {
127 let value = value.strip_prefix("manylinux_").unwrap_or(&value);
128 let mut parts = value.split('_');
129 let major = parts
130 .next()
131 .and_then(|major| major.parse::<u16>().ok())
132 .ok_or("invalid manylinux option")?;
133 let minor = parts
134 .next()
135 .and_then(|minor| minor.parse::<u16>().ok())
136 .ok_or("invalid manylinux option")?;
137 Ok(PlatformTag::Manylinux { major, minor })
138 }
139 }
140 }
141 }
142 }
143
144 impl<'de> Deserialize<'de> for PlatformTag {
145 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
146 where
147 D: Deserializer<'de>,
148 {
149 let s = String::deserialize(deserializer)?;
150 FromStr::from_str(&s).map_err(serde::de::Error::custom)
151 }
152 }