Skip to content

Commit

Permalink
feat!: Use Into<FormBuilder> as return type in the form closure
Browse files Browse the repository at this point in the history
  • Loading branch information
lu-zero committed Apr 17, 2023
1 parent 18e0a14 commit 1c6dae9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
43 changes: 38 additions & 5 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,15 +1483,14 @@ where
/// }
/// );
/// ```
pub fn form<F>(mut self, f: F) -> Self
pub fn form<F, R>(mut self, f: F) -> Self
where
F: FnOnce(
FormBuilder<Other, (), <Other::Form as Extendable>::Empty>,
) -> FormBuilder<Other, String, Other::Form>,
F: FnOnce(FormBuilder<Other, (), <Other::Form as Extendable>::Empty>) -> R,
R: Into<FormBuilder<Other, String, Other::Form>>,
{
self.forms
.get_or_insert_with(Default::default)
.push(f(FormBuilder::new()));
.push(f(FormBuilder::new()).into());
self
}
}
Expand Down Expand Up @@ -3977,6 +3976,40 @@ mod tests {
);
}

#[test]
fn simple_into_form() {
struct FormBuilderEx<Other: ExtendableThing, Href, OtherForm>(
FormBuilder<Other, Href, OtherForm>,
);
impl<Other: ExtendableThing, Href, OtherForm> From<FormBuilderEx<Other, Href, OtherForm>>
for FormBuilder<Other, Href, OtherForm>
{
fn from(value: FormBuilderEx<Other, Href, OtherForm>) -> Self {
value.0
}
}

let thing = ThingBuilder::<Nil, _>::new("MyLampThing")
.finish_extend()
.form(|form| FormBuilderEx(form.href("href").op(FormOperation::ReadAllProperties)))
.build()
.unwrap();

assert_eq!(
thing,
Thing {
context: TD_CONTEXT_11.into(),
title: "MyLampThing".to_string(),
forms: Some(vec![Form {
op: DefaultedFormOperations::Custom(vec![FormOperation::ReadAllProperties]),
href: "href".to_string(),
..Default::default()
}]),
..Default::default()
}
);
}

#[test]
fn simple_form_with_uri_variables() {
let thing = ThingBuilder::<Nil, _>::new("MyLampThing")
Expand Down
21 changes: 10 additions & 11 deletions src/builder/affordance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ pub trait BuildableInteractionAffordance<Other: ExtendableThing> {
/// })
/// );
/// ```
fn form<F>(self, f: F) -> Self
fn form<F, R>(self, f: F) -> Self
where
F: FnOnce(
FormBuilder<Other, (), <Other::Form as Extendable>::Empty>,
) -> FormBuilder<Other, String, Other::Form>,
F: FnOnce(FormBuilder<Other, (), <Other::Form as Extendable>::Empty>) -> R,
R: Into<FormBuilder<Other, String, Other::Form>>,
Other::Form: Extendable;

/// Adds a new _URI variable_.
Expand Down Expand Up @@ -311,13 +310,12 @@ where
Other: ExtendableThing,
Other::Form: Extendable,
{
fn form<F>(mut self, f: F) -> Self
fn form<F, R>(mut self, f: F) -> Self
where
F: FnOnce(
FormBuilder<Other, (), <Other::Form as Extendable>::Empty>,
) -> FormBuilder<Other, String, Other::Form>,
F: FnOnce(FormBuilder<Other, (), <Other::Form as Extendable>::Empty>) -> R,
R: Into<FormBuilder<Other, String, Other::Form>>,
{
self.forms.push(f(FormBuilder::new()));
self.forms.push(f(FormBuilder::new()).into());
self
}

Expand Down Expand Up @@ -349,9 +347,10 @@ macro_rules! impl_buildable_interaction_affordance {
where
Other::Form: Extendable
{
fn form<F>(mut self, f: F) -> Self
fn form<F, R>(mut self, f: F) -> Self
where
F: FnOnce(FormBuilder<Other, (), <Other::Form as Extendable>::Empty>) -> FormBuilder<Other, String, Other::Form>,
F: FnOnce(FormBuilder<Other, (), <Other::Form as Extendable>::Empty>) -> R,
R: Into<FormBuilder<Other, String, Other::Form>>,
Other::Form: Extendable,
{
self.$($interaction_path).* = self.$($interaction_path).*.form(f);
Expand Down

0 comments on commit 1c6dae9

Please sign in to comment.