Merge "Migrate Test Targets to New Android Ownership Model" into main
diff --git a/RustGen.cpp b/RustGen.cpp
index f113223..5344a79 100644
--- a/RustGen.cpp
+++ b/RustGen.cpp
@@ -31,46 +31,18 @@
namespace {
-constexpr const char* kDocs = R"(//! Autogenerated system properties.
+constexpr const char* kDocs = R"(//! Autogenerated system property accessors.
//!
-//! This is autogenerated crate. The crate contains methods for easy access to
-//! the Android system properties.)";
+//! This is an autogenerated module. The module contains methods for typed access to
+//! Android system properties.)";
constexpr const char* kRustFileImports = R"(use std::fmt;
-use rustutils::system_properties;
-use rustutils::system_properties::parsers_formatters;)";
+use rustutils::system_properties::{self, error::SysPropError, parsers_formatters};)";
constexpr const char* kIndent = " ";
constexpr const char* kDeprecated = "#[deprecated]";
-constexpr const char* kError = R"(/// Errors this crate could generate.
-#[derive(Debug)]
-pub enum SysPropError {
- /// Failed to fetch the system property.
- FetchError(system_properties::PropertyWatcherError),
- /// Failed to set the system property.
- SetError(system_properties::PropertyWatcherError),
- /// Failed to parse the system property value.
- ParseError(String),
-}
-
-impl fmt::Display for SysPropError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- SysPropError::FetchError(err) =>
- write!(f, "failed to fetch the system property: {}", err),
- SysPropError::SetError(err) =>
- write!(f, "failed to set the system property: {}", err),
- SysPropError::ParseError(err) =>
- write!(f, "failed to parse the system property value: {}", err),
- }
- }
-})";
-
-constexpr const char* kResult = R"(/// Result type specific for this crate.
-pub type Result<T> = std::result::Result<T, SysPropError>;)";
-
std::string GetRustEnumType(const sysprop::Property& prop) {
std::string result = ApiNameToIdentifier(prop.api_name());
return SnakeCaseToCamelCase(result) + "Values";
@@ -219,8 +191,6 @@
writer.Write("%s\n\n", kDocs);
writer.Write("%s", kGeneratedFileFooterComments);
writer.Write("%s\n\n", kRustFileImports);
- writer.Write("%s\n\n", kError);
- writer.Write("%s\n\n", kResult);
for (int i = 0; i < props.prop_size(); ++i) {
const sysprop::Property& prop = props.prop(i);
@@ -296,8 +266,9 @@
if (prop.deprecated()) writer.Write("%s\n", kDeprecated);
// Escape prop id if it is similar to `type` keyword.
std::string identifier = (prop_id == "type") ? "r#" + prop_id : prop_id;
- writer.Write("pub fn %s() -> Result<Option<%s>> {\n", identifier.c_str(),
- prop_return_type.c_str());
+ writer.Write(
+ "pub fn %s() -> std::result::Result<Option<%s>, SysPropError> {\n",
+ identifier.c_str(), prop_return_type.c_str());
writer.Indent();
// Try original property.
writer.Write("let result = match system_properties::read(\"%s\") {\n",
@@ -345,8 +316,9 @@
"returns 'Ok' if successful.\n",
prop.prop_name().c_str());
if (prop.deprecated()) writer.Write("%s\n", kDeprecated);
- writer.Write("pub fn set_%s(v: %s) -> Result<()> {\n", prop_id.c_str(),
- prop_accept_type.c_str());
+ writer.Write(
+ "pub fn set_%s(v: %s) -> std::result::Result<(), SysPropError> {\n",
+ prop_id.c_str(), prop_accept_type.c_str());
writer.Indent();
std::string write_arg;
if (prop.type() == sysprop::String) {
@@ -381,7 +353,7 @@
return res.error();
}
- std::string lib_path = rust_output_dir + "/lib.rs";
+ std::string lib_path = rust_output_dir + "/mod.rs";
std::string lib_result = GenerateRustSource(props, scope);
if (!android::base::WriteStringToFile(lib_result, lib_path)) {
return ErrnoErrorf("Writing generated rust lib to {} failed", lib_path);
diff --git a/tests/RustGenTest.cpp b/tests/RustGenTest.cpp
index 606af00..827558b 100644
--- a/tests/RustGenTest.cpp
+++ b/tests/RustGenTest.cpp
@@ -99,46 +99,18 @@
)";
constexpr const char* kExpectedPublicOutput =
- R"(//! Autogenerated system properties.
+ R"(//! Autogenerated system property accessors.
//!
-//! This is autogenerated crate. The crate contains methods for easy access to
-//! the Android system properties.
+//! This is an autogenerated module. The module contains methods for typed access to
+//! Android system properties.
// Generated by the sysprop generator. DO NOT EDIT!
use std::fmt;
-use rustutils::system_properties;
-use rustutils::system_properties::parsers_formatters;
-
-/// Errors this crate could generate.
-#[derive(Debug)]
-pub enum SysPropError {
- /// Failed to fetch the system property.
- FetchError(system_properties::PropertyWatcherError),
- /// Failed to set the system property.
- SetError(system_properties::PropertyWatcherError),
- /// Failed to parse the system property value.
- ParseError(String),
-}
-
-impl fmt::Display for SysPropError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- SysPropError::FetchError(err) =>
- write!(f, "failed to fetch the system property: {}", err),
- SysPropError::SetError(err) =>
- write!(f, "failed to set the system property: {}", err),
- SysPropError::ParseError(err) =>
- write!(f, "failed to parse the system property value: {}", err),
- }
- }
-}
-
-/// Result type specific for this crate.
-pub type Result<T> = std::result::Result<T, SysPropError>;
+use rustutils::system_properties::{self, error::SysPropError, parsers_formatters};
/// Returns the value of the property 'android.test_int' if set.
-pub fn test_int() -> Result<Option<i32>> {
+pub fn test_int() -> std::result::Result<Option<i32>, SysPropError> {
let result = match system_properties::read("android.test_int") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -148,13 +120,13 @@
}
/// Sets the value of the property 'android.test_int', returns 'Ok' if successful.
-pub fn set_test_int(v: i32) -> Result<()> {
+pub fn set_test_int(v: i32) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format(&v);
system_properties::write("android.test_int", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'android.test.string' if set.
-pub fn test_string() -> Result<Option<String>> {
+pub fn test_string() -> std::result::Result<Option<String>, SysPropError> {
let result = match system_properties::read("android.test.string") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -170,7 +142,7 @@
}
/// Returns the value of the property 'ro.android.test.b' if set.
-pub fn test_boo_lea_n() -> Result<Option<bool>> {
+pub fn test_boo_lea_n() -> std::result::Result<Option<bool>, SysPropError> {
let result = match system_properties::read("ro.android.test.b") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_bool(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -180,13 +152,13 @@
}
/// Sets the value of the property 'ro.android.test.b', returns 'Ok' if successful.
-pub fn set_test_boo_lea_n(v: bool) -> Result<()> {
+pub fn set_test_boo_lea_n(v: bool) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_bool(&v);
system_properties::write("ro.android.test.b", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'android_os_test-long' if set.
-pub fn android_os_test_long() -> Result<Option<i64>> {
+pub fn android_os_test_long() -> std::result::Result<Option<i64>, SysPropError> {
let result = match system_properties::read("android_os_test-long") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -196,13 +168,13 @@
}
/// Sets the value of the property 'android_os_test-long', returns 'Ok' if successful.
-pub fn set_android_os_test_long(v: i64) -> Result<()> {
+pub fn set_android_os_test_long(v: i64) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format(&v);
system_properties::write("android_os_test-long", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'test_list_int' if set.
-pub fn test_list_int() -> Result<Option<Vec<i32>>> {
+pub fn test_list_int() -> std::result::Result<Option<Vec<i32>>, SysPropError> {
let result = match system_properties::read("test_list_int") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -212,14 +184,14 @@
}
/// Sets the value of the property 'test_list_int', returns 'Ok' if successful.
-pub fn set_test_list_int(v: &[i32]) -> Result<()> {
+pub fn set_test_list_int(v: &[i32]) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_list(v);
system_properties::write("test_list_int", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'test_strlist' if set.
#[deprecated]
-pub fn test_strlist() -> Result<Option<Vec<String>>> {
+pub fn test_strlist() -> std::result::Result<Option<Vec<String>>, SysPropError> {
let result = match system_properties::read("test_strlist") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -230,7 +202,7 @@
/// Sets the value of the property 'test_strlist', returns 'Ok' if successful.
#[deprecated]
-pub fn set_test_strlist(v: &[String]) -> Result<()> {
+pub fn set_test_strlist(v: &[String]) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_list(v);
system_properties::write("test_strlist", value.as_str()).map_err(SysPropError::SetError)
}
@@ -238,46 +210,18 @@
)";
constexpr const char* kExpectedInternalOutput =
- R"(//! Autogenerated system properties.
+ R"(//! Autogenerated system property accessors.
//!
-//! This is autogenerated crate. The crate contains methods for easy access to
-//! the Android system properties.
+//! This is an autogenerated module. The module contains methods for typed access to
+//! Android system properties.
// Generated by the sysprop generator. DO NOT EDIT!
use std::fmt;
-use rustutils::system_properties;
-use rustutils::system_properties::parsers_formatters;
-
-/// Errors this crate could generate.
-#[derive(Debug)]
-pub enum SysPropError {
- /// Failed to fetch the system property.
- FetchError(system_properties::PropertyWatcherError),
- /// Failed to set the system property.
- SetError(system_properties::PropertyWatcherError),
- /// Failed to parse the system property value.
- ParseError(String),
-}
-
-impl fmt::Display for SysPropError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- SysPropError::FetchError(err) =>
- write!(f, "failed to fetch the system property: {}", err),
- SysPropError::SetError(err) =>
- write!(f, "failed to set the system property: {}", err),
- SysPropError::ParseError(err) =>
- write!(f, "failed to parse the system property value: {}", err),
- }
- }
-}
-
-/// Result type specific for this crate.
-pub type Result<T> = std::result::Result<T, SysPropError>;
+use rustutils::system_properties::{self, error::SysPropError, parsers_formatters};
/// Returns the value of the property 'android.test_double' if set.
-pub fn test_double() -> Result<Option<f64>> {
+pub fn test_double() -> std::result::Result<Option<f64>, SysPropError> {
let result = match system_properties::read("android.test_double") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -287,13 +231,13 @@
}
/// Sets the value of the property 'android.test_double', returns 'Ok' if successful.
-pub fn set_test_double(v: f64) -> Result<()> {
+pub fn set_test_double(v: f64) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format(&v);
system_properties::write("android.test_double", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'android.test_int' if set.
-pub fn test_int() -> Result<Option<i32>> {
+pub fn test_int() -> std::result::Result<Option<i32>, SysPropError> {
let result = match system_properties::read("android.test_int") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -303,13 +247,13 @@
}
/// Sets the value of the property 'android.test_int', returns 'Ok' if successful.
-pub fn set_test_int(v: i32) -> Result<()> {
+pub fn set_test_int(v: i32) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format(&v);
system_properties::write("android.test_int", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'android.test.string' if set.
-pub fn test_string() -> Result<Option<String>> {
+pub fn test_string() -> std::result::Result<Option<String>, SysPropError> {
let result = match system_properties::read("android.test.string") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -368,7 +312,7 @@
}
/// Returns the value of the property 'android.test.enum' if set.
-pub fn test_enum() -> Result<Option<TestEnumValues>> {
+pub fn test_enum() -> std::result::Result<Option<TestEnumValues>, SysPropError> {
let result = match system_properties::read("android.test.enum") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -378,13 +322,13 @@
}
/// Sets the value of the property 'android.test.enum', returns 'Ok' if successful.
-pub fn set_test_enum(v: TestEnumValues) -> Result<()> {
+pub fn set_test_enum(v: TestEnumValues) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format(&v);
system_properties::write("android.test.enum", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'ro.android.test.b' if set.
-pub fn test_boo_lea_n() -> Result<Option<bool>> {
+pub fn test_boo_lea_n() -> std::result::Result<Option<bool>, SysPropError> {
let result = match system_properties::read("ro.android.test.b") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_bool(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -394,13 +338,13 @@
}
/// Sets the value of the property 'ro.android.test.b', returns 'Ok' if successful.
-pub fn set_test_boo_lea_n(v: bool) -> Result<()> {
+pub fn set_test_boo_lea_n(v: bool) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_bool(&v);
system_properties::write("ro.android.test.b", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'android_os_test-long' if set.
-pub fn android_os_test_long() -> Result<Option<i64>> {
+pub fn android_os_test_long() -> std::result::Result<Option<i64>, SysPropError> {
let result = match system_properties::read("android_os_test-long") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -410,13 +354,13 @@
}
/// Sets the value of the property 'android_os_test-long', returns 'Ok' if successful.
-pub fn set_android_os_test_long(v: i64) -> Result<()> {
+pub fn set_android_os_test_long(v: i64) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format(&v);
system_properties::write("android_os_test-long", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'test_double_list' if set.
-pub fn test_double_list() -> Result<Option<Vec<f64>>> {
+pub fn test_double_list() -> std::result::Result<Option<Vec<f64>>, SysPropError> {
let result = match system_properties::read("test_double_list") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -426,13 +370,13 @@
}
/// Sets the value of the property 'test_double_list', returns 'Ok' if successful.
-pub fn set_test_double_list(v: &[f64]) -> Result<()> {
+pub fn set_test_double_list(v: &[f64]) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_list(v);
system_properties::write("test_double_list", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'test_list_int' if set.
-pub fn test_list_int() -> Result<Option<Vec<i32>>> {
+pub fn test_list_int() -> std::result::Result<Option<Vec<i32>>, SysPropError> {
let result = match system_properties::read("test_list_int") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -442,14 +386,14 @@
}
/// Sets the value of the property 'test_list_int', returns 'Ok' if successful.
-pub fn set_test_list_int(v: &[i32]) -> Result<()> {
+pub fn set_test_list_int(v: &[i32]) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_list(v);
system_properties::write("test_list_int", value.as_str()).map_err(SysPropError::SetError)
}
/// Returns the value of the property 'test_strlist' if set.
#[deprecated]
-pub fn test_strlist() -> Result<Option<Vec<String>>> {
+pub fn test_strlist() -> std::result::Result<Option<Vec<String>>, SysPropError> {
let result = match system_properties::read("test_strlist") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -460,7 +404,7 @@
/// Sets the value of the property 'test_strlist', returns 'Ok' if successful.
#[deprecated]
-pub fn set_test_strlist(v: &[String]) -> Result<()> {
+pub fn set_test_strlist(v: &[String]) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_list(v);
system_properties::write("test_strlist", value.as_str()).map_err(SysPropError::SetError)
}
@@ -498,7 +442,7 @@
/// Returns the value of the property 'el' if set.
#[deprecated]
-pub fn el() -> Result<Option<Vec<ElValues>>> {
+pub fn el() -> std::result::Result<Option<Vec<ElValues>>, SysPropError> {
let result = match system_properties::read("el") {
Err(e) => Err(SysPropError::FetchError(e)),
Ok(Some(val)) => parsers_formatters::parse_list(val.as_str()).map_err(SysPropError::ParseError).map(Some),
@@ -509,7 +453,7 @@
/// Sets the value of the property 'el', returns 'Ok' if successful.
#[deprecated]
-pub fn set_el(v: &[ElValues]) -> Result<()> {
+pub fn set_el(v: &[ElValues]) -> std::result::Result<(), SysPropError> {
let value = parsers_formatters::format_list(v);
system_properties::write("el", value.as_str()).map_err(SysPropError::SetError)
}
@@ -537,7 +481,7 @@
};
for (auto [scope, expected_output] : tests) {
- std::string rust_output_path = temp_dir.path + "/lib.rs"s;
+ std::string rust_output_path = temp_dir.path + "/mod.rs"s;
ASSERT_RESULT_OK(GenerateRustLibrary(temp_file.path, scope, temp_dir.path));