protospec_build/coder/
mod.rs

1use proc_macro2::TokenStream;
2use quote::{ToTokens, TokenStreamExt};
3
4use crate::ScalarType;
5
6#[derive(Debug)]
7pub enum FieldRef {
8    Name(String),
9    ArrayAccess(usize),
10    TupleAccess(usize),
11}
12
13#[derive(Debug, Clone, Copy)]
14pub enum Target {
15    Direct,
16    Buf(usize),
17    Stream(usize),
18}
19
20impl Target {
21    pub fn unwrap_buf(&self) -> usize {
22        match self {
23            Target::Buf(x) => *x,
24            _ => unimplemented!(),
25        }
26    }
27}
28
29#[derive(Clone, Copy, Debug)]
30pub enum PrimitiveType {
31    Bool,
32    F32,
33    F64,
34    Scalar(ScalarType),
35}
36
37impl PrimitiveType {
38    pub fn size(&self) -> u64 {
39        match self {
40            PrimitiveType::Bool => 1,
41            PrimitiveType::F32 => 4,
42            PrimitiveType::F64 => 8,
43            PrimitiveType::Scalar(s) => s.size(),
44        }
45    }
46}
47
48impl ToString for PrimitiveType {
49    fn to_string(&self) -> String {
50        match self {
51            PrimitiveType::Bool => "bool".to_string(),
52            PrimitiveType::F32 => "f32".to_string(),
53            PrimitiveType::F64 => "f64".to_string(),
54            PrimitiveType::Scalar(s) => s.to_string(),
55        }
56    }
57}
58
59impl ToTokens for PrimitiveType {
60    fn to_tokens(&self, tokens: &mut TokenStream) {
61        match self {
62            PrimitiveType::Bool => tokens.append(format_ident!("bool")),
63            PrimitiveType::F32 => tokens.append(format_ident!("f32")),
64            PrimitiveType::F64 => tokens.append(format_ident!("f64")),
65            PrimitiveType::Scalar(s) => tokens.append(format_ident!("{}", &s.to_string())),
66        }
67    }
68}
69
70pub mod decode;
71pub mod encode;