changeset 32:132e04b0655d

Clean up dispatch code in type parser
author Lewin Bormann <lbo@spheniscida.de>
date Sat, 17 Oct 2020 23:50:45 +0200
parents 122391a6ee26
children d1a1a533c6be
files drive_example/src/drive_v3_types.rs generate/generate.py
diffstat 2 files changed, 45 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/drive_example/src/drive_v3_types.rs	Sat Oct 17 23:43:18 2020 +0200
+++ b/drive_example/src/drive_v3_types.rs	Sat Oct 17 23:50:45 2020 +0200
@@ -165,7 +165,7 @@
     #[serde(rename = "teamDriveId")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub team_drive_id: Option<String>,
-    /// The time of this change (RFC 3339 date-time).
+    /// DateTime: The time of this change (RFC 3339 date-time).
     #[serde(rename = "time")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub time: Option<DateTime<Utc>>,
@@ -264,7 +264,7 @@
     #[serde(rename = "content")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub content: Option<String>,
-    /// The time at which the comment was created (RFC 3339 date-time).
+    /// DateTime: The time at which the comment was created (RFC 3339 date-time).
     #[serde(rename = "createdTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub created_time: Option<DateTime<Utc>>,
@@ -284,7 +284,7 @@
     #[serde(rename = "kind")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub kind: Option<String>,
-    /// The last time the comment or any of its replies was modified (RFC 3339 date-time).
+    /// DateTime: The last time the comment or any of its replies was modified (RFC 3339 date-time).
     #[serde(rename = "modifiedTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub modified_time: Option<DateTime<Utc>>,
@@ -331,7 +331,7 @@
     #[serde(rename = "restrictingUser")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub restricting_user: Option<User>,
-    /// The time at which the content restriction was set (formatted RFC 3339 timestamp). Only populated if readOnly is true.
+    /// DateTime: The time at which the content restriction was set (formatted RFC 3339 timestamp). Only populated if readOnly is true.
     #[serde(rename = "restrictionTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub restriction_time: Option<DateTime<Utc>>,
@@ -475,7 +475,7 @@
     #[serde(rename = "colorRgb")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub color_rgb: Option<String>,
-    /// The time at which the shared drive was created (RFC 3339 date-time).
+    /// DateTime: The time at which the shared drive was created (RFC 3339 date-time).
     #[serde(rename = "createdTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub created_time: Option<DateTime<Utc>>,
@@ -839,7 +839,7 @@
     #[serde(rename = "copyRequiresWriterPermission")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub copy_requires_writer_permission: Option<bool>,
-    /// The time at which the file was created (RFC 3339 date-time).
+    /// DateTime: The time at which the file was created (RFC 3339 date-time).
     #[serde(rename = "createdTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub created_time: Option<DateTime<Utc>>,
@@ -918,11 +918,11 @@
     #[serde(rename = "modifiedByMe")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub modified_by_me: Option<bool>,
-    /// The last time the file was modified by the user (RFC 3339 date-time).
+    /// DateTime: The last time the file was modified by the user (RFC 3339 date-time).
     #[serde(rename = "modifiedByMeTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub modified_by_me_time: Option<DateTime<Utc>>,
-    /// The last time the file was modified by anyone (RFC 3339 date-time). Note that setting modifiedTime will also update modifiedByMeTime for the user.
+    /// DateTime: The last time the file was modified by anyone (RFC 3339 date-time). Note that setting modifiedTime will also update modifiedByMeTime for the user.
     #[serde(rename = "modifiedTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub modified_time: Option<DateTime<Utc>>,
@@ -966,7 +966,7 @@
     #[serde(rename = "shared")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub shared: Option<bool>,
-    /// The time at which the file was shared with the user, if applicable (RFC 3339 date-time).
+    /// DateTime: The time at which the file was shared with the user, if applicable (RFC 3339 date-time).
     #[serde(rename = "sharedWithMeTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub shared_with_me_time: Option<DateTime<Utc>>,
@@ -1005,7 +1005,7 @@
     #[serde(rename = "trashed")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub trashed: Option<bool>,
-    /// The time that the item was trashed (RFC 3339 date-time). Only populated for items in shared drives.
+    /// DateTime: The time that the item was trashed (RFC 3339 date-time). Only populated for items in shared drives.
     #[serde(rename = "trashedTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub trashed_time: Option<DateTime<Utc>>,
@@ -1024,7 +1024,7 @@
     #[serde(rename = "viewedByMe")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub viewed_by_me: Option<bool>,
-    /// The last time the file was viewed by the user (RFC 3339 date-time).
+    /// DateTime: The last time the file was viewed by the user (RFC 3339 date-time).
     #[serde(rename = "viewedByMeTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub viewed_by_me_time: Option<DateTime<Utc>>,
@@ -1144,7 +1144,7 @@
     #[serde(rename = "emailAddress")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub email_address: Option<String>,
-    /// The time at which this permission will expire (RFC 3339 date-time). Expiration times have the following restrictions:   - They can only be set on user and group permissions  - The time must be in the future  - The time cannot be more than a year in the future
+    /// DateTime: The time at which this permission will expire (RFC 3339 date-time). Expiration times have the following restrictions:   - They can only be set on user and group permissions  - The time must be in the future  - The time cannot be more than a year in the future
     #[serde(rename = "expirationTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub expiration_time: Option<DateTime<Utc>>,
