D:\a\tools.proto\tools.proto\compiler\src\model\message.rs
Line | Count | Source |
1 | | // Copyright (c) 2024, BlockProject 3D |
2 | | // |
3 | | // All rights reserved. |
4 | | // |
5 | | // Redistribution and use in source and binary forms, with or without modification, |
6 | | // are permitted provided that the following conditions are met: |
7 | | // |
8 | | // * Redistributions of source code must retain the above copyright notice, |
9 | | // this list of conditions and the following disclaimer. |
10 | | // * Redistributions in binary form must reproduce the above copyright notice, |
11 | | // this list of conditions and the following disclaimer in the documentation |
12 | | // and/or other materials provided with the distribution. |
13 | | // * Neither the name of BlockProject 3D nor the names of its contributors |
14 | | // may be used to endorse or promote products derived from this software |
15 | | // without specific prior written permission. |
16 | | // |
17 | | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
18 | | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
19 | | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
20 | | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
21 | | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
22 | | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
23 | | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
24 | | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
25 | | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
26 | | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27 | | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | |
29 | | use crate::model::protocol::Description; |
30 | | use serde::Deserialize; |
31 | | |
32 | | #[derive(Clone, Debug, Deserialize)] |
33 | | #[serde(rename_all = "snake_case")] |
34 | | #[serde(tag = "type")] |
35 | | pub enum MessageFieldValue { |
36 | | List { |
37 | | max_len: usize, |
38 | | item_type: String, |
39 | | max_size: Option<usize>, |
40 | | nested: Option<bool>, |
41 | | }, |
42 | | Container { |
43 | | max_len: usize, |
44 | | item_type: String, |
45 | | max_size: Option<usize>, |
46 | | nested: Option<bool>, |
47 | | }, |
48 | | String { |
49 | | max_len: Option<usize>, |
50 | | }, |
51 | | Buffer { |
52 | | max_len: Option<usize>, |
53 | | }, |
54 | | Payload, |
55 | | Union { |
56 | | name: String, |
57 | | }, |
58 | | Unsigned { |
59 | | bits: usize, |
60 | | }, |
61 | | } |
62 | | |
63 | | #[derive(Clone, Debug, Deserialize)] |
64 | | pub struct MessageField { |
65 | | pub name: String, |
66 | | pub header: Option<String>, |
67 | | pub value: Option<MessageFieldValue>, |
68 | | pub optional: Option<bool>, |
69 | | pub description: Option<Description>, |
70 | | pub item_type: Option<String>, |
71 | | pub codec: Option<String>, |
72 | | } |
73 | | |
74 | | impl MessageField { |
75 | 6 | pub fn references(&self, name1: &str) -> bool { |
76 | 6 | if self.item_type.as_deref() == Some(name1) { Branch (76:12): [True: 0, False: 6]
Branch (76:12): [Folded - Ignored]
|
77 | 0 | return true; |
78 | 6 | } |
79 | 6 | if self.header.as_deref() == Some(name1) { Branch (79:12): [True: 0, False: 6]
Branch (79:12): [Folded - Ignored]
|
80 | 0 | return true; |
81 | 6 | } |
82 | 6 | match &self.value { |
83 | 0 | None => false, |
84 | 6 | Some(v) => match v { |
85 | 2 | MessageFieldValue::List { item_type, .. } => name1 == item_type, |
86 | 0 | MessageFieldValue::Union { name } => name == name1, |
87 | 4 | _ => false, |
88 | | }, |
89 | | } |
90 | 6 | } |
91 | | } |
92 | | |
93 | | #[derive(Clone, Debug, Deserialize)] |
94 | | pub struct Message { |
95 | | pub name: String, |
96 | | pub description: Option<Description>, |
97 | | pub fields: Vec<MessageField>, |
98 | | } |
99 | | |
100 | | impl Message { |
101 | 6 | pub fn references(&self, name1: &str) -> bool { |
102 | 6 | self.fields.iter().any(|v| v.references(name1)) |
103 | 6 | } |
104 | | } |