Skip to main content

qubit_fs/metadata/
file_system_id.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Stable configured filesystem identity.
9
10use std::fmt::Display;
11use std::fmt::Formatter;
12use std::fmt::Result as FmtResult;
13
14use crate::error::FsError;
15use crate::error::FsErrorKind;
16use crate::error::FsOperation;
17use crate::error::FsResult;
18
19/// Stable identity of one configured filesystem object.
20///
21/// # Examples
22///
23/// ```rust
24/// use qubit_fs::metadata::FileSystemId;
25///
26/// let id = FileSystemId::new("local-instance")?;
27/// assert_eq!("local-instance", id.as_str());
28/// # Ok::<(), qubit_fs::FsError>(())
29/// ```
30#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
31pub struct FileSystemId(
32    /// Validated provider-supplied identity text.
33    Box<str>,
34);
35
36impl FileSystemId {
37    /// Validates a filesystem identity supplied by a provider.
38    ///
39    /// # Errors
40    ///
41    /// Returns [`FsErrorKind::InvalidOptions`] for empty identities or control
42    /// characters.
43    pub fn new(id: &str) -> FsResult<Self> {
44        if id.is_empty() || id.chars().any(char::is_control) {
45            return Err(FsError::new(
46                FsErrorKind::InvalidOptions,
47                FsOperation::Provider,
48                "filesystem id must be non-empty and contain no controls",
49            ));
50        }
51        Ok(Self(id.into()))
52    }
53
54    /// Returns the provider-supplied stable identity.
55    #[inline]
56    #[must_use]
57    pub fn as_str(&self) -> &str {
58        &self.0
59    }
60}
61
62impl Display for FileSystemId {
63    #[inline]
64    fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
65        formatter.write_str(self.as_str())
66    }
67}