1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use log::debug;
6use objects::ObexObjectError as Error;
78pub mod event_report;
9pub mod messages_listing;
1011/// The ISO 8601 time format used in the Time Header packet.
12/// The format is YYYYMMDDTHHMMSS where "T" delimits the date from the time.
13// TODO(b/348051261): support UTC timestamp.
14pub(crate) const ISO_8601_TIME_FORMAT: &str = "%Y%m%dT%H%M%S";
1516/// Some string values have byte data length limit.
17/// We truncate the strings to fit that limit if necessary.
18// TODO(b/348051603): use `floor_char_boundary` instead once it stabilizes.
19pub(crate) fn truncate_string(value: &String, max_len: usize) -> String {
20let mut v = value.clone();
21if v.len() <= max_len {
22return v;
23 }
24let mut l = max_len;
25while !v.is_char_boundary(l) {
26 l -= 1;
27 }
28 v.truncate(l);
29debug!("truncated string value from length {} to {}", value.len(), v.len());
30 v
31}
3233// Converts the "yes" / "no" values to corresponding boolean.
34pub(crate) fn str_to_bool(val: &str) -> Result<bool, Error> {
35match val {
36"yes" => Ok(true),
37"no" => Ok(false),
38 val => Err(Error::invalid_data(val)),
39 }
40}
4142pub(crate) fn bool_to_string(val: bool) -> String {
43if val {
44"yes".to_string()
45 } else {
46"no".to_string()
47 }
48}