golem_rust/
uuid.rs

1// Copyright 2024 Golem Cloud
2//
3// Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 crate::bindings::golem::api::host::ComponentId;
16use std::fmt;
17use std::fmt::{Display, Formatter};
18use std::str::FromStr;
19use uuid::Uuid;
20
21impl From<crate::bindings::golem::api::host::Uuid> for Uuid {
22    fn from(uuid: crate::bindings::golem::api::host::Uuid) -> Self {
23        Uuid::from_u64_pair(uuid.high_bits, uuid.low_bits)
24    }
25}
26
27impl From<Uuid> for crate::bindings::golem::api::host::Uuid {
28    fn from(value: Uuid) -> Self {
29        let (high_bits, low_bits) = value.as_u64_pair();
30        Self {
31            high_bits,
32            low_bits,
33        }
34    }
35}
36
37impl From<Uuid> for crate::bindings::golem::api::host::ComponentId {
38    fn from(value: Uuid) -> Self {
39        Self { uuid: value.into() }
40    }
41}
42
43impl From<crate::bindings::golem::api::host::ComponentId> for Uuid {
44    fn from(value: crate::bindings::golem::api::host::ComponentId) -> Self {
45        value.uuid.into()
46    }
47}
48
49impl FromStr for crate::bindings::golem::api::host::ComponentId {
50    type Err = uuid::Error;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        Ok(Uuid::parse_str(s)?.into())
54    }
55}
56
57impl Display for crate::bindings::golem::api::host::ComponentId {
58    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
59        write!(f, "{:?}", &self.uuid)
60    }
61}
62
63impl Display for crate::bindings::golem::api::host::WorkerId {
64    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
65        write!(f, "{}/{}", self.component_id, self.worker_name)
66    }
67}
68
69impl FromStr for crate::bindings::golem::api::host::WorkerId {
70    type Err = String;
71
72    fn from_str(s: &str) -> Result<Self, Self::Err> {
73        let parts: Vec<&str> = s.split('/').collect();
74        if parts.len() == 2 {
75            let component_id = ComponentId::from_str(parts[0])
76                .map_err(|_| format!("invalid component id: {s} - expected uuid"))?;
77            let worker_name = parts[1].to_string();
78            Ok(Self {
79                component_id,
80                worker_name,
81            })
82        } else {
83            Err(format!(
84                "invalid worker id: {s} - expected format: <component_id>/<worker_name>"
85            ))
86        }
87    }
88}
89
90impl Display for crate::bindings::golem::api::host::PromiseId {
91    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
92        write!(f, "{}/{}", self.worker_id, self.oplog_idx)
93    }
94}
95
96impl FromStr for crate::bindings::golem::api::host::PromiseId {
97    type Err = String;
98
99    fn from_str(s: &str) -> Result<Self, Self::Err> {
100        let parts: Vec<&str> = s.split('/').collect();
101        if parts.len() == 2 {
102            let worker_id = crate::bindings::golem::api::host::WorkerId::from_str(parts[0])
103                .map_err(|_| {
104                    format!(
105                        "invalid worker id: {s} - expected format: <component_id>/<worker_name>"
106                    )
107                })?;
108            let oplog_idx = parts[1]
109                .parse()
110                .map_err(|_| format!("invalid oplog index: {s} - expected integer"))?;
111            Ok(Self {
112                worker_id,
113                oplog_idx,
114            })
115        } else {
116            Err(format!(
117                "invalid promise id: {s} - expected format: <worker_id>/<oplog_idx>"
118            ))
119        }
120    }
121}