1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::IntoWithContext;

pub trait TryFromWithContext<U, C>: Sized {
	type Error;

	fn try_from_with(value: U, context: &C) -> Result<Self, Self::Error>;
}

impl<T, U: IntoWithContext<T, C>, C> TryFromWithContext<U, C> for T {
	type Error = std::convert::Infallible;

	fn try_from_with(value: U, context: &C) -> Result<Self, Self::Error> {
		Ok(value.into_with(context))
	}
}

pub trait TryIntoWithContext<U, C> {
	type Error;

	fn try_into_with(self, context: &C) -> Result<U, Self::Error>;
}

impl<T, U: TryFromWithContext<T, C>, C> TryIntoWithContext<U, C> for T {
	type Error = U::Error;

	fn try_into_with(self, context: &C) -> Result<U, Self::Error> {
		U::try_from_with(self, context)
	}
}