1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::sync::Arc;

use crate::descriptor::EnumDescriptorProto;
use crate::descriptor::FileDescriptorProto;
use crate::reflect::enums::index::EnumIndex;
use crate::reflect::message::path::MessagePath;
use crate::reflect::name::append_path;

#[derive(Debug)]
pub(crate) struct DynamicEnumValueDescriptor {}

#[derive(Debug)]
pub(crate) struct DynamicEnumDescriptor {
    pub full_name: String,
    file_descriptor_proto: Arc<FileDescriptorProto>,
    path: MessagePath,
    enum_index: usize,
    pub(crate) _values: Vec<DynamicEnumValueDescriptor>,

    pub(crate) indices: EnumIndex<String>,
}

impl DynamicEnumDescriptor {
    pub fn new(
        proto: Arc<FileDescriptorProto>,
        path: &MessagePath,
        enum_index: usize,
    ) -> DynamicEnumDescriptor {
        let mut full_name = proto.get_package().to_owned();
        let e = if path.len() == 0 {
            &proto.enum_type[enum_index]
        } else {
            let messages = path.eval_path(&*proto);
            for m in &messages {
                append_path(&mut full_name, m.get_name());
            }
            &messages.last().cloned().unwrap().enum_type[enum_index]
        };
        append_path(&mut full_name, e.get_name());

        let indices = EnumIndex::<String>::index(e);

        let values = e
            .value
            .iter()
            .map(|_| DynamicEnumValueDescriptor {})
            .collect();

        DynamicEnumDescriptor {
            full_name,
            file_descriptor_proto: proto,
            path: path.clone(),
            enum_index,
            _values: values,
            indices,
        }
    }

    pub fn get_proto(&self) -> &EnumDescriptorProto {
        match self.path.eval(&self.file_descriptor_proto) {
            None => &self.file_descriptor_proto.enum_type[self.enum_index],
            Some(m) => &m.enum_type[self.enum_index],
        }
    }
}