Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // When building with the Android tool-chain |
| 18 | // |
| 19 | // - an external crate `aconfig_protos` will be generated |
| 20 | // - the feature "cargo" will be disabled |
| 21 | // |
| 22 | // When building with cargo |
| 23 | // |
| 24 | // - a local sub-module will be generated in OUT_DIR and included in this file |
| 25 | // - the feature "cargo" will be enabled |
| 26 | // |
| 27 | // This module hides these differences from the rest of aconfig. |
| 28 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 29 | // ---- When building with the Android tool-chain ---- |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 30 | #[cfg(not(feature = "cargo"))] |
Mårten Kongstad | f942252 | 2023-06-14 08:38:46 +0200 | [diff] [blame] | 31 | mod auto_generated { |
| 32 | pub use aconfig_protos::aconfig::Flag_declaration as ProtoFlagDeclaration; |
| 33 | pub use aconfig_protos::aconfig::Flag_declarations as ProtoFlagDeclarations; |
| 34 | pub use aconfig_protos::aconfig::Flag_permission as ProtoFlagPermission; |
| 35 | pub use aconfig_protos::aconfig::Flag_state as ProtoFlagState; |
| 36 | pub use aconfig_protos::aconfig::Flag_value as ProtoFlagValue; |
| 37 | pub use aconfig_protos::aconfig::Flag_values as ProtoFlagValues; |
| 38 | pub use aconfig_protos::aconfig::Parsed_flag as ProtoParsedFlag; |
| 39 | pub use aconfig_protos::aconfig::Parsed_flags as ProtoParsedFlags; |
| 40 | pub use aconfig_protos::aconfig::Tracepoint as ProtoTracepoint; |
| 41 | } |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 42 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 43 | // ---- When building with cargo ---- |
Mårten Kongstad | fe753f5 | 2023-04-26 09:13:03 +0200 | [diff] [blame] | 44 | #[cfg(feature = "cargo")] |
Mårten Kongstad | f942252 | 2023-06-14 08:38:46 +0200 | [diff] [blame] | 45 | mod auto_generated { |
| 46 | // include! statements should be avoided (because they import file contents verbatim), but |
| 47 | // because this is only used during local development, and only if using cargo instead of the |
| 48 | // Android tool-chain, we allow it |
| 49 | include!(concat!(env!("OUT_DIR"), "/aconfig_proto/mod.rs")); |
| 50 | pub use aconfig::Flag_declaration as ProtoFlagDeclaration; |
| 51 | pub use aconfig::Flag_declarations as ProtoFlagDeclarations; |
| 52 | pub use aconfig::Flag_permission as ProtoFlagPermission; |
| 53 | pub use aconfig::Flag_state as ProtoFlagState; |
| 54 | pub use aconfig::Flag_value as ProtoFlagValue; |
| 55 | pub use aconfig::Flag_values as ProtoFlagValues; |
| 56 | pub use aconfig::Parsed_flag as ProtoParsedFlag; |
| 57 | pub use aconfig::Parsed_flags as ProtoParsedFlags; |
| 58 | pub use aconfig::Tracepoint as ProtoTracepoint; |
| 59 | } |
Mårten Kongstad | a102909 | 2023-05-08 11:51:59 +0200 | [diff] [blame] | 60 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 61 | // ---- Common for both the Android tool-chain and cargo ---- |
Mårten Kongstad | f942252 | 2023-06-14 08:38:46 +0200 | [diff] [blame] | 62 | pub use auto_generated::*; |
| 63 | |
Mårten Kongstad | bb52072 | 2023-04-26 13:16:41 +0200 | [diff] [blame] | 64 | use anyhow::Result; |
| 65 | |
| 66 | pub fn try_from_text_proto<T>(s: &str) -> Result<T> |
| 67 | where |
| 68 | T: protobuf::MessageFull, |
| 69 | { |
| 70 | // warning: parse_from_str does not check if required fields are set |
| 71 | protobuf::text_format::parse_from_str(s).map_err(|e| e.into()) |
| 72 | } |