rust_unixfs/dir/
builder.rs1use core::fmt;
2use ipld_core::cid::Cid;
3
4mod dir_builder;
5use dir_builder::DirBuilder;
6
7mod iter;
8pub use iter::{OwnedTreeNode, PostOrderIterator, TreeNode};
9
10mod buffered;
11pub use buffered::BufferingTreeBuilder;
12
13mod custom_pb;
14use custom_pb::CustomFlatUnixFs;
15
16enum Entry {
17 Leaf(Leaf),
18 Directory(DirBuilder),
19}
20
21impl fmt::Debug for Entry {
22 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
23 use Entry::*;
24
25 match self {
26 Leaf(leaf) => write!(fmt, "Leaf {{ {leaf:?} }}"),
27 Directory(_) => write!(fmt, "DirBuilder {{ .. }}"),
28 }
29 }
30}
31
32impl Entry {
33 fn as_dir_builder(&mut self) -> Result<&mut DirBuilder, ()> {
34 use Entry::*;
35 match self {
36 Directory(ref mut d) => Ok(d),
37 _ => Err(()),
38 }
39 }
40}
41
42struct Leaf {
43 link: Cid,
44 total_size: u64,
45}
46
47impl fmt::Debug for Leaf {
48 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
49 write!(fmt, "{}, {}", self.link, self.total_size)
50 }
51}
52
53#[derive(Debug, Clone)]
55pub struct TreeOptions {
56 block_size_limit: Option<u64>,
57 wrap_with_directory: bool,
58}
59
60impl Default for TreeOptions {
61 fn default() -> Self {
62 TreeOptions {
63 block_size_limit: Some(512 * 1024),
64 wrap_with_directory: false,
65 }
66 }
67}
68
69impl TreeOptions {
70 pub fn block_size_limit(&mut self, limit: Option<u64>) {
73 self.block_size_limit = limit;
74 }
75
76 pub fn wrap_with_directory(&mut self) {
79 self.wrap_with_directory = true;
80 }
81}
82
83#[derive(Debug)]
85pub enum TreeBuildingFailed {
86 RootedPath(String),
88 RepeatSlashesInPath(String),
90 PathEndsInSlash(String),
92 TooManyRootLevelEntries,
95 DuplicatePath(String),
97 LeafAsDirectory(String),
99}
100
101impl fmt::Display for TreeBuildingFailed {
102 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
103 use TreeBuildingFailed::*;
104
105 match self {
106 RootedPath(s) => write!(fmt, "path is rooted: {s:?}"),
107 RepeatSlashesInPath(s) => write!(fmt, "path contains repeat slashes: {s:?}"),
108 PathEndsInSlash(s) => write!(fmt, "path ends in a slash: {s:?}"),
109 TooManyRootLevelEntries => write!(
110 fmt,
111 "multiple root level entries while configured wrap_with_directory = false"
112 ),
113 DuplicatePath(s) => write!(fmt, "path exists already: {s:?}"),
115 LeafAsDirectory(s) => write!(
116 fmt,
117 "attempted to use already added leaf as a subdirectory: {s:?}"
118 ),
119 }
120 }
121}
122
123impl std::error::Error for TreeBuildingFailed {}
124
125#[derive(Debug)]
127pub enum TreeConstructionFailed {
128 Protobuf(quick_protobuf::Error),
130 TooLargeBlock(u64),
133}
134
135impl fmt::Display for TreeConstructionFailed {
136 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
137 use TreeConstructionFailed::*;
138
139 match self {
140 Protobuf(e) => write!(fmt, "serialization failed: {e}"),
141 TooLargeBlock(size) => write!(fmt, "attempted to create block of {size} bytes"),
142 }
143 }
144}
145
146impl std::error::Error for TreeConstructionFailed {}
147
148#[derive(Debug)]
149struct NamedLeaf(String, Cid, u64);