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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#![allow(clippy::module_name_repetitions)]
pub mod message;
pub mod metadata;
pub mod request;
pub mod response;

pub use message::DynamicMessage;
pub use request::RequestMessage;
pub use response::ResponseMessage;

use crate::error::Error;
use crate::Result;
use prost_reflect::{DescriptorPool, MessageDescriptor, MethodDescriptor, ServiceDescriptor};
use std::path::Path;

#[derive(Default, Debug, Clone)]
pub struct ProtoDescriptor {
    pool: DescriptorPool,
}

impl ProtoDescriptor {
    // /// Instantiates a descriptor from a [`ProtoConfig`]
    // ///
    // /// # Errors
    // /// - Failed to compile proto `ProtoxCompileError`
    // /// - Failed to generate descriptor `DescriptorError`
    // pub fn from_config(cfg: &Config) -> Result<Self> {
    //     let files = cfg.files.clone();
    //     let includes = vec![cfg.workspace.clone()];
    //     Self::from_files(files, includes)
    // }

    /// Instantiate `DescriptorPool` from proto files and include paths
    ///
    /// # Errors
    /// - Failed to compile proto `ProtoxCompileError`
    /// - Failed to generate descriptor `DescriptorError`
    pub fn new(
        includes: impl IntoIterator<Item = impl AsRef<Path>>,
        files: impl IntoIterator<Item = impl AsRef<Path>>,
    ) -> Result<Self> {
        // Compile proto files to file descriptors
        let file_desc_set = protox::compile(files, includes).map_err(Error::ProtoxCompileError)?;
        // Generate descriptor pool from file descriptor
        let pool = DescriptorPool::from_file_descriptor_set(file_desc_set)
            .map_err(Error::DescriptorError)?;
        Ok(Self { pool })
    }

    /// Returns a Service by its name
    #[must_use]
    pub fn get_service_by_name(&self, name: &str) -> Option<ServiceDescriptor> {
        self.pool.get_service_by_name(name)
    }

    /// Returns a Message by its name
    #[must_use]
    pub fn get_message_by_name(&self, name: &str) -> Option<MessageDescriptor> {
        self.pool.get_message_by_name(name)
    }

    /// Returns a Method of a service by its name
    #[must_use]
    pub fn get_method_by_name(
        &self,
        service_name: &str,
        method_name: &str,
    ) -> Option<MethodDescriptor> {
        self.get_service_by_name(service_name)?
            .methods()
            .find(|m| m.name() == method_name)
    }

    /// Returns all Services from the descriptor pool
    #[must_use]
    pub fn get_services(&self) -> Vec<ServiceDescriptor> {
        let mut services: Vec<ServiceDescriptor> = self.pool.services().collect();
        services.sort_by(|a, b| a.full_name().cmp(b.full_name()));
        services
    }
    // Returns all Methods of a given Service
    #[must_use]
    pub fn get_methods(&self, service: &ServiceDescriptor) -> Vec<MethodDescriptor> {
        let mut methods: Vec<MethodDescriptor> = service.methods().collect();
        methods.sort_by(|a, b| a.full_name().cmp(b.full_name()));
        methods
    }

    // Returns the request MessageDescriptor of a given Method
    #[must_use]
    pub fn get_request_descriptor(&self, method: &MethodDescriptor) -> MessageDescriptor {
        method.input()
    }

    // Returns the response MessageDescriptor of a given Method
    #[must_use]
    pub fn get_response_descriptor(&self, method: &MethodDescriptor) -> MessageDescriptor {
        method.output()
    }

    // Returns the request Message of a given Method
    #[must_use]
    pub fn get_request(&self, method: &MethodDescriptor) -> RequestMessage {
        RequestMessage::new(self.get_request_descriptor(method), method.clone())
    }

    // Returns the response Message of a given Method
    #[must_use]
    pub fn get_response(&self, method: &MethodDescriptor) -> ResponseMessage {
        ResponseMessage::new(self.get_response_descriptor(method), method.clone())
    }
}