protobuf/reflect/file/
fds.rs

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
66
67
68
use std::collections::HashMap;
use std::collections::HashSet;
use std::mem;

use protobuf_support::toposort::toposort;

use crate::descriptor::FileDescriptorProto;
use crate::reflect::error::ReflectError;
use crate::reflect::FileDescriptor;

pub(crate) fn build_fds(
    protos: Vec<FileDescriptorProto>,
    dependencies: &[FileDescriptor],
) -> crate::Result<Vec<FileDescriptor>> {
    let mut index_by_name: HashMap<&str, usize> = HashMap::new();
    for (i, proto) in protos.iter().enumerate() {
        let prev = index_by_name.insert(proto.name(), i);
        if prev.is_some() {
            return Err(ReflectError::NonUniqueFileDescriptor(proto.name().to_owned()).into());
        }
    }

    let sorted = match toposort(0..protos.len(), |&i| {
        protos[i]
            .dependency
            .iter()
            .filter_map(|d| index_by_name.get(d.as_str()).copied())
    }) {
        Ok(s) => s,
        Err(_) => return Err(ReflectError::CycleInFileDescriptors.into()),
    };

    let mut built_descriptors_by_index = vec![None; protos.len()];

    let mut protos: Vec<Option<FileDescriptorProto>> = protos.into_iter().map(Some).collect();

    let mut all_descriptors = dependencies.to_vec();
    for f in sorted {
        let proto = mem::take(&mut protos[f]).unwrap();
        let d = FileDescriptor::new_dynamic(proto, &all_descriptors)?;
        all_descriptors.push(d.clone());
        built_descriptors_by_index[f] = Some(d);
    }

    Ok(built_descriptors_by_index
        .into_iter()
        .map(Option::unwrap)
        .collect())
}

pub(crate) fn fds_extend_with_public(file_descriptors: Vec<FileDescriptor>) -> Vec<FileDescriptor> {
    let mut visited = HashSet::new();

    let mut r = Vec::new();
    let mut stack = file_descriptors;
    stack.reverse();

    while let Some(f) = stack.pop() {
        if !visited.insert(f.proto().name().to_owned()) {
            continue;
        }

        stack.extend(f.public_deps());

        r.push(f);
    }
    r
}