bt_map/packets/
mod.rs

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.
4
5use log::debug;
6use objects::ObexObjectError as Error;
7
8pub mod event_report;
9pub mod messages_listing;
10
11/// 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";
15
16/// 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 {
20    let mut v = value.clone();
21    if v.len() <= max_len {
22        return v;
23    }
24    let mut l = max_len;
25    while !v.is_char_boundary(l) {
26        l -= 1;
27    }
28    v.truncate(l);
29    debug!("truncated string value from length {} to {}", value.len(), v.len());
30    v
31}
32
33// Converts the "yes" / "no" values to corresponding boolean.
34pub(crate) fn str_to_bool(val: &str) -> Result<bool, Error> {
35    match val {
36        "yes" => Ok(true),
37        "no" => Ok(false),
38        val => Err(Error::invalid_data(val)),
39    }
40}
41
42pub(crate) fn bool_to_string(val: bool) -> String {
43    if val {
44        "yes".to_string()
45    } else {
46        "no".to_string()
47    }
48}