Skip to main content

intel_crashlog/collateral/
path.rs

1// Copyright (C) 2025 Intel Corporation
2// SPDX-License-Identifier: MIT
3
4#[cfg(not(feature = "std"))]
5use alloc::{fmt, str::FromStr, string::String, vec::Vec};
6#[cfg(feature = "std")]
7use std::{fmt, path::PathBuf, str::FromStr};
8
9/// A path within a [`crate::collateral::PVSS`] to an item stored in the collateral tree.
10#[derive(Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
11pub struct ItemPath(Vec<String>);
12
13impl ItemPath {
14    pub fn new<const N: usize>(path: [&str; N]) -> Self {
15        ItemPath(path.into_iter().map(String::from).collect())
16    }
17
18    pub(crate) fn push(&mut self, element: &str) {
19        self.0.push(element.into())
20    }
21}
22
23#[cfg(feature = "std")]
24impl From<&ItemPath> for PathBuf {
25    fn from(path: &ItemPath) -> Self {
26        path.0.iter().collect()
27    }
28}
29
30impl FromStr for ItemPath {
31    type Err = ();
32
33    fn from_str(path: &str) -> Result<Self, Self::Err> {
34        Ok(Self(path.split('/').map(String::from).collect()))
35    }
36}
37
38impl From<&str> for ItemPath {
39    fn from(path: &str) -> Self {
40        path.parse().unwrap()
41    }
42}
43
44impl fmt::Display for ItemPath {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        for (i, path) in self.0.iter().enumerate() {
47            write!(f, "{path}")?;
48            if i < self.0.len() - 1 {
49                write!(f, "/")?;
50            }
51        }
52        Ok(())
53    }
54}