@@ -1211,7 +1211,7 @@
     #[serde(rename = "content")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub content: Option<String>,
-    /// The time at which the reply was created (RFC 3339 date-time).
+    /// DateTime: The time at which the reply was created (RFC 3339 date-time).
     #[serde(rename = "createdTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub created_time: Option<DateTime<Utc>>,
@@ -1231,7 +1231,7 @@
     #[serde(rename = "kind")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub kind: Option<String>,
-    /// The last time the reply was modified (RFC 3339 date-time).
+    /// DateTime: The last time the reply was modified (RFC 3339 date-time).
     #[serde(rename = "modifiedTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub modified_time: Option<DateTime<Utc>>,
@@ -1282,7 +1282,7 @@
     #[serde(rename = "mimeType")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub mime_type: Option<String>,
-    /// The last time the revision was modified (RFC 3339 date-time).
+    /// DateTime: The last time the revision was modified (RFC 3339 date-time).
     #[serde(rename = "modifiedTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub modified_time: Option<DateTime<Utc>>,
@@ -1478,7 +1478,7 @@
     #[serde(rename = "colorRgb")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub color_rgb: Option<String>,
-    /// The time at which the Team Drive was created (RFC 3339 date-time).
+    /// DateTime: The time at which the Team Drive was created (RFC 3339 date-time).
     #[serde(rename = "createdTime")]
     #[serde(skip_serializing_if = "Option::is_none")]
     pub created_time: Option<DateTime<Utc>>,
--- a/generate/generate.py	Sat Oct 17 23:43:18 2020 +0200
+++ b/generate/generate.py	Sat Oct 17 23:50:45 2020 +0200
@@ -107,49 +107,62 @@
                 else:
                     typ = field
                 return (optionalize('HashMap<String,' + typ + '>', optional), schema.get('description', '')), structs
+
         if schema['type'] == 'array':
             typ, substructs = parse_schema_types(name, schema['items'], optional=False)
             if type(typ) is tuple:
                 typ = typ[0]
             return (optionalize('Vec<' + typ + '>', optional), schema.get('description', '')), structs + substructs
+
         if schema['type'] == 'string':
+
+            def build(intt, typ='String'):
+                return (optionalize(typ, optional), intt + ': ' + schema.get('description', '')), structs
+
             if 'format' in schema:
                 if schema['format'] == 'int64':
-                    return (optionalize('String', optional), 'i64: ' + schema.get('description', '')), structs
+                    return build('i64')
                 if schema['format'] == 'int32':
-                    return (optionalize('String', optional), 'i32: ' + schema.get('description', '')), structs
+                    return build('i32')
                 if schema['format'] == 'uint64':
-                    return (optionalize('String', optional), 'u64: ' + schema.get('description', '')), structs
+                    return build('u64')
                 if schema['format'] == 'uint32':
-                    return (optionalize('String', optional), 'u32: ' + schema.get('description', '')), structs
+                    return build('u32')
                 if schema['format'] == 'double':
-                    return (optionalize('String', optional), 'f64: ' + schema.get('description', '')), structs
+                    return build('f64')
                 if schema['format'] == 'float':
-                    return (optionalize('String', optional), 'f32: ' + schema.get('description', '')), structs
+                    return build('f32')
                 if schema['format'] == 'date-time':
-                    return (optionalize('DateTime<Utc>', optional), schema.get('description', '')), structs
+                    return build('DateTime', typ='DateTime<Utc>')
             return (optionalize('String', optional), schema.get('description', '')), structs
+
         if schema['type'] == 'boolean':
             return (optionalize('bool', optional), schema.get('description', '')), structs
+
         if schema['type'] in ('number', 'integer'):
+
+            def build(intt):
+                return (optionalize(intt, optional), schema.get('description', '')), structs
+
             if schema['format'] == 'float':
-                return (optionalize('f32', optional), schema.get('description', '')), structs
+                return build('f32')
             if schema['format'] == 'double':
-                return (optionalize('f64', optional), schema.get('description', '')), structs
+                return build('f64')
             if schema['format'] == 'int32':
-                return (optionalize('i32', optional), schema.get('description', '')), structs
+                return build('i32')
             if schema['format'] == 'int64':
-                return (optionalize('i64', optional), schema.get('description', '')), structs
+                return build('i64')
             if schema['format'] == 'uint32':
-                return (optionalize('u32', optional), schema.get('description', '')), structs
+                return build('u32')
             if schema['format'] == 'uint64':
-                return (optionalize('u64', optional), schema.get('description', '')), structs
+                return build('u64')
+
         if schema['type'] == 'any':
             return (optionalize('String', optional), 'ANY data: ' + schema.get('description', '')), structs
-        raise Exception('unimplemented!', name, schema)
+
+        raise Exception('unimplemented schema type!', name, schema)
     except KeyError as e:
-        print(name, schema)
-        print(e)
+        print('KeyError while processing:', name, schema)
         raise e