xcb_rust_connection/helpers/
basic_info_provider.rs

1use xcb_rust_protocol::util::{ExtensionInfoProvider, ExtensionInformation};
2
3#[derive(Default, Debug)]
4pub struct BasicExtensionInfoProvider {
5    pub(crate) extensions: alloc::vec::Vec<(&'static str, ExtensionInformation)>,
6}
7
8impl ExtensionInfoProvider for BasicExtensionInfoProvider {
9    #[inline]
10    fn get_by_name(&self, name: &str) -> Option<ExtensionInformation> {
11        self.extensions
12            .iter()
13            .find_map(|(ext_name, ext)| (*ext_name == name).then_some(*ext))
14    }
15
16    #[inline]
17    fn get_from_major_opcode(
18        &self,
19        major_opcode: u8,
20    ) -> Option<(&'static str, ExtensionInformation)> {
21        for (name, ext_info) in &self.extensions {
22            if ext_info.major_opcode == major_opcode {
23                return Some((name, *ext_info));
24            }
25        }
26        None
27    }
28
29    #[inline]
30    fn get_from_event_code(&self, event_code: u8) -> Option<(&'static str, ExtensionInformation)> {
31        self.extensions
32            .iter()
33            .filter_map(|(name, ext_info)| {
34                (ext_info.first_event <= event_code).then_some((*name, *ext_info))
35            })
36            .max_by_key(|i| i.1.first_event)
37    }
38
39    #[inline]
40    fn get_from_error_code(&self, error_code: u8) -> Option<(&'static str, ExtensionInformation)> {
41        self.extensions
42            .iter()
43            .filter_map(|(name, ext_info)| {
44                (ext_info.first_error <= error_code).then_some((*name, *ext_info))
45            })
46            .max_by_key(|i| i.1.first_error)
47    }
48}