-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
690 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "schema" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
serde = { version = "1.0", features = ["derive"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# JSON Schema | ||
|
||
Schema used in benches, copied from `https://github.com/serde-rs/json-benchmark`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Permission is hereby granted, free of charge, to any | ||
person obtaining a copy of this software and associated | ||
documentation files (the "Software"), to deal in the | ||
Software without restriction, including without | ||
limitation the rights to use, copy, modify, merge, | ||
publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software | ||
is furnished to do so, subject to the following | ||
conditions: | ||
|
||
The above copyright notice and this permission notice | ||
shall be included in all copies or substantial portions | ||
of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | ||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED | ||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | ||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR | ||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::collections::BTreeMap as Map; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::enum_str; | ||
|
||
pub type Canada = FeatureCollection; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct FeatureCollection { | ||
#[serde(rename = "type")] | ||
pub obj_type: ObjType, | ||
pub features: Vec<Feature>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct Feature { | ||
#[serde(rename = "type")] | ||
pub obj_type: ObjType, | ||
pub properties: Map<String, String>, | ||
pub geometry: Geometry, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields)] | ||
pub struct Geometry { | ||
#[serde(rename = "type")] | ||
pub obj_type: ObjType, | ||
pub coordinates: Vec<Vec<(Latitude, Longitude)>>, | ||
} | ||
|
||
pub type Latitude = f32; | ||
pub type Longitude = f32; | ||
|
||
enum_str!(ObjType { | ||
FeatureCollection("FeatureCollection"), | ||
Feature("Feature"), | ||
Polygon("Polygon"), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::collections::BTreeMap as Map; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{empty, prim_str::PrimStr}; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct CitmCatalog { | ||
pub area_names: Map<IdStr, String>, | ||
pub audience_sub_category_names: Map<IdStr, String>, | ||
pub block_names: Map<IdStr, String>, | ||
pub events: Map<IdStr, Event>, | ||
pub performances: Vec<Performance>, | ||
pub seat_category_names: Map<IdStr, String>, | ||
pub sub_topic_names: Map<IdStr, String>, | ||
pub subject_names: Map<IdStr, String>, | ||
pub topic_names: Map<IdStr, String>, | ||
pub topic_sub_topics: Map<IdStr, Vec<Id>>, | ||
pub venue_names: Map<String, String>, | ||
} | ||
|
||
pub type Id = u32; | ||
pub type IdStr = PrimStr<u32>; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct Event { | ||
pub description: (), | ||
pub id: Id, | ||
pub logo: Option<String>, | ||
pub name: String, | ||
pub sub_topic_ids: Vec<Id>, | ||
pub subject_code: (), | ||
pub subtitle: (), | ||
pub topic_ids: Vec<Id>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct Performance { | ||
pub event_id: Id, | ||
pub id: Id, | ||
pub logo: Option<String>, | ||
pub name: (), | ||
pub prices: Vec<Price>, | ||
pub seat_categories: Vec<SeatCategory>, | ||
pub seat_map_image: (), | ||
pub start: u64, | ||
pub venue_code: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct Price { | ||
pub amount: u32, | ||
pub audience_sub_category_id: Id, | ||
pub seat_category_id: Id, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct SeatCategory { | ||
pub areas: Vec<Area>, | ||
pub seat_category_id: Id, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
#[serde(deny_unknown_fields, rename_all = "camelCase")] | ||
pub struct Area { | ||
pub area_id: Id, | ||
pub block_ids: empty::Array, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use std::{fmt, mem::MaybeUninit, ptr, slice, str}; | ||
|
||
use serde::{ | ||
de::{self, Deserialize, Deserializer, Unexpected}, | ||
ser::{Serialize, Serializer}, | ||
}; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct Color(u32); | ||
|
||
const HEX_LUT: &[u8] = b"\ | ||
000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F\ | ||
202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F\ | ||
404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F\ | ||
606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F\ | ||
808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F\ | ||
A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF\ | ||
C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF\ | ||
E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"; | ||
|
||
impl Color { | ||
fn as_str(self, buf: &mut MaybeUninit<[u8; 6]>) -> &str { | ||
let buf_len = 6; | ||
let buf_ptr = buf.as_mut_ptr() as *mut u8; | ||
let lut_ptr = HEX_LUT.as_ptr(); | ||
|
||
let r = ((self.0 & 0xFF0000) >> 15) as isize; | ||
let g = ((self.0 & 0x00FF00) >> 7) as isize; | ||
let b = ((self.0 & 0x0000FF) << 1) as isize; | ||
|
||
unsafe { | ||
ptr::copy_nonoverlapping(lut_ptr.offset(r), buf_ptr, 2); | ||
ptr::copy_nonoverlapping(lut_ptr.offset(g), buf_ptr.offset(2), 2); | ||
ptr::copy_nonoverlapping(lut_ptr.offset(b), buf_ptr.offset(4), 2); | ||
|
||
str::from_utf8(slice::from_raw_parts(buf_ptr, buf_len)).unwrap() | ||
} | ||
} | ||
} | ||
|
||
impl Serialize for Color { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let mut buf = MaybeUninit::uninit(); | ||
serializer.serialize_str(self.as_str(&mut buf)) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Color { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct Visitor; | ||
|
||
impl<'de> de::Visitor<'de> for Visitor { | ||
type Value = Color; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("color string") | ||
} | ||
|
||
fn visit_str<E>(self, value: &str) -> Result<Color, E> | ||
where | ||
E: de::Error, | ||
{ | ||
match u32::from_str_radix(value, 16) { | ||
Ok(hex) => Ok(Color(hex)), | ||
Err(_) => Err(E::invalid_value(Unexpected::Str(value), &self)), | ||
} | ||
} | ||
} | ||
|
||
deserializer.deserialize_str(Visitor) | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_color() { | ||
let mut buf = MaybeUninit::uninit(); | ||
let string = Color(0xA0A0A0).as_str(&mut buf); | ||
assert_eq!(string, "A0A0A0"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::fmt; | ||
|
||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct Array; | ||
|
||
impl Serialize for Array { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
[(); 0].serialize(serializer) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for Array { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct Visitor; | ||
|
||
impl<'de> de::Visitor<'de> for Visitor { | ||
type Value = Array; | ||
|
||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { | ||
formatter.write_str("empty array") | ||
} | ||
|
||
fn visit_seq<V>(self, _: V) -> Result<Array, V::Error> | ||
where | ||
V: de::SeqAccess<'de>, | ||
{ | ||
Ok(Array) | ||
} | ||
} | ||
|
||
deserializer.deserialize_tuple(0, Visitor) | ||
} | ||
} |
Oops, something went wrong.