Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / ukey2_proto / src / lib.rs
lib.rs88 lines · 3.2 KiBRaw
| 1 | // Copyright 2023 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #[cfg(feature = "cargo")] |
| 16 | pub mod ukey2_all_proto { |
| 17 | include!(concat!(env!("OUT_DIR"), "/proto/mod.rs")); |
| 18 | } |
| 19 | |
| 20 | #[cfg(feature = "soong")] |
| 21 | pub mod ukey2_all_proto { |
| 22 | pub mod device_to_device_messages; |
| 23 | pub mod securegcm; |
| 24 | pub mod securemessage; |
| 25 | pub mod ukey; |
| 26 | } |
| 27 | |
| 28 | #[cfg(all(not(feature = "cargo"), not(feature = "soong")))] |
| 29 | pub mod ukey2_all_proto { |
| 30 | pub mod device_to_device_messages; |
| 31 | pub mod securegcm; |
| 32 | pub mod securemessage; |
| 33 | pub mod ukey; |
| 34 | } |
| 35 | |
| 36 | pub use protobuf; |
| 37 | |
| 38 | #[cfg(all(test, feature = "cargo"))] |
| 39 | mod tests { |
| 40 | use std::{fs, path::Path}; |
| 41 | |
| 42 | #[test] |
| 43 | fn check_proto_needs_update() { |
| 44 | let proto_src_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/src/ukey2_all_proto/"); |
| 45 | for file in fs::read_dir(proto_src_dir).unwrap() { |
| 46 | let actual_file = file.unwrap(); |
| 47 | let file_name_str = actual_file.file_name(); |
| 48 | if actual_file.file_type().unwrap().is_file() |
| 49 | && file_name_str.to_string_lossy().ends_with(".rs") |
| 50 | { |
| 51 | println!("Checking file {}", &file_name_str.to_string_lossy()); |
| 52 | let newly_generated_file_path = |
| 53 | env!("OUT_DIR").to_string() + "/proto/" + &file_name_str.to_string_lossy(); |
| 54 | let current_file_path = proto_src_dir.to_owned() + file_name_str.to_str().unwrap(); |
| 55 | if let Some(diff_str) = diff_file(&newly_generated_file_path, ¤t_file_path) { |
| 56 | panic!( |
| 57 | "file '{}' needs to be updated.\n\n{}\n", |
| 58 | file_name_str.to_string_lossy(), |
| 59 | diff_str |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // Ignore the first 17 lines of the generated proto files, since that contains the apache header and |
| 67 | // version numbers of rust-protobuf and protoc that may differ based on the host machine configuration. |
| 68 | const DIFF_IGNORE_LINES: usize = 17; |
| 69 | |
| 70 | fn diff_file<P: AsRef<Path>>(left: P, right: P) -> Option<String> { |
| 71 | let left_content = fs::read_to_string(left).unwrap(); |
| 72 | let right_content = fs::read_to_string(right).unwrap(); |
| 73 | let diff = &diff::lines(&left_content, &right_content)[DIFF_IGNORE_LINES..]; |
| 74 | let line_diffs = diff |
| 75 | .iter() |
| 76 | .filter_map(|d| match d { |
| 77 | diff::Result::Left(l) => Some(format!("-{l}")), |
| 78 | diff::Result::Both(_, _) => None, |
| 79 | diff::Result::Right(r) => Some(format!("+{r}")), |
| 80 | }) |
| 81 | .collect::<Vec<String>>(); |
| 82 | if line_diffs.is_empty() { |
| 83 | None |
| 84 | } else { |
| 85 | Some(line_diffs.join("\n")) |
| 86 | } |
| 87 | } |
| 88 | } |