routing/
subdir.rs

1// Copyright 2024 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
5//! Utilities for working with subdirectories.
6
7use std::fmt;
8
9use cm_types::{ParseError, RelativePath};
10
11/// A subdirectory of a directory capability.
12#[derive(Eq, Ord, PartialOrd, PartialEq, Debug, Hash, Clone, Default)]
13pub struct SubDir(RelativePath);
14
15impl SubDir {
16    pub fn new(path: impl AsRef<str>) -> Result<Self, ParseError> {
17        let path = RelativePath::new(path)?;
18        Ok(SubDir(path))
19    }
20
21    pub fn dot() -> Self {
22        Self(RelativePath::dot())
23    }
24}
25
26impl AsRef<RelativePath> for SubDir {
27    fn as_ref(&self) -> &RelativePath {
28        &self.0
29    }
30}
31
32impl AsMut<RelativePath> for SubDir {
33    fn as_mut(&mut self) -> &mut RelativePath {
34        &mut self.0
35    }
36}
37
38impl From<RelativePath> for SubDir {
39    fn from(value: RelativePath) -> Self {
40        Self(value)
41    }
42}
43
44impl Into<RelativePath> for SubDir {
45    fn into(self) -> RelativePath {
46        let Self(path) = self;
47        path
48    }
49}
50
51impl fmt::Display for SubDir {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        self.0.fmt(f)
54    }
55}