Skip to content

Commit

Permalink
age-plugin: Make arguments to run_state_machine optional
Browse files Browse the repository at this point in the history
Closes #387.
  • Loading branch information
str4d committed Aug 6, 2023
1 parent 56edd88 commit 0ec5e79
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions age-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ to 1.0.0 are beta releases.
## [Unreleased]
### Changed
- MSRV is now 1.65.0.
- `age_plugin::run_state_machine` now takes optional arguments, to enable the
creation of recipient-only or identity-only plugins.

## [0.4.0] - 2022-10-27
### Changed
Expand Down
6 changes: 5 additions & 1 deletion age-plugin/examples/age-plugin-unencrypted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ fn main() -> io::Result<()> {
let opts = PluginOptions::parse_args_default_or_exit();

if let Some(state_machine) = opts.age_plugin {
run_state_machine(&state_machine, || RecipientPlugin, || IdentityPlugin)
run_state_machine(
&state_machine,
Some(|| RecipientPlugin),
Some(|| IdentityPlugin),
)
} else {
// A real plugin would generate a new identity here.
print_new_identity(PLUGIN_NAME, &[], &[]);
Expand Down
26 changes: 22 additions & 4 deletions age-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,32 @@ pub fn print_new_identity(plugin_name: &str, identity: &[u8], recipient: &[u8])
/// argument when starting the plugin.
pub fn run_state_machine<R: recipient::RecipientPluginV1, I: identity::IdentityPluginV1>(
state_machine: &str,
recipient_v1: impl FnOnce() -> R,
identity_v1: impl FnOnce() -> I,
recipient_v1: Option<impl FnOnce() -> R>,
identity_v1: Option<impl FnOnce() -> I>,
) -> io::Result<()> {
use age_core::plugin::{IDENTITY_V1, RECIPIENT_V1};

match state_machine {
RECIPIENT_V1 => recipient::run_v1(recipient_v1()),
IDENTITY_V1 => identity::run_v1(identity_v1()),
RECIPIENT_V1 => {
if let Some(plugin) = recipient_v1 {
recipient::run_v1(plugin())

Check warning on line 224 in age-plugin/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

age-plugin/src/lib.rs#L222-L224

Added lines #L222 - L224 were not covered by tests
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"plugin doesn't support recipient-v1 state machine",

Check warning on line 228 in age-plugin/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

age-plugin/src/lib.rs#L226-L228

Added lines #L226 - L228 were not covered by tests
))
}
}
IDENTITY_V1 => {
if let Some(plugin) = identity_v1 {
identity::run_v1(plugin())

Check warning on line 234 in age-plugin/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

age-plugin/src/lib.rs#L232-L234

Added lines #L232 - L234 were not covered by tests
} else {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
"plugin doesn't support identity-v1 state machine",

Check warning on line 238 in age-plugin/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

age-plugin/src/lib.rs#L236-L238

Added lines #L236 - L238 were not covered by tests
))
}
}
_ => Err(io::Error::new(
io::ErrorKind::InvalidInput,
"unknown plugin state machine",
Expand Down

0 comments on commit 0ec5e79

Please sign in to comment.