cargo_resources/
specifications.rs

1use crate::resource_encoding::ResourceEncoding;
2use crate::{ResourceName, ResourceSha};
3use cargo_metadata::camino::Utf8PathBuf;
4use cargo_metadata::semver::Version;
5use cargo_metadata::Package;
6
7/// The fully populated resource specification (derived from a crate's resource declaration).
8#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
9pub struct ResourceSpecification {
10    /// The crate identifier
11    pub declaring_crate_name: String,
12    /// The crate version
13    pub declaring_crate_version: Version,
14
15    /// Whether resource's file encoding is text or binary
16    pub encoding: ResourceEncoding,
17
18    /// The full path of the resource within the crate.
19    pub full_crate_path: Utf8PathBuf,
20
21    /// The path of the resource as a resource
22    pub output_path: Utf8PathBuf,
23
24    /// The unique name for the resource
25    pub resource_name: String,
26    
27    /// Whether the package is local/patched or from a package registry.
28    pub is_local: bool
29}
30
31/// The fully populated specification of the consuming package.
32#[derive(serde::Deserialize, Debug)]
33pub struct ResourceConsumerSpecification {
34    /// The relative path of the resource root from the crate root
35    pub resource_root: Utf8PathBuf,
36
37    /// The required resources
38    pub required_resources: Vec<ResourceRequirement>
39}
40
41/// The fully populated specification for a resource usage.
42#[derive(serde::Deserialize, Debug)]
43pub struct ResourceRequirement {
44    /// The unique name of the required resource
45    pub resource_name: ResourceName,
46
47    /// The optional hex-encoded SHA256 value of the required resource
48    pub required_sha: Option<ResourceSha>
49}
50
51/// Derived Package Details
52#[derive(Debug)]
53pub (crate) struct PackageDetails<'m> {
54    /// True if is a dependency of the package (from the root package)
55    is_dependency: bool,
56
57    /// The package details from metadata
58    pub (crate) package: &'m Package
59}
60
61impl<'m> PackageDetails<'m> {
62    /// Create an instance initially assuming not a dependency.
63    pub (crate) fn new(package: &'m Package) -> Self {
64        Self {
65            is_dependency: false,
66            package,
67        }
68    }
69
70    /// Mark package as a dependency (of root node).
71    pub (crate) fn set_is_dependency(&mut self) {
72        self.is_dependency = true;
73    }
74
75    /// True if the package is a dependency (of the root node, inclusively)
76    pub (crate) fn is_dependency(&self) -> bool {
77        self.is_dependency
78    }
79
80}