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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! A small crate to deal with data in context.
//!
//! ```
//! use contextual::{WithContext, AsRefWithContext, DisplayWithContext};
//! use std::fmt;
//!
//! /// Index of an element in some array.
//! pub struct Index(usize);
//!
//! impl<T, C: AsRef<[T]>> AsRefWithContext<T, C> for Index {
//!   fn as_ref_with<'a>(&'a self, context: &'a C) -> &'a T {
//!     &context.as_ref()[self.0]
//!   }
//! }
//!
//! impl<'a, C: AsRef<[&'a str]>> DisplayWithContext<C> for Index {
//!   fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result {
//!     use fmt::Display;
//!     context.as_ref()[self.0].fmt(f)
//!   }
//! }
//!
//! let i = Index(1);
//! let context = ["a", "b", "c"];
//!
//! print!("index: {}", i.with(&context));
//! assert_eq!(*i.with(&context).as_ref(), "b")
//! ```
use std::fmt;

pub struct Contextual<T, C>(pub T, pub C);

pub trait WithContext {
	fn with<C>(&self, context: C) -> Contextual<&Self, C>;

	fn into_with<C>(self, context: C) -> Contextual<Self, C>
	where
		Self: Sized;
}

impl<T: ?Sized> WithContext for T {
	fn with<C>(&self, context: C) -> Contextual<&Self, C> {
		Contextual(self, context)
	}

	fn into_with<C>(self, context: C) -> Contextual<Self, C>
	where
		Self: Sized,
	{
		Contextual(self, context)
	}
}

impl<T, C> std::ops::Deref for Contextual<T, C> {
	type Target = T;

	fn deref(&self) -> &T {
		&self.0
	}
}

impl<T, C> std::ops::DerefMut for Contextual<T, C> {
	fn deref_mut(&mut self) -> &mut T {
		&mut self.0
	}
}

impl<T: DisplayWithContext<C::Target>, C: std::ops::Deref> fmt::Display for Contextual<T, C> {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		self.0.fmt_with(self.1.deref(), f)
	}
}

impl<'a, T: AsRefWithContext<str, C> + ?Sized, C: ?Sized> Contextual<&'a T, &'a C> {
	pub fn as_str(&self) -> &'a str {
		self.0.as_ref_with(self.1)
	}
}

impl<'a, T: AsRefWithContext<str, C> + ?Sized, C: ?Sized> Contextual<&'a T, &'a mut C> {
	pub fn as_str(&self) -> &str {
		self.0.as_ref_with(self.1)
	}
}

impl<'a, T: IntoRefWithContext<'a, str, C>, C: ?Sized> Contextual<T, &'a C> {
	pub fn into_str(self) -> &'a str {
		self.0.into_ref_with(self.1)
	}
}

impl<'a, T: IntoRefWithContext<'a, str, C>, C: ?Sized> Contextual<T, &'a mut C> {
	pub fn into_str(self) -> &'a str {
		self.0.into_ref_with(self.1)
	}
}

impl<'t, 'c, T: AsRefWithContext<U, C> + ?Sized, U: ?Sized, C> AsRef<U>
	for Contextual<&'t T, &'c C>
{
	fn as_ref(&self) -> &U {
		self.0.as_ref_with(self.1)
	}
}

impl<'t, 'c, T: AsRefWithContext<U, C> + ?Sized, U: ?Sized, C> AsRef<U>
	for Contextual<&'t T, &'c mut C>
{
	fn as_ref(&self) -> &U {
		self.0.as_ref_with(self.1)
	}
}

pub trait DisplayWithContext<C: ?Sized> {
	fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result;
}

impl<'a, T: DisplayWithContext<C> + ?Sized, C> DisplayWithContext<C> for &'a T {
	fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result {
		T::fmt_with(*self, context, f)
	}
}

impl<'a, T: DisplayWithContext<C> + std::borrow::ToOwned + ?Sized, C> DisplayWithContext<C>
	for std::borrow::Cow<'a, T>
{
	fn fmt_with(&self, context: &C, f: &mut fmt::Formatter) -> fmt::Result {
		T::fmt_with(self, context, f)
	}
}

pub trait AsRefWithContext<U: ?Sized, C: ?Sized> {
	fn as_ref_with<'a>(&'a self, context: &'a C) -> &'a U;
}

pub trait IntoRefWithContext<'a, U: ?Sized, C: ?Sized> {
	fn into_ref_with(self, context: &'a C) -> &'a U;
}