Diffstat (limited to 'src/transform.rs')
| -rw-r--r-- | src/transform.rs | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/transform.rs b/src/transform.rs new file mode 100644 index 0000000..12f7576 --- /dev/null +++ b/src/transform.rs @@ -0,0 +1,86 @@ +use crate::{ + build::{Build, Builder}, + walk::{Walk, WalkMut, WalkOnce}, + UniError, Walker, +}; + +pub fn from<'value, 'ctx: 'value, U, T, VisitorErr>( + value: T, +) -> Result<U, UniError<<T as WalkOnce<'value, 'ctx, VisitorErr>>::ErrorOnce, VisitorErr>> +where + U: Build< + 'value, + 'ctx, + <T as WalkOnce<'value, 'ctx, VisitorErr>>::ErrorOnce, + Error = VisitorErr, + >, + T: WalkOnce<'value, 'ctx, VisitorErr>, +{ + build_from::<U::Builder, _, _>(value) +} + +pub fn from_mut<'value, 'ctx, U, T, VisitorErr>( + value: &'value mut T, +) -> Result<U, UniError<<T as WalkMut<'value, 'ctx, VisitorErr>>::ErrorMut, VisitorErr>> +where + U: Build<'value, 'ctx, <T as WalkMut<'value, 'ctx, VisitorErr>>::ErrorMut, Error = VisitorErr>, + T: WalkMut<'value, 'ctx, VisitorErr>, +{ + build_from_mut::<U::Builder, _, _>(value) +} + +pub fn from_ref<'value, 'ctx: 'value, U, T, VisitorErr>( + value: &'value T, +) -> Result<U, UniError<<T as Walk<'value, 'ctx, VisitorErr>>::Error, VisitorErr>> +where + U: Build<'value, 'ctx, <T as Walk<'value, 'ctx, VisitorErr>>::Error, Error = VisitorErr>, + T: Walk<'value, 'ctx, VisitorErr>, +{ + build_from_ref::<U::Builder, _, _>(value) +} + +pub fn build_from<'value, 'ctx: 'value, B, T, VisitorErr>( + value: T, +) -> Result<B::Value, UniError<<T as WalkOnce<'value, 'ctx, VisitorErr>>::ErrorOnce, VisitorErr>> +where + B: Builder< + 'value, + 'ctx, + <T as WalkOnce<'value, 'ctx, VisitorErr>>::ErrorOnce, + Error = VisitorErr, + >, + T: WalkOnce<'value, 'ctx, VisitorErr>, +{ + let mut builder = B::init(); + value.into_walker().walk(builder.as_visitor())?; + builder.finish() +} + +pub fn build_from_mut<'value, 'ctx: 'value, B, T, VisitorErr>( + value: &'value mut T, +) -> Result<B::Value, UniError<<T as WalkMut<'value, 'ctx, VisitorErr>>::ErrorMut, VisitorErr>> +where + B: Builder< + 'value, + 'ctx, + <T as WalkMut<'value, 'ctx, VisitorErr>>::ErrorMut, + Error = VisitorErr, + >, + T: WalkMut<'value, 'ctx, VisitorErr>, +{ + let mut builder = B::init(); + value.walker_mut().walk(builder.as_visitor())?; + builder.finish() +} + +pub fn build_from_ref<'value, 'ctx: 'value, B, T, VisitorErr>( + value: &'value T, +) -> Result<B::Value, UniError<<T as Walk<'value, 'ctx, VisitorErr>>::Error, VisitorErr>> +where + B: Builder<'value, 'ctx, <T as Walk<'value, 'ctx, VisitorErr>>::Error, Error = VisitorErr>, + T: Walk<'value, 'ctx, VisitorErr>, +{ + let mut builder = B::init(); + value.walker().walk(builder.as_visitor())?; + builder.finish() +} |