Skip to content

Commit

Permalink
Conversions for ComponentId, WorkerId and PromiseId
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Jul 26, 2024
1 parent 6aba395 commit a4b2c34
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion golem-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
golem-rust-macro = { path = "../golem-rust-macro", version = "0.0.0", optional = true }
uuid = { version = "1.8.0", features = ["v4"], optional = true }
uuid = { version = "1.10.0", features = ["v4"], optional = true }
wit-bindgen-rt = { version = "0.26.0", features = ["bitflags"] }

[features]
Expand Down
90 changes: 90 additions & 0 deletions golem-rust/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::bindings::golem::api::host::ComponentId;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use uuid::Uuid;

impl From<crate::bindings::golem::api::host::Uuid> for Uuid {
Expand All @@ -29,3 +33,89 @@ impl From<Uuid> for crate::bindings::golem::api::host::Uuid {
}
}
}

impl From<Uuid> for crate::bindings::golem::api::host::ComponentId {
fn from(value: Uuid) -> Self {
Self { uuid: value.into() }
}
}

impl From<crate::bindings::golem::api::host::ComponentId> for Uuid {
fn from(value: crate::bindings::golem::api::host::ComponentId) -> Self {
value.uuid.into()
}
}

impl FromStr for crate::bindings::golem::api::host::ComponentId {
type Err = uuid::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Uuid::parse_str(s)?.into())
}
}

impl Display for crate::bindings::golem::api::host::ComponentId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", &self.uuid)
}
}

impl Display for crate::bindings::golem::api::host::WorkerId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.component_id, self.worker_name)
}
}

impl FromStr for crate::bindings::golem::api::host::WorkerId {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split('/').collect();
if parts.len() == 2 {
let component_id = ComponentId::from_str(parts[0])
.map_err(|_| format!("invalid component id: {s} - expected uuid"))?;
let worker_name = parts[1].to_string();
Ok(Self {
component_id,
worker_name,
})
} else {
Err(format!(
"invalid worker id: {s} - expected format: <component_id>/<worker_name>"
))
}
}
}

impl Display for crate::bindings::golem::api::host::PromiseId {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.worker_id, self.oplog_idx)
}
}

impl FromStr for crate::bindings::golem::api::host::PromiseId {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.split('/').collect();
if parts.len() == 2 {
let worker_id = crate::bindings::golem::api::host::WorkerId::from_str(parts[0])
.map_err(|_| {
format!(
"invalid worker id: {s} - expected format: <component_id>/<worker_name>"
)
})?;
let oplog_idx = parts[1]
.parse()
.map_err(|_| format!("invalid oplog index: {s} - expected integer"))?;
Ok(Self {
worker_id,
oplog_idx,
})
} else {
Err(format!(
"invalid promise id: {s} - expected format: <worker_id>/<oplog_idx>"
))
}
}
}

0 comments on commit a4b2c34

Please sign in to comment.