nemicosm/hidl/src/lib.rs

161 lines
2.9 KiB
Rust
Raw Normal View History

2022-01-15 22:16:52 +00:00
use debug2::Debug as Debug2;
use serde::{Deserialize, Serialize};
2022-01-16 01:25:47 +00:00
pub mod ast;
#[allow(clippy::all)]
pub mod grammar;
2022-01-15 22:16:52 +00:00
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Module {
interfaces: Vec<Interface>,
2022-01-16 01:25:47 +00:00
2022-01-15 22:16:52 +00:00
types: Vec<CustomType>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct IDL {
name: String,
root: Module,
2022-01-16 01:25:47 +00:00
2022-01-15 22:16:52 +00:00
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,
methods: Vec<Method>,
2022-01-16 01:25:47 +00:00
2022-01-15 22:16:52 +00:00
events: Vec<Method>,
}
#[derive(Debug, Debug2, Default, Clone, Serialize, Deserialize, PartialEq)]
pub struct Method {
name: String,
2022-01-16 01:25:47 +00:00
2022-01-15 22:16:52 +00:00
ret: Type,
2022-01-16 01:25:47 +00:00
2022-01-15 22:16:52 +00:00
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)
}
}