devices/sys/
linux.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::path::PathBuf;
6
7pub(crate) mod serial_device;
8
9/// Parses a wayland socket path with optional name parameter (e.g. "PATH[,name=NAME]").
10pub fn parse_wayland_sock(value: &str) -> Result<(String, PathBuf), String> {
11    let mut components = value.split(',');
12    let path = PathBuf::from(match components.next() {
13        None => return Err("missing socket path".to_string()),
14        Some(c) => c,
15    });
16    let mut name = "";
17    for c in components {
18        let mut kv = c.splitn(2, '=');
19        let (kind, value) = match (kv.next(), kv.next()) {
20            (Some(kind), Some(value)) => (kind, value),
21            _ => return Err(format!("option must be of the form `kind=value`: {c}")),
22        };
23        match kind {
24            "name" => name = value,
25            _ => return Err(format!("unrecognized option: {kind}")),
26        }
27    }
28
29    Ok((name.to_string(), path))
30}