Skip to content

Commit

Permalink
feat(rs-port2): add errors for Value
Browse files Browse the repository at this point in the history
  • Loading branch information
hulxv committed Nov 25, 2024
1 parent 6d5660d commit 5203822
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 25 deletions.
63 changes: 63 additions & 0 deletions source/ports/rs_port2/Cargo.lock

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

2 changes: 2 additions & 0 deletions source/ports/rs_port2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.93"
metacall-bindings = { path = "crates/metacall-bindings" }
thiserror = "2.0.3"
36 changes: 11 additions & 25 deletions source/ports/rs_port2/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,20 @@ use metacall_bindings::value::{
create::metacall_value_create_long, metacall_value_destroy, metacall_value_from_long,
metacall_value_to_long,
};
use std::ffi::c_long;

trait Create<T> {
fn new(value: T) -> Self;
}

trait From<T> {
fn from(&mut self, value: T) -> &Self;
use thiserror::Error;
}

trait To<T> {
fn to(&self) -> T;
#[derive(Error, Debug)]
pub enum ValueError {
#[error("Failed to create CString: {0}")]
CStringError(#[from] std::ffi::NulError),
#[error("Failed to convert CString to &str: {0}")]
Utf8Error(#[from] std::str::Utf8Error),
#[error("Null pointer encountered")]
NullPointer,
#[error("Unknown error")]
Unknown,
}

struct Value(*mut std::os::raw::c_void);

impl Create<i64> for Value {
fn new(value: i64) -> Self {
let val = unsafe { metacall_value_create_long(value as c_long) };
Self(val)
}
}

impl From<i64> for Value {
fn from(&mut self, value: i64) -> &Self {
self.0 = unsafe { metacall_value_from_long(self.0, value as c_long) };
self
}
}

impl To<i64> for Value {
Expand Down

0 comments on commit 5203822

Please sign in to comment.