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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//                    Version 2, December 2004
//
// Copyleft (ↄ) meh. <meh@schizofreni.co> | http://meh.schizofreni.co
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
//            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
//   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
//  0. You just DO WHAT THE FUCK YOU WANT TO.

use std::rc::Rc;

use document::Document;
use config::Config;

pub trait Kawaii {
	fn document(&self, config: &Config) -> Rc<Document>;
}

#[cfg(feature = "unstable")]
mod magic {
	use std::rc::Rc;
	use std::fmt;
	use std::intrinsics;

	use document::Document;
	use config::Config;
	use traits::Kawaii;

	default impl<T: 'static> Kawaii for T {
		fn document(&self, _: &Config) -> Rc<Document> {
			use util::*;
			string(unsafe { intrinsics::type_name::<T>() })
		}
	}

	default impl<T: fmt::Debug> Kawaii for T {
		fn document(&self, _: &Config) -> Rc<Document> {
			use util::*;
			string(format!("{:?}", self))
		}
	}
}

impl Kawaii for Rc<Document> {
	fn document(&self, _: &Config) -> Rc<Document> {
		Rc::clone(self)
	}
}

#[macro_export]
macro_rules! integer {
	($name:ty) => (
		impl Kawaii for $name {
			fn document(&self, config: &Config) -> Rc<Document> {
				use config::{Base, Syntax};
				use util::*;

				let string = match config.get::<Base>().map(|b| *b).unwrap_or(Base::default()) {
					Base::Binary =>
						string(format!("{:b}", self)),

					Base::Octal =>
						string(format!("{:o}", self)),

					Base::Decimal =>
						string(format!("{}", self)),

					Base::Hexadecimal =>
						string(format!("{:0X}", self)),
				};

				if let Some(syntax) = config.get::<Syntax>()
					.and_then(|s| s.get("integer").or(s.get("number")))
				{
					style(string, *syntax)
				}
				else {
					string
				}
			}
		}
	);

	($name:ty, $($rest:ty),*) => (
		integer!($name);
		integer!($($rest),*);
	);
}

integer!(u8, i8, u16, i16, u32, i32, u64, i64);

#[macro_export]
macro_rules! float {
	($name:ty) => (
		impl Kawaii for $name {
			fn document(&self, config: &Config) -> Rc<Document> {
				use config::{Precision, Syntax};
				use util::*;

				let string = if let Some(&Precision(size)) = config.get::<Precision>() {
					string(format!("{:.1$}", self, size))
				}
				else {
					string(format!("{}", self))
				};

				if let Some(syntax) = config.get::<Syntax>()
					.and_then(|s| s.get("float").or(s.get("number")))
				{
					style(string, *syntax)
				}
				else {
					string
				}
			}
		}
	);

	($name:ty, $($rest:ty),*) => (
		float!($name);
		float!($($rest),*);
	);
}

float!(f32, f64);

#[macro_export]
macro_rules! string {
	($name:ty) => (
		impl Kawaii for $name {
			fn document(&self, config: &Config) -> Rc<Document> {
				use config::Syntax;
				use util::*;

				let string = string(format!("{:?}", self));

				if let Some(syntax) = config.get::<Syntax>()
					.and_then(|s| s.get("string"))
				{
					style(string, *syntax)
				}
				else {
					string
				}
			}
		}

		impl<'a> Kawaii for &'a $name {
			fn document(&self, config: &Config) -> Rc<Document> {
				(*self).document(config)
			}
		}
	);

	($name:ty, $($rest:ty),*) => (
		string!($name);
		string!($($rest),*);
	);
}

string!(String, str);

//#[macro_export]
//macro_rules! list {
//	($($params:tt)* ; $name:ty) => (
//		impl<$($params)*> Kawaii for $name {
//			fn document(&self, config: &Config) -> Rc<Document> {
//				unimplemented!();
//			}
//		}
//	);
//}
//
//list!(T: Kawaii; Vec<T>);
//list!('a, T: Kawaii; &'a [T]);