1 // Copyright (C) 2024 The Android Open Source Project
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 //! Errors accessing system properties.
16 
17 use std::str::Utf8Error;
18 use thiserror::Error;
19 
20 /// Errors this crate can generate
21 #[derive(Debug, Error)]
22 pub enum PropertyWatcherError {
23     /// We can't watch for a property whose name contains a NUL character.
24     #[error("Cannot convert name to C string")]
25     BadNameError(#[from] std::ffi::NulError),
26     /// We can only watch for properties that exist when the watcher is created.
27     #[error("System property is absent")]
28     SystemPropertyAbsent,
29     /// System properties are not initialized
30     #[error("System properties are not initialized.")]
31     Uninitialized,
32     /// __system_property_wait timed out.
33     #[error("Wait failed")]
34     WaitFailed,
35     /// read callback was not called
36     #[error("__system_property_read_callback did not call callback")]
37     ReadCallbackNotCalled,
38     /// read callback gave us a NULL pointer
39     #[error("__system_property_read_callback gave us a NULL pointer instead of a string")]
40     MissingCString,
41     /// read callback gave us a bad C string
42     #[error("__system_property_read_callback gave us a non-UTF8 C string")]
43     BadCString(#[from] Utf8Error),
44     /// read callback returned an error
45     #[error("Callback failed")]
46     CallbackError(#[from] anyhow::Error),
47     /// Failure in setting the system property
48     #[error("__system_property_set failed.")]
49     SetPropertyFailed,
50 }
51 
52 /// Result type specific for this crate.
53 pub type Result<T> = std::result::Result<T, PropertyWatcherError>;
54 
55 /// Errors returned by generated system property accessors.
56 #[derive(Debug, Error)]
57 pub enum SysPropError {
58     /// Failed to fetch the system property.
59     #[error("Failed to fetch system property: {0}")]
60     FetchError(PropertyWatcherError),
61     /// Failed to set the system property.
62     #[error("Failed to set system property: {0}")]
63     SetError(PropertyWatcherError),
64     /// Failed to parse the system property value.
65     #[error("Failed to parse the system property value: {0}")]
66     ParseError(String),
67 }
68