/build/source/nativelink-service/src/remote_asset_proto.rs
Line  | Count  | Source  | 
1  |  | use bytes::Bytes;  | 
2  |  | use nativelink_proto::build::bazel::remote::asset::v1::Qualifier;  | 
3  |  | use nativelink_proto::build::bazel::remote::execution::v2::Digest;  | 
4  |  | use nativelink_util::common::DigestInfo;  | 
5  |  | use nativelink_util::digest_hasher::{DigestHasher, DigestHasherFunc}; | 
6  |  | use prost::Message;  | 
7  |  | use prost_types::Timestamp;  | 
8  |  |  | 
9  |  | #[derive(Debug, Clone, PartialEq)]  | 
10  |  | pub struct RemoteAssetQuery { | 
11  |  |     pub uri: String,  | 
12  |  |     pub qualifiers: Vec<Qualifier>,  | 
13  |  | }  | 
14  |  |  | 
15  |  | impl RemoteAssetQuery { | 
16  | 1  |     pub const fn new(uri: String, qualifiers: Vec<Qualifier>) -> Self { | 
17  | 1  |         Self { uri, qualifiers } | 
18  | 1  |     }  | 
19  |  |  | 
20  | 1  |     pub fn digest(&self) -> DigestInfo { | 
21  | 1  |         let mut hasher = DigestHasherFunc::Blake3.hasher();  | 
22  | 1  |         hasher.update(self.uri.as_bytes());  | 
23  | 2  |         for qualifier1  in &self.qualifiers {  | 
24  | 1  |             hasher.update(qualifier.name.as_bytes());  | 
25  | 1  |             hasher.update(qualifier.value.as_bytes());  | 
26  | 1  |         }  | 
27  | 1  |         hasher.finalize_digest()  | 
28  | 1  |     }  | 
29  |  | }  | 
30  |  |  | 
31  |  | #[derive(Clone, PartialEq, prost::Message)]  | 
32  |  | pub struct RemoteAssetArtifact { | 
33  |  |     #[prost(string, tag = "1")]  | 
34  |  |     pub uri: String,  | 
35  |  |  | 
36  |  |     #[prost(message, repeated, tag = "2")]  | 
37  |  |     pub qualifiers: Vec<Qualifier>,  | 
38  |  |  | 
39  |  |     #[prost(message, tag = "3")]  | 
40  |  |     pub blob_digest: Option<Digest>,  | 
41  |  |  | 
42  |  |     #[prost(message, optional, tag = "4")]  | 
43  |  |     pub expire_at: Option<Timestamp>,  | 
44  |  |  | 
45  |  |     #[prost(  | 
46  |  |         enumeration = "nativelink_proto::build::bazel::remote::execution::v2::digest_function::Value",  | 
47  |  |         tag = "5"  | 
48  |  |     )]  | 
49  |  |     pub digest_function: i32,  | 
50  |  | }  | 
51  |  |  | 
52  |  | impl RemoteAssetArtifact { | 
53  | 3  |     pub const fn new(  | 
54  | 3  |         uri: String,  | 
55  | 3  |         qualifiers: Vec<Qualifier>,  | 
56  | 3  |         blob_digest: Digest,  | 
57  | 3  |         expire_at: Option<Timestamp>,  | 
58  | 3  |         digest_function: i32,  | 
59  | 3  |     ) -> Self { | 
60  | 3  |         Self { | 
61  | 3  |             uri,  | 
62  | 3  |             qualifiers,  | 
63  | 3  |             blob_digest: Some(blob_digest),  | 
64  | 3  |             expire_at,  | 
65  | 3  |             digest_function,  | 
66  | 3  |         }  | 
67  | 3  |     }  | 
68  |  |  | 
69  | 2  |     pub fn digest(&self) -> DigestInfo { | 
70  | 2  |         let mut hasher = DigestHasherFunc::Blake3.hasher();  | 
71  | 2  |         hasher.update(self.uri.as_bytes());  | 
72  | 4  |         for qualifier2  in &self.qualifiers {  | 
73  | 2  |             hasher.update(qualifier.name.as_bytes());  | 
74  | 2  |             hasher.update(qualifier.value.as_bytes());  | 
75  | 2  |         }  | 
76  | 2  |         hasher.finalize_digest()  | 
77  | 2  |     }  | 
78  |  |  | 
79  | 2  |     pub fn as_bytes(&self) -> Bytes { | 
80  | 2  |         let encoded_asset = self.encode_to_vec();  | 
81  | 2  |         Bytes::from_owner(encoded_asset)  | 
82  | 2  |     }  | 
83  |  | }  |