guppy/platform/platform_spec.rs
1// Copyright (c) The cargo-guppy Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4#[allow(unused_imports)]
5use crate::platform::EnabledTernary;
6use crate::{errors::TargetSpecError, platform::Platform};
7use std::sync::Arc;
8
9/// A specifier for a single platform, or for a range of platforms.
10///
11/// Some uses of `guppy` care about a single platform, and others care about queries against the
12/// intersection of all hypothetical platforms, or against a union of any of them. `PlatformSpec`
13/// handles the
14///
15/// `PlatformSpec` does not currently support expressions, but it might in the future, using an
16/// [SMT solver](https://en.wikipedia.org/wiki/Satisfiability_modulo_theories).
17#[derive(Clone, Debug)]
18#[non_exhaustive]
19pub enum PlatformSpec {
20 /// The intersection of all platforms.
21 ///
22 /// Dependency queries performed against this variant will return [`EnabledTernary::Enabled`] if
23 /// and only if a dependency is not platform-dependent. They can never return
24 /// [`EnabledTernary::Unknown`].
25 ///
26 /// This variant does not currently understand expressions that always evaluate to true
27 /// (tautologies), like `cfg(any(unix, not(unix)))` or `cfg(all())`. In the future, an SMT
28 /// solver would be able to handle such expressions.
29 Always,
30
31 /// An individual platform.
32 ///
33 /// Dependency queries performed against this variant will return [`EnabledTernary::Enabled`] if
34 /// and only if a dependency is enabled on this platform. They may also return
35 /// [`EnabledTernary::Unknown`] if a platform is not enabled.
36 Platform(Arc<Platform>),
37
38 /// The union of all platforms.
39 ///
40 /// Dependency queries performed against this variant will return [`EnabledTernary::Enabled`] if
41 /// a dependency is enabled on any platform.
42 ///
43 /// This variant does not currently understand expressions that always evaluate to false
44 /// (contradictions), like `cfg(all(unix, not(unix)))` or `cfg(any())`. In the future, an SMT
45 /// solver would be able to handle such expressions.
46 Any,
47}
48
49impl PlatformSpec {
50 /// Previous name for [`Self::build_target`], renamed to clarify what
51 /// `current` means.
52 ///
53 /// This method is deprecated and will be removed in a future version.
54 #[deprecated(
55 since = "0.17.13",
56 note = "this method has been renamed to `build_target`"
57 )]
58 #[inline]
59 pub fn current() -> Result<Self, TargetSpecError> {
60 Self::build_target()
61 }
62
63 /// Returns a `PlatformSpec` corresponding to the target platform, as
64 /// determined at build time.
65 ///
66 /// Returns an error if the build target was unknown to the version of
67 /// `target-spec` in use.
68 pub fn build_target() -> Result<Self, TargetSpecError> {
69 Ok(PlatformSpec::Platform(Arc::new(Platform::build_target()?)))
70 }
71}
72
73impl<T: Into<Arc<Platform>>> From<T> for PlatformSpec {
74 #[inline]
75 fn from(platform: T) -> Self {
76 PlatformSpec::Platform(platform.into())
77 }
78}