trunk
Alona EM 2022-01-15 22:16:52 +00:00
parent f3b147a6a3
commit 5a5199f91f
11 changed files with 722 additions and 0 deletions

1
hidl/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

153
hidl/Cargo.lock generated Normal file
View File

@ -0,0 +1,153 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "debug2"
version = "0.1.0"
source = "git+https://github.com/aDotInTheVoid/debug2/#8b14afb90fc1d9ce470792ea393a9ab821a9e07f"
dependencies = [
"debug2-derive",
]
[[package]]
name = "debug2-derive"
version = "0.1.0"
source = "git+https://github.com/aDotInTheVoid/debug2/#8b14afb90fc1d9ce470792ea393a9ab821a9e07f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hidl"
version = "0.1.0"
dependencies = [
"debug2",
"serde",
"serde_yaml",
]
[[package]]
name = "indexmap"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282a6247722caba404c065016bbfa522806e51714c34f5dfc3e4a3a46fcb4223"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "linked-hash-map"
version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
[[package]]
name = "proc-macro2"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47aa80447ce4daf1717500037052af176af5d38cc3e571d9ec1c7353fc10c87d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f"
[[package]]
name = "serde"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97565067517b60e2d1ea8b268e59ce036de907ac523ad83a0475da04e818989a"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed201699328568d8d08208fdd080e3ff594e6c422e438b6705905da01005d537"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_yaml"
version = "0.8.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0"
dependencies = [
"indexmap",
"ryu",
"serde",
"yaml-rust",
]
[[package]]
name = "syn"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a684ac3dcd8913827e18cd09a68384ee66c1de24157e3c556c9ab16d85695fb7"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "synstructure"
version = "0.12.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

13
hidl/Cargo.toml Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "hidl"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
debug2 = { git = "https://github.com/aDotInTheVoid/debug2/" }
serde = { version = "1.0.133", features = ["derive"] }
serde_yaml = "0.8.23"
[features]

63
hidl/spec-demo.yaml Normal file
View File

@ -0,0 +1,63 @@
---
name: hypercosm
root:
interfaces:
- name: object
version:
- 1
- 0
- 0
methods:
- name: listInterfaces
ret:
Array: String
- name: release
- name: root
version:
- 0
- 1
- 0
methods:
- name: listExtensions
ret:
Array: String
- name: ping
- name: getObjectById
ret: Object
args:
- - id
- Uuid
- name: getObjectByName
ret: Object
args:
- - name
- String
extenstions:
- name: assetdelivery
version:
- 0
- 1
- 0
module:
interfaces:
- name: assetdelivery
version:
- 0
- 1
- 0
events:
- name: loadAssets
args:
- - name
- String
types:
- name: asset
kind:
Struct:
fields:
- - id
- Uuid
- - name
- String
- - data
- Array: U8

89
hidl/spec-testcosm.yaml Normal file
View File

@ -0,0 +1,89 @@
---
- interface:
name: hypercosm.object.v1.0.0
methods:
- listInterfaces:
- array[string]
- release
- interface:
name: hypercosm.root.v0.1.0
methods:
- listExtensions:
- array[string]
- ping
- getObjectById:
- object
- uuid: id
- getObjectByName:
- object
- string: name
- extension:
- name: hypercosm.assetdelivery.v0.1.0
- interface: # Implicit name; matches extension and visible from root object
events: # Events can come after methods in the YAML but always first for command numbering
- loadAssets:
- void
- array[asset]: asset
- unloadAssets:
- void
- array[uuid]: ids
methods:
- fetchAssetById:
- asset
- uuid: id
- fetchAssetByName:
- asset
- string: name
- fetchAssetsByIds:
- array[asset]
- array[uuid]: ids
- fetchAssetsByNames:
- array[asset]
- array[string]: names
- getId:
- uuid
- string: name
- struct:
- name: asset # First element of a struct is always the name
- uuid: id
- string: name
- array[u8]: data
- extension:
- name: hypercosm.world.v0.1.0
- interface:
events:
- addEntities:
- void
- array[entityInfo]: entities
- updateEntities:
- void
- array[entityInfo]: entities
- removeEntities:
- void
- array[entity]: entities
- struct:
- name: entityInfo
- uuid: assetId # glTF
- entity: entity
- matrix4x4: transformation
- entityFlags: flags
- interface:
name: entity
methods:
- interact
- flags:
- name: entityFlags
- type: vu128
- None: 0
- Interactable
- Collidable
- enum: # Alternate form
- name: entityFlags
- type: vu128
- None: 0
- Interactable: 1
- Collidable: 2

