sonnerie_api/
types.rs

1
2use ::std::io::Write;
3
4/// This trait is implemented for the numeric types
5/// that Sonnerie supports.
6///
7/// Sonnerie's API accepts this type in places
8/// where any column value is acceptable. (The
9/// server checks for compatibility)
10pub trait FromValue
11{
12	fn serialize(&self, to: &mut Write)
13		-> ::std::io::Result<()>;
14}
15
16impl FromValue for f64
17{
18	fn serialize(&self, to: &mut Write)
19		-> ::std::io::Result<()>
20	{
21		write!(to, "{:.17}", self)?;
22		Ok(())
23	}
24}
25
26impl FromValue for f32
27{
28	fn serialize(&self, to: &mut Write)
29		-> ::std::io::Result<()>
30	{
31		write!(to, "{:.17}", self)?;
32		Ok(())
33	}
34}
35
36impl FromValue for u64
37{
38	fn serialize(&self, to: &mut Write)
39		-> ::std::io::Result<()>
40	{
41		write!(to, "{}", self)?;
42		Ok(())
43	}
44}
45
46impl FromValue for u32
47{
48	fn serialize(&self, to: &mut Write)
49		-> ::std::io::Result<()>
50	{
51		write!(to, "{}", self)?;
52		Ok(())
53	}
54}
55
56impl FromValue for i64
57{
58	fn serialize(&self, to: &mut Write)
59		-> ::std::io::Result<()>
60	{
61		write!(to, "{}", self)?;
62		Ok(())
63	}
64}
65
66impl FromValue for i32
67{
68	fn serialize(&self, to: &mut Write)
69		-> ::std::io::Result<()>
70	{
71		write!(to, "{}", self)?;
72		Ok(())
73	}
74}
75
76/// This is a reference to a column's value.
77/// You can call `from` to convert it to a
78/// concrete type.
79pub struct Column<'v>
80{
81	pub(crate) serialized: &'v str,
82}
83
84impl<'v> Column<'v>
85{
86	/// Convert to an inferred concrete (numeric) type
87	///
88	/// Or return None if the conversion could not be done.
89	pub fn from_checked<Type: ToValue>(&self)
90		-> Option<Type>
91	{
92		Type::from_checked(self.serialized)
93	}
94
95	/// Convert to an inferred concrete (numeric) type
96	///
97	/// Panic if the type cannot be converted
98	pub fn from<Type: ToValue>(&self)
99		-> Type
100	{
101		self.from_checked().unwrap()
102	}
103	pub(crate) fn copy(&self) -> OwnedColumn
104	{
105		OwnedColumn
106		{
107			serialized: self.serialized.to_owned(),
108		}
109	}
110}
111
112impl<'v> ::std::fmt::Display for Column<'v>
113{
114	fn fmt(&self, f: &mut ::std::fmt::Formatter)
115		-> ::std::fmt::Result
116	{
117		write!(f, "{}", self.serialized)
118	}
119}
120
121
122/// Same as `Column`, except can be moved
123/// and is heavier weight.
124#[derive(Debug,Clone)]
125pub struct OwnedColumn
126{
127	pub(crate) serialized: String,
128}
129
130impl ::std::fmt::Display for OwnedColumn
131{
132	fn fmt(&self, f: &mut ::std::fmt::Formatter)
133		-> ::std::fmt::Result
134	{
135		write!(f, "{}", self.serialized)
136	}
137}
138
139impl OwnedColumn
140{
141	pub fn from_checked<Type: ToValue>(&self)
142		-> Option<Type>
143	{
144		Type::from_checked(&self.serialized)
145	}
146	pub fn from<Type: ToValue>(&self)
147		-> Type
148	{
149		self.from_checked().unwrap()
150	}
151}
152
153pub trait ToValue: Sized
154{
155	fn from_checked(serialifrzed: &str)
156		-> Option<Self>;
157	fn from(serialized: &str) -> Self
158	{
159		Self::from_checked(serialized).unwrap()
160	}
161}
162
163impl ToValue for f64
164{
165	fn from_checked(serialized: &str)
166		-> Option<Self>
167	{
168		serialized.parse().ok()
169	}
170}
171
172impl ToValue for f32
173{
174	fn from_checked(serialized: &str)
175		-> Option<Self>
176	{
177		serialized.parse().ok()
178	}
179}
180
181impl ToValue for u64
182{
183	fn from_checked(serialized: &str)
184		-> Option<Self>
185	{
186		serialized.parse().ok()
187	}
188}
189
190impl ToValue for u32
191{
192	fn from_checked(serialized: &str)
193		-> Option<Self>
194	{
195		serialized.parse().ok()
196	}
197}
198
199
200impl ToValue for i64
201{
202	fn from_checked(serialized: &str)
203		-> Option<Self>
204	{
205		serialized.parse().ok()
206	}
207}
208
209impl ToValue for i32
210{
211	fn from_checked(serialized: &str)
212		-> Option<Self>
213	{
214		serialized.parse().ok()
215	}
216}