ailloli_ui_fs/
entry.rs

1//! Directory-entry values returned by providers and tree sources.
2
3use crate::{FileMetadata, FileUri};
4
5/// One listed filesystem entry with URI-derived display name and metadata.
6///
7/// The public fields can be changed independently, so consumers must not assume
8/// `name` still matches `uri` after arbitrary construction or mutation.
9///
10/// # Examples
11///
12/// ```
13/// use ailloli_ui_fs::{FileEntry, FileKind, FileMetadata, FileUri};
14/// let entry = FileEntry::new(FileUri::parse("file:///tmp/main%20file.rs")?, FileMetadata::new(FileKind::File));
15/// assert_eq!(entry.name, "main%20file.rs");
16/// # Ok::<(), ailloli_ui_fs::FileError>(())
17/// ```
18#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
19pub struct FileEntry {
20    /// Stable provider URI for the entry.
21    pub uri: FileUri,
22    /// Provider/display name; [`Self::new`] uses the final encoded URI segment.
23    pub name: String,
24    /// Metadata snapshot associated with the listing.
25    pub metadata: FileMetadata,
26}
27
28impl FileEntry {
29    /// Creates an entry whose name is the URI's last non-empty encoded segment.
30    ///
31    /// A root URI has no file name and therefore produces an empty name. Percent
32    /// escapes are not decoded; use [`FileUri::file_name_decoded`] when needed.
33    ///
34    /// # Examples
35    ///
36    /// ```
37    /// use ailloli_ui_fs::{FileEntry, FileKind, FileMetadata, FileUri};
38    /// let root = FileEntry::new(FileUri::parse("file:///")?, FileMetadata::new(FileKind::Directory));
39    /// assert!(root.name.is_empty());
40    /// # Ok::<(), ailloli_ui_fs::FileError>(())
41    /// ```
42    pub fn new(uri: FileUri, metadata: FileMetadata) -> Self {
43        let name = uri.file_name().unwrap_or_default().to_string();
44        Self {
45            uri,
46            name,
47            metadata,
48        }
49    }
50}