polywrap_core/wrapper.rs
1use std::{any::Any, fmt::Debug, sync::Arc};
2
3use crate::{error::Error, invoker::Invoker};
4
5/// The `Encoding` enum is used to specify the type of encoding for a file.
6/// It currently supports Base64 and UTF8 encoding.
7pub enum Encoding {
8 Base64,
9 UTF8,
10}
11
12/// Specifies the options when getting a file.
13/// It contains a `path` field for the file path and an `encoding` field for the file encoding.
14pub struct GetFileOptions {
15 /// The path of the file to get.
16 pub path: String,
17 /// The encoding of the file. This is an optional field.
18 pub encoding: Option<Encoding>,
19}
20
21/// Common interface for objects that can be invoked and can get files.
22pub trait Wrapper: Send + Sync + Debug + Any {
23 /// The `invoke` method is used to invoke the object with a method, arguments, environment, and invoker.
24 /// It returns a Result containing a msgpack buffer on success, or an Error on failure.
25 ///
26 /// # Arguments
27 ///
28 /// * `method` - The name of the method to invoke.
29 /// * `args` - Optional msgpack buffer representing the arguments to the method.
30 /// * `env` - Optional msgpack buffer representing the environment for the method.
31 /// * `invoker` - `Invoker` to invoke this wrapper with.
32 fn invoke(
33 &self,
34 method: &str,
35 args: Option<&[u8]>,
36 env: Option<&[u8]>,
37 invoker: Arc<dyn Invoker>,
38 ) -> Result<Vec<u8>, Error>;
39
40 /// The `get_file` method is used to get a file with the specified options.
41 /// It returns a Result containing a byte vector on success, or an Error on failure.
42 fn get_file(&self, options: &GetFileOptions) -> Result<Vec<u8>, Error>;
43}