80
hidl/spec.yaml Normal file
View File

@ -0,0 +1,80 @@
---
name: hypercosm
root:
interfaces:
- name: object
version:
- 1
- 0
- 0
methods:
- name: listInterfaces
ret:
Array: String
- name: release
- name: root
version:
- 0
- 1
- 0
methods:
- name: listExtensions
ret:
Array: String
- name: ping
- name: getObjectById
ret: Object
args:
- - id
- Uuid
- name: getObjectByName
ret: Object
args:
- - name
- String
extenstions:
- name: assetdelivery
version:
- 0
- 1
- 0
module:
interfaces:
- name: assetdelivery
version:
- 0
- 1
- 0
events:
- name: loadAssets
args:
- - assets
- Array:
Custom:
asset
- name: unloadAssets
args:
- - ids
- Array: Uuid
methods:
- name: fetchAssetByName
args:
- - name
- String
ret:
Custom:
asset
types:
- name: asset
kind:
Struct:
fields:
- - id
- Uuid
- - name
- String
- - data
- Array: U8

9
hidl/src/bin/demo.rs Normal file
View File

@ -0,0 +1,9 @@
use std::fs;
fn main() {
let root = hidl::spec::spec();
debug2::dbg!(&root);
let yaml_str = serde_yaml::to_string(&root).unwrap();
eprintln!("{}", yaml_str);
fs::write("spec-demo.yaml", &yaml_str).unwrap();
}

10
hidl/src/bin/load.rs Normal file
View File

@ -0,0 +1,10 @@
use std::fs;
use hidl::IDL;
fn main() {
let file = fs::read_to_string("spec.yaml").unwrap();
let spec: IDL = serde_yaml::from_str(&file).unwrap();
debug2::dbg!(spec);
}

159
hidl/src/lib.rs Normal file
View File

