Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples #314

Merged
merged 6 commits into from
Feb 3, 2025
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
4 changes: 2 additions & 2 deletions crux_core/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,14 @@ where
self.effects.is_empty() && self.events.is_empty() && self.tasks.is_empty()
}

/// Run the effect state machine until it settles and collect all effects generated
/// Run the effect state machine until it settles and return an iterator over the effects
pub fn effects(&mut self) -> impl Iterator<Item = Effect> + '_ {
self.run_until_settled();

self.effects.try_iter()
}

/// Run the effect state machine until it settles and collect all events generated
/// Run the effect state machine until it settles and return an iterator over the events
pub fn events(&mut self) -> impl Iterator<Item = Event> + '_ {
self.run_until_settled();

Expand Down
21 changes: 20 additions & 1 deletion crux_core/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
channel::Receiver, executor_and_spawner, CommandSpawner, Operation, ProtoContext,
QueuingExecutor,
},
Request, WithContext,
Command, Request, WithContext,
};

/// AppTester is a simplified execution environment for Crux apps for use in
Expand Down Expand Up @@ -237,6 +237,25 @@ impl<Ef, Ev> Update<Ef, Ev> {
}
}

impl<Effect, Event> Command<Effect, Event>
where
Effect: Send + 'static,
Event: Send + 'static,
{
/// Assert that the Command contains _exactly_ one effect and zero events,
/// and return the effect
pub fn expect_one_effect(&mut self) -> Effect {
if self.events().next().is_some() {
panic!("Expected only one effect, but found an event");
}
if let Some(effect) = self.effects().next() {
effect
} else {
panic!("Expected one effect but found none");
}
}
}

/// Panics if the pattern doesn't match an `Effect` from the specified `Update`
///
/// Like in a `match` expression, the pattern can be optionally followed by `if`
Expand Down
20 changes: 20 additions & 0 deletions crux_platform/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::{future::Future, marker::PhantomData};

use crux_core::{command::RequestBuilder, Command, Request};

use crate::{PlatformRequest, PlatformResponse};

pub struct Platform<Effect, Event> {
effect: PhantomData<Effect>,
event: PhantomData<Event>,
}

impl<Effect, Event> Platform<Effect, Event>
where
Effect: From<Request<PlatformRequest>> + Send + 'static,
Event: Send + 'static,
{
pub fn get() -> RequestBuilder<Effect, Event, impl Future<Output = PlatformResponse>> {
Command::request_from_shell(PlatformRequest)
}
}
2 changes: 2 additions & 0 deletions crux_platform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! TODO mod docs

pub mod command;

use crux_core::capability::{CapabilityContext, Operation};
use crux_core::macros::Capability;
use serde::{Deserialize, Serialize};
Expand Down
17 changes: 3 additions & 14 deletions examples/cat_facts/Cargo.lock

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

20 changes: 10 additions & 10 deletions examples/cat_facts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ rust-version = "1.66"

[workspace.dependencies]
anyhow = "1.0.95"
# crux_core = { path = "../../crux_core" }
# crux_http = { path = "../../crux_http" }
# crux_kv = { path = "../../crux_kv" }
# crux_platform = { path = "../../crux_platform" }
# crux_time = { path = "../../crux_time", features = ["chrono"] }
crux_core = "0.11.1"
crux_http = "0.11.4"
crux_kv = "0.6.0"
crux_platform = "0.3.0"
crux_time = { version = "0.8.0", features = ["chrono"] }
crux_core = { path = "../../crux_core" }
crux_http = { path = "../../crux_http" }
crux_kv = { path = "../../crux_kv" }
crux_platform = { path = "../../crux_platform" }
crux_time = { path = "../../crux_time", features = ["chrono"] }
# crux_core = "0.11.1"
# crux_http = "0.11.4"
# crux_kv = "0.6.0"
# crux_platform = "0.3.0"
# crux_time = { version = "0.8.0", features = ["chrono"] }
serde = "1.0.217"

[workspace.metadata.bin]
Expand Down
Loading