Skip to main content

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.
18pub(crate) fn truncate_string(value: &String, max_len: usize) -> String {
19    let mut v = value.clone();
20    if v.len() <= max_len {
21        return v;
22    }
23
24    let l = v.floor_char_boundary(max_len);
25    v.truncate(l);
26
27    debug!("truncated string value from length {} to {}", value.len(), v.len());
28    v
29}
30
31// Converts the "yes" / "no" values to corresponding boolean.
32pub(crate) fn str_to_bool(val: &str) -> Result<bool, Error> {
33    match val {
34        "yes" => Ok(true),
35        "no" => Ok(false),
36        val => Err(Error::invalid_data(val)),
37    }
38}
39
40pub(crate) fn bool_to_string(val: bool) -> String {
41    if val { "yes".to_string() } else { "no".to_string() }
42}