littlefs2_core/lib.rs
1#![no_std]
2
3//! Core types for the [`littlefs2`][] crate.
4//!
5//! See the documentation for [`littlefs2`][] for more information.
6//!
7//! [`littlefs2`]: https://docs.rs/littlefs2
8
9mod fs;
10mod io;
11mod object_safe;
12mod path;
13
14pub use fs::{Attribute, DirEntry, FileOpenFlags, FileType, Metadata};
15pub use io::{Error, OpenSeekFrom, Read, Result, Seek, SeekFrom, Write};
16pub use object_safe::{DirEntriesCallback, DynFile, DynFilesystem, FileCallback, Predicate, Vec};
17pub use path::{Ancestors, Iter, Path, PathBuf, PathError};
18
19/// Creates a path from a string without a trailing null.
20///
21/// Panics and causes a compiler error if the string contains null bytes or non-ascii characters.
22///
23/// # Examples
24///
25/// ```
26/// use littlefs2_core::{path, Path};
27///
28/// const HOME: &Path = path!("/home");
29/// let root = path!("/");
30/// ```
31///
32/// Illegal values:
33///
34/// ```compile_fail
35/// # use littlefs2_core::{path, Path};
36/// const WITH_NULL: &Path = path!("/h\0me"); // does not compile
37/// ```
38///
39/// ```compile_fail
40/// # use littlefs2_core::{path, Path};
41/// const WITH_UTF8: &Path = path!("/höme"); // does not compile
42/// ```
43///
44/// The macro enforces const evaluation so that compilation fails for illegal values even if the
45/// macro is not used in a const context:
46///
47/// ```compile_fail
48/// # use littlefs2_core::path;
49/// let path = path!("te\0st"); // does not compile
50/// ```
51#[macro_export]
52macro_rules! path {
53 ($path:literal) => {{
54 const _PATH: &$crate::Path =
55 match $crate::Path::from_str_with_nul(::core::concat!($path, "\0")) {
56 Ok(path) => path,
57 Err(_) => panic!("invalid littlefs2 path"),
58 };
59 _PATH
60 }};
61}
62
63/// Creates an owned path from a string without a trailing null.
64///
65/// Panics and causes a compiler error if the string contains null bytes or non-ascii characters.
66///
67/// # Examples
68///
69/// ```
70/// use littlefs2_core::{path_buf, PathBuf};
71///
72/// const HOME: PathBuf = path_buf!("/home");
73/// let root = path_buf!("/");
74/// ```
75///
76/// Illegal values:
77///
78/// ```compile_fail
79/// # use littlefs2_core::{path_buf, PathBuf};
80/// const WITH_NULL: PathBuf = path_buf!("/h\0me"); // does not compile
81/// ```
82///
83/// ```compile_fail
84/// # use littlefs2_core::{path_buf, PathBuf};
85/// const WITH_UTF8: PathBuf = path_buf!("/höme"); // does not compile
86/// ```
87///
88/// The macro enforces const evaluation so that compilation fails for illegal values even if the
89/// macro is not used in a const context:
90///
91/// ```compile_fail
92/// # use littlefs2_core::path_buf;
93/// let path = path_buf!("te\0st"); // does not compile
94/// ```
95#[macro_export]
96macro_rules! path_buf {
97 ($path:literal) => {{
98 const _PATH: $crate::PathBuf =
99 match $crate::Path::from_str_with_nul(::core::concat!($path, "\0")) {
100 Ok(path) => $crate::PathBuf::from_path(path),
101 Err(_) => panic!("invalid littlefs2 path"),
102 };
103 _PATH
104 }};
105}