xref: /aosp_15_r20/system/librustutils/system_properties/error.rs (revision e51878c104ea269309bae357ae559a9fff179380)
1*e51878c1SAndroid Build Coastguard Worker // Copyright (C) 2024 The Android Open Source Project
2*e51878c1SAndroid Build Coastguard Worker //
3*e51878c1SAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*e51878c1SAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*e51878c1SAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*e51878c1SAndroid Build Coastguard Worker //
7*e51878c1SAndroid Build Coastguard Worker //     http://www.apache.org/licenses/LICENSE-2.0
8*e51878c1SAndroid Build Coastguard Worker //
9*e51878c1SAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*e51878c1SAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*e51878c1SAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e51878c1SAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*e51878c1SAndroid Build Coastguard Worker // limitations under the License.
14*e51878c1SAndroid Build Coastguard Worker 
15*e51878c1SAndroid Build Coastguard Worker //! Errors accessing system properties.
16*e51878c1SAndroid Build Coastguard Worker 
17*e51878c1SAndroid Build Coastguard Worker use std::str::Utf8Error;
18*e51878c1SAndroid Build Coastguard Worker use thiserror::Error;
19*e51878c1SAndroid Build Coastguard Worker 
20*e51878c1SAndroid Build Coastguard Worker /// Errors this crate can generate
21*e51878c1SAndroid Build Coastguard Worker #[derive(Debug, Error)]
22*e51878c1SAndroid Build Coastguard Worker pub enum PropertyWatcherError {
23*e51878c1SAndroid Build Coastguard Worker     /// We can't watch for a property whose name contains a NUL character.
24*e51878c1SAndroid Build Coastguard Worker     #[error("Cannot convert name to C string")]
25*e51878c1SAndroid Build Coastguard Worker     BadNameError(#[from] std::ffi::NulError),
26*e51878c1SAndroid Build Coastguard Worker     /// We can only watch for properties that exist when the watcher is created.
27*e51878c1SAndroid Build Coastguard Worker     #[error("System property is absent")]
28*e51878c1SAndroid Build Coastguard Worker     SystemPropertyAbsent,
29*e51878c1SAndroid Build Coastguard Worker     /// System properties are not initialized
30*e51878c1SAndroid Build Coastguard Worker     #[error("System properties are not initialized.")]
31*e51878c1SAndroid Build Coastguard Worker     Uninitialized,
32*e51878c1SAndroid Build Coastguard Worker     /// __system_property_wait timed out.
33*e51878c1SAndroid Build Coastguard Worker     #[error("Wait failed")]
34*e51878c1SAndroid Build Coastguard Worker     WaitFailed,
35*e51878c1SAndroid Build Coastguard Worker     /// read callback was not called
36*e51878c1SAndroid Build Coastguard Worker     #[error("__system_property_read_callback did not call callback")]
37*e51878c1SAndroid Build Coastguard Worker     ReadCallbackNotCalled,
38*e51878c1SAndroid Build Coastguard Worker     /// read callback gave us a NULL pointer
39*e51878c1SAndroid Build Coastguard Worker     #[error("__system_property_read_callback gave us a NULL pointer instead of a string")]
40*e51878c1SAndroid Build Coastguard Worker     MissingCString,
41*e51878c1SAndroid Build Coastguard Worker     /// read callback gave us a bad C string
42*e51878c1SAndroid Build Coastguard Worker     #[error("__system_property_read_callback gave us a non-UTF8 C string")]
43*e51878c1SAndroid Build Coastguard Worker     BadCString(#[from] Utf8Error),
44*e51878c1SAndroid Build Coastguard Worker     /// read callback returned an error
45*e51878c1SAndroid Build Coastguard Worker     #[error("Callback failed")]
46*e51878c1SAndroid Build Coastguard Worker     CallbackError(#[from] anyhow::Error),
47*e51878c1SAndroid Build Coastguard Worker     /// Failure in setting the system property
48*e51878c1SAndroid Build Coastguard Worker     #[error("__system_property_set failed.")]
49*e51878c1SAndroid Build Coastguard Worker     SetPropertyFailed,
50*e51878c1SAndroid Build Coastguard Worker }
51*e51878c1SAndroid Build Coastguard Worker 
52*e51878c1SAndroid Build Coastguard Worker /// Result type specific for this crate.
53*e51878c1SAndroid Build Coastguard Worker pub type Result<T> = std::result::Result<T, PropertyWatcherError>;
54*e51878c1SAndroid Build Coastguard Worker 
55*e51878c1SAndroid Build Coastguard Worker /// Errors returned by generated system property accessors.
56*e51878c1SAndroid Build Coastguard Worker #[derive(Debug, Error)]
57*e51878c1SAndroid Build Coastguard Worker pub enum SysPropError {
58*e51878c1SAndroid Build Coastguard Worker     /// Failed to fetch the system property.
59*e51878c1SAndroid Build Coastguard Worker     #[error("Failed to fetch system property: {0}")]
60*e51878c1SAndroid Build Coastguard Worker     FetchError(PropertyWatcherError),
61*e51878c1SAndroid Build Coastguard Worker     /// Failed to set the system property.
62*e51878c1SAndroid Build Coastguard Worker     #[error("Failed to set system property: {0}")]
63*e51878c1SAndroid Build Coastguard Worker     SetError(PropertyWatcherError),
64*e51878c1SAndroid Build Coastguard Worker     /// Failed to parse the system property value.
65*e51878c1SAndroid Build Coastguard Worker     #[error("Failed to parse the system property value: {0}")]
66*e51878c1SAndroid Build Coastguard Worker     ParseError(String),
67*e51878c1SAndroid Build Coastguard Worker }
68