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
75 changes: 74 additions & 1 deletion 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 rust_slim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ readme = "readme.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = { version = "0.4", default-features = false, features = ["std"] }
slim_protocol = { path = "../slim_protocol" }
convert_case = "0.6"
rust_slim_macros = { path = "../rust_slim_macros", optional = true }
thiserror = "1"

[dev-dependencies]
anyhow = "1"
trybuild = "1"

[features]
macros = ["dep:rust_slim_macros"]
11 changes: 10 additions & 1 deletion rust_slim/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ It does not support using the STOUD and STDIN for comunication.

This is currently in an unstable version. The general API can change in the next versions.

For more details, take a look at the [documentation](https://docs.rs/rust_slim/latest/rust_slim/)
For more details, take a look at the [documentation](https://docs.rs/rust_slim/latest/rust_slim/)

## Migrating manually implemented fixtures

`SlimFixture::execute_method` now receives `Vec<SlimValue>` and returns
`Result<SlimValue, ExecuteMethodError>`. Use `FromSlimValue` to convert each
argument and `IntoSlimValue` to convert the method result. These conversions
preserve protocol lists, null, void, and in-process object handles. The old
`ToSlimResultString` trait remains available as a string conversion helper,
but it does not preserve those structured values.
24 changes: 19 additions & 5 deletions rust_slim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ pub use server::SlimServer;
use std::fmt::{Display, Formatter};
pub use to_slim_result_string::*;
pub use utils::from_rust_module_path_to_class_path;
pub use value::{FromSlimValue, IntoSlimValue, SlimObject, SlimValue};

mod server;
mod to_slim_result_string;
mod utils;
mod value;

/// Fixtures must implement this trait to be able to be executed by the slim server.
/// The `#[fixture]` macro will automatically implement it for the type in the impl block.
Expand All @@ -132,8 +134,8 @@ pub trait SlimFixture {
fn execute_method(
&mut self,
method: &str,
args: Vec<String>,
) -> Result<String, ExecuteMethodError>;
args: Vec<SlimValue>,
) -> Result<SlimValue, ExecuteMethodError>;
}

/// ClassPath that will be used in the construction of the fixture.
Expand All @@ -155,14 +157,14 @@ pub trait ClassPath {

/// Trait used to construct the fixture. It is auto-implemented for fixtures that also implement Default.
pub trait Constructor {
fn construct(args: Vec<String>) -> Self;
fn construct(args: Vec<SlimValue>) -> Self;
}

impl<T> Constructor for T
where
T: Default,
{
fn construct(_args: Vec<String>) -> T {
fn construct(_args: Vec<SlimValue>) -> T {
T::default()
}
}
Expand All @@ -172,12 +174,24 @@ where
pub enum ExecuteMethodError {
/// The method might not exists, which should cause a MethodNotFound error.
MethodNotFound { method: String, class: String },
/// We might have an issue parsing the arguments. The implementation made by the `#[fixture]` macro tries to parse each argument using the [FromStr](https://doc.rust-lang.org/std/str/trait.FromStr.html) trait.
/// A fixture argument could not be converted through [`FromSlimValue`].
ArgumentParsingError(String),
/// And there might be some failure in the method itself, which should cause an ExecutionError.
ExecutionError(String),
}

impl ExecuteMethodError {
/// Adds the zero-based fixture argument position to a conversion error.
pub fn for_argument(self, index: usize) -> Self {
match self {
Self::ArgumentParsingError(message) => {
Self::ArgumentParsingError(format!("{}: {message}", index + 1))
}
error => error,
}
}
}

impl Display for ExecuteMethodError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
Loading
Loading