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
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(Store)]
pub fn store(input: TokenStream) -> TokenStream {
	// Parse the token stream
	let input = parse_macro_input!(input as DeriveInput);
	// Fetch the struct name
	let name = &input.ident;
	// Add derived implementations
	let output = quote! {

		impl From<Vec<u8>> for #name {
			fn from(v: Vec<u8>) -> Self {
				Self::from(&v)
			}
		}

		impl From<#name> for Vec<u8> {
			fn from(v: #name) -> Vec<u8> {
				Self::from(&v)
			}
		}

		impl From<&#name> for Vec<u8> {
			fn from(v: &#name) -> Vec<u8> {
				let mut out:Vec<u8> = vec![];
				revision::Revisioned::serialize_revisioned(v, &mut out).unwrap();
				out
			}
		}

		impl From<&Vec<u8>> for #name {
			fn from(v: &Vec<u8>) -> Self {
				revision::Revisioned::deserialize_revisioned(&mut v.as_slice()).unwrap()
			}
		}

		// TODO: for a future pull request

		// impl TryFrom<&#name> for Vec<u8> {
		// 	type Error = crate::err::Error;
		// 	fn try_from(v: &#name) -> Result<Self, Self::Error> {
		// 		let mut out:Vec<u8> = vec![];
		// 		revision::Revisioned::serialize_revisioned(v, &mut out)?;
		// 		Ok(out)
		// 	}
		// }

		// impl TryFrom<&Vec<u8>> for #name {
		// 	type Error = crate::err::Error;
		// 	fn try_from(v: &Vec<u8>) -> Result<Self, Self::Error> {
		// 		revision::Revisioned::deserialize_revisioned(&mut v.as_slice())
		// 	}
		// }

	};
	//
	output.into()
}

#[proc_macro_derive(Key)]
pub fn key(input: TokenStream) -> TokenStream {
	// Parse the token stream
	let input = parse_macro_input!(input as DeriveInput);
	// Fetch the struct name
	let name = &input.ident;
	// Compute the generics
	let generics = input.generics;
	let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
	assert!(generics.lifetimes().count() <= 1);
	let (lifetime, from_owned) = if let Some(lifetime_def) = generics.lifetimes().next() {
		let lifetime = &lifetime_def.lifetime;
		(quote! {#lifetime}, quote! {})
	} else {
		(
			quote! {},
			quote! {
				impl #impl_generics From<Vec<u8>> for #name #ty_generics #where_clause {
					fn from(v: Vec<u8>) -> Self {
						Self::decode(&v).unwrap()
					}
				}
			},
		)
	};

	// Generate the output
	let output = quote! {

		impl #impl_generics From<#name #ty_generics> for Vec<u8> #where_clause {
			fn from(v: #name #ty_generics) -> Vec<u8> {
				v.encode().unwrap_or_default()
			}
		}

		impl #impl_generics From<&#name #ty_generics> for Vec<u8> #where_clause {
			fn from(v: &#name #ty_generics) -> Vec<u8> {
				v.encode().unwrap_or_default()
			}
		}

		#from_owned

		impl #impl_generics From<&#lifetime Vec<u8>> for #name #ty_generics #where_clause {
			fn from(v: &#lifetime Vec<u8>) -> Self {
				Self::decode(v).unwrap()
			}
		}

		impl #impl_generics #name #ty_generics #where_clause {

			pub fn encode(&self) -> Result<Vec<u8>, crate::err::Error> {
				let v = storekey::serialize(self);
				Ok(v?)
			}

			pub fn decode(v: &#lifetime[u8]) -> Result<Self, crate::err::Error> {
				let v = storekey::deserialize(v);
				Ok(v?)
			}

		}

	};

	output.into()
}