|
@@ -5,7 +5,7 @@ pub struct CreateDeserializer<'de> {
|
|
|
table_name: Option<&'static str>,
|
|
|
column_names: Option<&'static [&'static str]>,
|
|
|
column_types: Vec<String>,
|
|
|
- _de: std::marker::PhantomData<&'de u8>
|
|
|
+ _de: std::marker::PhantomData<&'de u8>,
|
|
|
}
|
|
|
|
|
|
impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut CreateDeserializer<'de> {
|
|
@@ -36,9 +36,13 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut CreateDeserializer<'de> {
|
|
|
self.column_types.push("varchar".to_owned());
|
|
|
v.visit_string("".to_owned())
|
|
|
}
|
|
|
-
|
|
|
|
|
|
- fn deserialize_struct<V: Visitor<'de>>(self, name: &'static str, fields: &'static [&'static str], v: V) -> Result<V::Value, Self::Error> {
|
|
|
+ fn deserialize_struct<V: Visitor<'de>>(
|
|
|
+ self,
|
|
|
+ name: &'static str,
|
|
|
+ fields: &'static [&'static str],
|
|
|
+ v: V,
|
|
|
+ ) -> Result<V::Value, Self::Error> {
|
|
|
self.table_name = Some(name);
|
|
|
self.column_names = Some(fields);
|
|
|
v.visit_seq(self)
|
|
@@ -47,46 +51,69 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut CreateDeserializer<'de> {
|
|
|
|
|
|
impl<'de> serde::de::SeqAccess<'de> for CreateDeserializer<'de> {
|
|
|
type Error = super::ModelError;
|
|
|
-
|
|
|
- fn next_element_seed<T: serde::de::DeserializeSeed<'de>>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error> {
|
|
|
+
|
|
|
+ fn next_element_seed<T: serde::de::DeserializeSeed<'de>>(
|
|
|
+ &mut self,
|
|
|
+ seed: T,
|
|
|
+ ) -> Result<Option<T::Value>, Self::Error> {
|
|
|
seed.deserialize(self).map(Some)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pub fn sql_for<'de, T: crate::model::Entity>() -> (String,String) {
|
|
|
- let mut cd = CreateDeserializer { table_name: None, column_names: None, column_types: Vec::new(), _de: std::marker::PhantomData{} };
|
|
|
+pub fn sql_for<'de, T: crate::model::Entity>() -> (String, String) {
|
|
|
+ let mut cd = CreateDeserializer {
|
|
|
+ table_name: None,
|
|
|
+ column_names: None,
|
|
|
+ column_types: Vec::new(),
|
|
|
+ _de: std::marker::PhantomData {},
|
|
|
+ };
|
|
|
|
|
|
T::deserialize(&mut cd).expect("SQL creation failed!");
|
|
|
|
|
|
(
|
|
|
- format!("DROP TABLE IF EXISTS {}", <T as crate::model::Entity>::table_name()),
|
|
|
- format!("CREATE TABLE {} ({})",
|
|
|
+ format!(
|
|
|
+ "DROP TABLE IF EXISTS {}",
|
|
|
+ <T as crate::model::Entity>::table_name()
|
|
|
+ ),
|
|
|
+ format!(
|
|
|
+ "CREATE TABLE {} ({})",
|
|
|
<T as crate::model::Entity>::table_name(),
|
|
|
- cd.column_names.unwrap().iter().zip(cd.column_types.iter())
|
|
|
- .map(|(n,t)| n.to_string() + " " + t).collect::<Vec<_>>().join(",")
|
|
|
- )
|
|
|
+ cd.column_names
|
|
|
+ .unwrap()
|
|
|
+ .iter()
|
|
|
+ .zip(cd.column_types.iter())
|
|
|
+ .map(|(n, t)| n.to_string() + " " + t)
|
|
|
+ .collect::<Vec<_>>()
|
|
|
+ .join(",")
|
|
|
+ ),
|
|
|
)
|
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
- #[derive(serde::Serialize,serde::Deserialize,crate::Entity)]
|
|
|
+ #[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
|
struct Empty {}
|
|
|
|
|
|
- #[derive(serde::Serialize,serde::Deserialize,crate::Entity)]
|
|
|
+ #[derive(serde::Serialize, serde::Deserialize, crate::Entity)]
|
|
|
struct Single {
|
|
|
- e: i32
|
|
|
+ e: i32,
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
|
fn example_sql_for() {
|
|
|
assert_eq!(
|
|
|
super::sql_for::<Empty>(),
|
|
|
- ("DROP TABLE IF EXISTS empty".to_owned(), "CREATE TABLE empty ()".to_owned())
|
|
|
+ (
|
|
|
+ "DROP TABLE IF EXISTS empty".to_owned(),
|
|
|
+ "CREATE TABLE empty ()".to_owned()
|
|
|
+ )
|
|
|
);
|
|
|
assert_eq!(
|
|
|
super::sql_for::<Single>(),
|
|
|
- ("DROP TABLE IF EXISTS single".to_owned(), "CREATE TABLE single (e integer)".to_owned())
|
|
|
+ (
|
|
|
+ "DROP TABLE IF EXISTS single".to_owned(),
|
|
|
+ "CREATE TABLE single (e integer)".to_owned()
|
|
|
+ )
|
|
|
);
|
|
|
}
|
|
|
}
|