@ -0,0 +1,159 @@
use debug2::Debug as Debug2;
use serde::{Deserialize, Serialize};
pub mod spec;
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Module {
#[serde(default, skip_serializing_if = "is_default")]
interfaces: Vec<Interface>,
#[serde(default, skip_serializing_if = "is_default")]
types: Vec<CustomType>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct IDL {
name: String,
root: Module,
#[serde(default, skip_serializing_if = "is_default")]
extenstions: Vec<Extension>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Extension {
name: String,
version: Version,
module: Module,
}
type Version = (u8, u8, u8);
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Interface {
name: String,
version: Version,
#[serde(default, skip_serializing_if = "is_default")]
methods: Vec<Method>,
#[serde(default, skip_serializing_if = "is_default")]
events: Vec<Method>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Method {
name: String,
#[serde(default, skip_serializing_if = "is_default")]
ret: Type,
#[serde(default, skip_serializing_if = "is_default")]
args: Vec<(String, Type)>,
}
#[derive(Debug, Debug2, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum Type {
NotIntType(NotIntType),
IntType(IntType),
}
#[derive(Debug, Debug2, Clone, Serialize, Deserialize, PartialEq)]
pub enum NotIntType {
/// TODO: Should we special case Array of u8?
Array(Box<Type>),
Custom(String),
String,
Object,
Uuid,
/// Unit type
Void,
}
impl Default for Type {
fn default() -> Self {
Self::NotIntType(NotIntType::Void)
}
}
#[derive(Debug, Debug2, Clone, Serialize, Deserialize, PartialEq)]
pub enum IntType {
U8,
U16,
U32,
U64,
I8,
I16,
I32,
I64,
VU16,
VU8,
VU32,
VU64,
VI8,
VI16,
VI32,
VI64,
}
#[derive(Debug, Debug2, Clone, Serialize, Deserialize, PartialEq)]
pub struct CustomType {
name: String,
kind: CustomTypeKind,
}
#[derive(Debug, Debug2, Clone, Serialize, Deserialize, PartialEq)]
enum CustomTypeKind {
Struct(CustomStruct),
Enum(Enum),
}
#[derive(Debug, Debug2, Clone, Serialize, Deserialize, PartialEq)]
pub struct Enum {
storage: IntType,
values: Vec<(String, i64)>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct CustomStruct {
fields: Vec<(String, Type)>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Event {}
fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}
mod ty {
use super::IntType::*;
use super::NotIntType::*;
use super::Type::{self, *};
pub fn void() -> Type {
NotIntType(Void)
}
pub fn array(t: Type) -> Type {
NotIntType(Array(Box::new(t)))
}
pub fn string() -> Type {
NotIntType(String)
}
pub fn object() -> Type {
NotIntType(Object)
}
pub fn uuid() -> Type {
NotIntType(Uuid)
}
pub fn u8() -> Type {
IntType(U8)
}
}

87
hidl/src/spec.rs Normal file
View File

@ -0,0 +1,87 @@
use crate::*;
// TODO: Use autodefault when https://github.com/Lucretiel/autodefault/issues/4
// is fixed
pub fn spec() -> IDL {
IDL {
name: "hypercosm".to_owned(),
root: Module {
interfaces: vec![
Interface {
name: "object".to_owned(),
version: (1, 0, 0), // TODO: Change
methods: vec![
Method {
name: "listInterfaces".to_owned(),
args: vec![],
ret: ty::array(ty::string()),
},
Method {
name: "release".to_owned(),
..default()
},
],
..default()
},
Interface {
name: "root".to_owned(),
version: (0, 1, 0),
methods: vec![
Method {
name: "listExtensions".to_owned(),
ret: ty::array(ty::string()),
..default()
},
Method {
name: "ping".to_owned(),
..default()
},
Method {
name: "getObjectById".to_owned(),
ret: ty::object(),
args: vec![("id".to_owned(), ty::uuid())],
},
Method {
name: "getObjectByName".to_owned(),
ret: ty::object(),
args: vec![("name".to_owned(), ty::string())],
},
],
..default()
},
],
..default()
},
extenstions: vec![Extension {
name: "assetdelivery".to_owned(),
version: (0, 1, 0),
module: Module {
interfaces: vec![Interface {
name: "assetdelivery".to_owned(),
version: (0, 1, 0),
events: vec![Method {
name: "loadAssets".to_owned(),
args: vec![("name".to_owned(), ty::string())],
..default()
}],
..default()
}],
types: vec![CustomType {
name: "asset".to_owned(),
kind: CustomTypeKind::Struct(CustomStruct {
fields: vec![
("id".to_owned(), ty::uuid()),
("name".to_owned(), ty::string()),
("data".to_owned(), ty::array(ty::u8())),
],
}),
}],
},
}],
}
}
fn default<D: Default>() -> D {
Default::default()
}

58
hidl/src/spec.yaml Normal file
View File

@ -0,0 +1,58 @@
---
name: hypercosm
root:
interfaces:
- name: object
version:
- 1
- 0
- 0
methods:
- name: listInterfaces
ret:
Array: String
- name: release
- name: root
version:
- 0
- 1
- 0
methods:
- name: listExtensions
ret:
Array: String
- name: ping
- name: getObjectById
ret: Object
args:
- - id
- Uuid
- name: getObjectByName
ret: Object
args:
- - name
- String
extenstions:
- name: assetdelivery
version:
- 0
- 1
- 0
module:
interfaces:
- name: assetdelivery
version:
- 0
- 0
- 0
types:
- name: asset
kind:
Struct:
fields:
- - id
- Uuid
- - name
- String
- - data
- Array: U8