Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 43 additions & 38 deletions packages/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use polywrap_core::{
uri_resolver::{UriResolver, UriResolverHandler}, helpers::get_env_from_resolution_path,
},
uri::Uri,
wrapper::Wrapper,
wrapper::Wrapper, wrap_loader::WrapLoader, wrap_invoker::WrapInvoker,
};
use polywrap_msgpack::decode;
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -148,54 +148,56 @@ impl Invoker for PolywrapClient {

None
}
}

impl Client for PolywrapClient {
fn get_env_by_uri(&self, uri: &Uri) -> Option<&Env> {
if let Some(envs) = &self.envs {
return envs.get(&uri.to_string());
}

None
}
}

impl WrapLoader for PolywrapClient {
fn load_wrapper(
&self,
uri: &Uri,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Arc<dyn Wrapper>, Error> {
let mut empty_res_context = UriResolutionContext::new();
let mut resolution_context = match resolution_context {
None => &mut empty_res_context,
Some(ctx) => ctx,
};

let uri_package_or_wrapper = self
.try_resolve_uri(uri, Some(&mut resolution_context))
.map_err(|e| Error::ResolutionError(e.to_string()))?;

match uri_package_or_wrapper {
UriPackageOrWrapper::Uri(uri) => Err(Error::ResolutionError(format!(
"Failed to resolve wrapper: {uri}"
))),
UriPackageOrWrapper::Wrapper(_, wrapper) => Ok(wrapper),
UriPackageOrWrapper::Package(_, package) => {
let wrapper = package
.create_wrapper()
.map_err(|e| Error::WrapperCreateError(e.to_string()))?;
Ok(wrapper)
}
}
&self,
uri: &Uri,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Arc<dyn Wrapper>, Error> {
let mut empty_res_context = UriResolutionContext::new();
let mut resolution_context = match resolution_context {
None => &mut empty_res_context,
Some(ctx) => ctx,
};
let uri_package_or_wrapper = self
.try_resolve_uri(uri, Some(&mut resolution_context))
.map_err(|e| Error::ResolutionError(e.to_string()))?;
match uri_package_or_wrapper {
UriPackageOrWrapper::Uri(uri) => Err(Error::ResolutionError(format!(
"Failed to resolve wrapper: {uri}"
))),
UriPackageOrWrapper::Wrapper(_, wrapper) => Ok(wrapper),
UriPackageOrWrapper::Package(_, package) => {
let wrapper = package
.create_wrapper()
.map_err(|e| Error::WrapperCreateError(e.to_string()))?;
Ok(wrapper)
}
}
}
}

impl WrapInvoker for PolywrapClient {
fn invoke_wrapper_raw(
&self,
wrapper: &dyn Wrapper,
uri: &Uri,
method: &str,
args: Option<&[u8]>,
env: Option<&Env>,
resolution_context: Option<&mut UriResolutionContext>,
&self,
wrapper: &dyn Wrapper,
uri: &Uri,
method: &str,
args: Option<&[u8]>,
env: Option<&Env>,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Vec<u8>, Error> {
let mut empty_res_context = UriResolutionContext::new();
let resolution_context = match resolution_context {
Expand Down Expand Up @@ -252,19 +254,22 @@ impl UriResolverHandler for PolywrapClient {
}
}

impl Client for PolywrapClient {
}

#[cfg(test)]
mod client_tests {
use crate::client::Env;
use polywrap_core::{
client::{ClientConfig, Client},
client::ClientConfig,
error::Error,
invoker::Invoker,
resolvers::{
uri_resolution_context::{UriPackageOrWrapper, UriResolutionContext},
uri_resolver::{UriResolver, UriResolverHandler},
},
uri::Uri,
wrapper::{GetFileOptions, Wrapper},
wrapper::{GetFileOptions, Wrapper}, wrap_loader::WrapLoader,
};
use std::sync::Arc;

Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/subinvoker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ impl Invoker for Subinvoker {
fn get_interfaces(&self) -> Option<InterfaceImplementations> {
self.invoker.get_interfaces()
}
fn get_env_by_uri(&self, uri: &Uri) -> Option<&Env> {
self.invoker.get_env_by_uri(uri)
}
}
27 changes: 5 additions & 22 deletions packages/core/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::sync::Arc;

use crate::error::Error;
use crate::invoker::Invoker;
use crate::resolvers::uri_resolution_context::UriResolutionContext;
use crate::uri::Uri;
use crate::interface_implementation::InterfaceImplementations;
use crate::resolvers::uri_resolver::{UriResolverHandler, UriResolver};
use crate::env::{Envs, Env};
use crate::wrapper::Wrapper;
use crate::env::Envs;
use crate::invoker::Invoker;
use crate::wrap_invoker::WrapInvoker;
use crate::wrap_loader::WrapLoader;

#[derive(Clone,Debug)]
pub struct UriRedirect {
Expand All @@ -28,20 +27,4 @@ pub struct ClientConfig {
pub interfaces: Option<InterfaceImplementations>
}

pub trait Client: Invoker + UriResolverHandler {
fn get_env_by_uri(&self, uri: &Uri) -> Option<&Env>;
fn load_wrapper(
&self,
uri: &Uri,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Arc<dyn Wrapper>, Error>;
fn invoke_wrapper_raw(
&self,
wrapper: &dyn Wrapper,
uri: &Uri,
method: &str,
args: Option<&[u8]>,
env: Option<&Env>,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Vec<u8>, Error>;
}
pub trait Client: Invoker + WrapLoader + WrapInvoker + UriResolverHandler {}
5 changes: 3 additions & 2 deletions packages/core/src/invoker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
error::Error, uri::Uri, resolvers::uri_resolution_context::UriResolutionContext, env::{Env}, interface_implementation::InterfaceImplementations,
error::Error, uri::Uri, resolvers::uri_resolution_context::UriResolutionContext, env::Env,
interface_implementation::InterfaceImplementations,
};

pub trait Invoker: Send + Sync {
fn invoke_raw(
&self,
Expand All @@ -13,4 +13,5 @@ pub trait Invoker: Send + Sync {
) -> Result<Vec<u8>, Error>;
fn get_implementations(&self, uri: &Uri) -> Result<Vec<Uri>, Error>;
fn get_interfaces(&self) -> Option<InterfaceImplementations>;
fn get_env_by_uri(&self, uri: &Uri) -> Option<&Env>;
}
2 changes: 2 additions & 0 deletions packages/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod error;
pub mod client;
pub mod wrapper;
pub mod invoker;
pub mod wrap_loader;
pub mod wrap_invoker;
pub mod redirects;
pub mod package;
pub mod file_reader;
Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/wrap_invoker.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use crate::{resolvers::uri_resolution_context::UriResolutionContext, error::Error, wrapper::Wrapper, uri::Uri, env::Env};

pub trait WrapInvoker: Send + Sync {
fn invoke_wrapper_raw(
&self,
wrapper: &dyn Wrapper,
uri: &Uri,
method: &str,
args: Option<&[u8]>,
env: Option<&Env>,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Vec<u8>, Error>;
}
11 changes: 11 additions & 0 deletions packages/core/src/wrap_loader.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::sync::Arc;

use crate::{resolvers::uri_resolution_context::UriResolutionContext, error::Error, wrapper::Wrapper, uri::Uri};

pub trait WrapLoader: Send + Sync {
fn load_wrapper(
&self,
uri: &Uri,
resolution_context: Option<&mut UriResolutionContext>,
) -> Result<Arc<dyn Wrapper>, Error>;
}
2 changes: 1 addition & 1 deletion packages/native/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl FFIClient {
&_decoded_env
});

self.inner_client.invoke_wrapper_raw(wrapper.0.clone(), uri.as_ref(), method, args, env, None)
self.inner_client.invoke_wrapper_raw(&*wrapper.0.clone(), uri.as_ref(), method, args, env, None)
}

pub fn load_wrapper(&self, uri: Arc<Uri>) -> Result<Arc<FFIWrapper>, Error> {
Expand Down
4 changes: 4 additions & 0 deletions packages/wasm/tests/test_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl Invoker for MockInvoker {
let i = HashMap::new();
Some(i)
}

fn get_env_by_uri(&self, _: &Uri) -> Option<&Env> {
None
}
}

#[test]
Expand Down