rib/registry/
component_dependency_key.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16use uuid::Uuid;
17
18#[derive(Debug, Hash, PartialEq, Eq, Clone, Ord, PartialOrd)]
19pub struct ComponentDependencyKey {
20    pub component_name: String,
21    pub component_id: Uuid,
22    pub root_package_name: Option<String>,
23    pub root_package_version: Option<String>,
24}
25
26impl bincode::Encode for ComponentDependencyKey {
27    fn encode<E: bincode::enc::Encoder>(
28        &self,
29        encoder: &mut E,
30    ) -> Result<(), bincode::error::EncodeError> {
31        use bincode::enc::write::Writer;
32
33        encoder.writer().write(self.component_name.as_bytes())?;
34        self.component_id.as_bytes().encode(encoder)?;
35        Option::<String>::encode(&self.root_package_name, encoder)?;
36        Option::<String>::encode(&self.root_package_version, encoder)?;
37
38        Ok(())
39    }
40}
41
42impl<Context> bincode::Decode<Context> for ComponentDependencyKey {
43    fn decode<D: bincode::de::Decoder>(
44        decoder: &mut D,
45    ) -> Result<Self, bincode::error::DecodeError> {
46        use bincode::de::read::Reader;
47
48        let component_name = String::decode(decoder)?;
49        let mut bytes = [0u8; 16];
50        decoder.reader().read(&mut bytes)?;
51        let component_id = Uuid::from_bytes(bytes);
52        let root_package_name = Option::<String>::decode(decoder)?;
53        let root_package_version = Option::<String>::decode(decoder)?;
54
55        Ok(ComponentDependencyKey {
56            component_name,
57            component_id,
58            root_package_name,
59            root_package_version,
60        })
61    }
62}
63
64impl<'de, Context> bincode::BorrowDecode<'de, Context> for ComponentDependencyKey {
65    fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = Context>>(
66        decoder: &mut D,
67    ) -> Result<Self, bincode::error::DecodeError> {
68        use bincode::de::read::Reader;
69
70        let component_name = String::borrow_decode(decoder)?;
71        let mut bytes = [0u8; 16];
72        decoder.reader().read(&mut bytes)?;
73        let component_id = Uuid::from_bytes(bytes);
74        let root_package_name = Option::<String>::borrow_decode(decoder)?;
75        let root_package_version = Option::<String>::borrow_decode(decoder)?;
76
77        Ok(ComponentDependencyKey {
78            component_name,
79            component_id,
80            root_package_name,
81            root_package_version,
82        })
83    }
84}
85
86impl Display for ComponentDependencyKey {
87    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
88        write!(
89            f,
90            "Component: {}, ID: {}, Root Package: {}@{}",
91            self.component_name,
92            self.component_id,
93            self.root_package_name.as_deref().unwrap_or("unknown"),
94            self.root_package_version.as_deref().unwrap_or("unknown")
95        )
96    }
97}