plan9/protocol/
macros.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
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
113
114
115
116
117
118
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// Defines a struct to hold a 9p message's fields.
macro_rules! p9_message_struct {
    ($( $num:literal $name:ident $($field_name:ident [$field_type:tt] )* ;)*) => {
        $(
            // The struct holds the reader it was created from for two reasons:
            // 1. Some operations (e.g. Twrite) need it to access additional data.
            // 2. It allows the lifetime 'a to be there unconditionally; otherwise, only some
            //    messages would need it and the macro can't easily filter on that.
            #[allow(dead_code)]
            pub struct $name<'a> {
                pub reader: super::SliceReader<'a>,
                $(pub $field_name: p9_message_struct!(@to_type $field_type),)*
            }

            // Create a message from a slice reader.
            impl<'a> TryFrom<super::SliceReader<'a>> for $name<'a> {
                type Error = lx::Error;

                p9_message_struct!(@try_from $name $($field_name [$field_type])*);
            }

            // Custom Debug trait because the reader field must be excluded.
            impl<'a> std::fmt::Debug for $name<'a> {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    f.debug_struct(stringify!($name))
                    $(
                        .field(stringify!($field_name), &self.$field_name)
                    )*
                        .finish()
                }
            }
        )*
    };

    // Convert size to the field type.
    (@to_type s) => { &'a lx::LxStr };
    (@to_type n) => { &'a lx::LxStr };
    (@to_type q) => { Qid };
    (@to_type ns) => { super::NameIterator<'a> };
    (@to_type qs) => { super::QidIterator<'a> };
    (@to_type 2) => { u16 };
    (@to_type 4) => { u32 };
    (@to_type 8) => { u64 };

    // Convert size to the associated reader method.
    (@to_read $name:ident s) => { $name.string()? };
    (@to_read $name:ident n) => { $name.name()? };
    (@to_read $name:ident q) => { $name.qid()? };
    (@to_read $name:ident ns) => { $name.names()? };
    (@to_read $name:ident qs) => { $name.qids()? };
    (@to_read $name:ident 2) => { $name.u16()? };
    (@to_read $name:ident 4) => { $name.u32()? };
    (@to_read $name:ident 8) => { $name.u64()? };

    // Generate the try_from method for a message with fields.
    (@try_from $name:ident $($field_name:ident [$field_type:tt] )+) => {
        fn try_from(mut reader: super::SliceReader<'a>) -> lx::Result<$name<'a>> {
            $(
                let $field_name = p9_message_struct!(@to_read reader $field_type);
            )+
            Ok($name {
                reader,
                $(
                    $field_name,
                )+
            })
        }
    };

    // The case of a message with no fields must be handled separately so the compiler doesn't
    // complain about an unnecessary "mut" on the argument.
    (@try_from $name:ident) => {
        fn try_from(reader: super::SliceReader<'a>) -> lx::Result<$name<'a>> {
            Ok($name {
                reader,
            })
        }
    };
}

// Generate the Plan9Message enum.
macro_rules! p9_message_enum {
    ($( $num:literal $name:ident $($field_name:ident [$field_type:tt] )* ;)*) => {
        #[expect(dead_code)]
        #[derive(Debug)]
        pub enum Plan9Message<'a> {
            $($name($name<'a>),)*
        }

        impl<'a> Plan9Message<'a> {
            // Create a Plan9Message for the specified message type, reading the fields from the
            // reader.
            pub fn read(message_type: u8, reader: super::SliceReader<'a>) -> lx::Result<Plan9Message<'a>> {
                let message = match message_type {
                    $($num => Plan9Message::$name(reader.try_into()?),)*
                    _ => {
                        tracing::warn!(message_type, "[9P] Unhandled message type");
                        return Err(lx::Error::EINVAL)
                    }
                };

                Ok(message)
            }
        }
    };
}

// Generate structs and an enum to represent 9p protocol messages.
#[macro_export]
macro_rules! p9_protocol_messages {
    ($($contents:tt)*) => {
        p9_message_struct!($($contents)*);
        p9_message_enum!($($contents)*);
    }
}