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
use std::{any::TypeId, borrow::Cow, marker::PhantomData};

use super::{Error, SmallVec};

pub trait Scope: Send + Sync + 'static {
	type Parent: Scope;
	type Descriptor<T>: Descriptor<T>;
	const NAME: &'static str;
	const FULLNAME: &'static str;
}

pub trait Descriptor<T> {
	fn name(v: &T) -> Option<Cow<str>>;
	fn summary(v: &T) -> Option<Cow<str>>;
	fn detail(v: &T) -> Option<Cow<str>>;
	fn inner(v: &T) -> Option<SmallVec<[&Error; 1]>>;
	fn type_id(v: &T) -> Option<TypeId>;
}

#[marker]
pub trait DescriptableMark<T>
where
	T: ?Sized,
{
}

pub(crate) trait Descriptee: Send + Sync {
	fn name(&self) -> Option<Cow<str>>;
	fn summary(&self) -> Option<Cow<str>>;
	fn detail(&self) -> Option<Cow<str>>;
	fn inner(&self) -> Option<SmallVec<[&Error; 1]>>;
	fn type_id(&self) -> Option<TypeId>;
}

#[repr(transparent)]
pub(crate) struct Dispatcher<T, S> {
	pub(crate) data: T,
	_p: PhantomData<S>,
}

impl<T, S> Dispatcher<T, S>
where
	S: Scope,
{
	pub fn new(data: T) -> Self {
		Self {
			data,
			_p: PhantomData,
		}
	}
}

impl<T, S> Descriptee for Dispatcher<T, S>
where
	T: Send + Sync,
	S: Scope,
{
	default fn name(&self) -> Option<Cow<str>> {
		<<S as Scope>::Descriptor<T> as Descriptor<T>>::name(&self.data)
	}

	default fn summary(&self) -> Option<Cow<str>> {
		<<S as Scope>::Descriptor<T> as Descriptor<T>>::summary(&self.data)
	}

	default fn detail(&self) -> Option<Cow<str>> {
		<<S as Scope>::Descriptor<T> as Descriptor<T>>::detail(&self.data)
	}

	default fn inner(&self) -> Option<SmallVec<[&Error; 1]>> {
		<<S as Scope>::Descriptor<T> as Descriptor<T>>::inner(&self.data)
	}

	default fn type_id(&self) -> Option<TypeId> {
		<<S as Scope>::Descriptor<T> as Descriptor<T>>::type_id(&self.data)
	}
}