ipfs_unixfs/dir/
builder.rs1use cid::Cid;
2use core::fmt;
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),
65 wrap_with_directory: false,
66 }
67 }
68}
69
70impl TreeOptions {
71 pub fn block_size_limit(&mut self, limit: Option<u64>) {
74 self.block_size_limit = limit;
75 }
76
77 pub fn wrap_with_directory(&mut self) {
80 self.wrap_with_directory = true;
81 }
82}
83
84#[derive(Debug)]
86pub enum TreeBuildingFailed {
87 RootedPath(String),
89 RepeatSlashesInPath(String),
91 PathEndsInSlash(String),
93 TooManyRootLevelEntries,
96 DuplicatePath(String),
98 LeafAsDirectory(String),
100}
101
102impl fmt::Display for TreeBuildingFailed {
103 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
104 use TreeBuildingFailed::*;
105
106 match self {
107 RootedPath(s) => write!(fmt, "path is rooted: {:?}", s),
108 RepeatSlashesInPath(s) => write!(fmt, "path contains repeat slashes: {:?}", s),
109 PathEndsInSlash(s) => write!(fmt, "path ends in a slash: {:?}", s),
110 TooManyRootLevelEntries => write!(
111 fmt,
112 "multiple root level entries while configured wrap_with_directory = false"
113 ),
114 DuplicatePath(s) => write!(fmt, "path exists already: {:?}", s),
116 LeafAsDirectory(s) => write!(
117 fmt,
118 "attempted to use already added leaf as a subdirectory: {:?}",
119 s
120 ),
121 }
122 }
123}
124
125impl std::error::Error for TreeBuildingFailed {}
126
127#[derive(Debug)]
129pub enum TreeConstructionFailed {
130 Protobuf(quick_protobuf::Error),
132 TooLargeBlock(u64),
135}
136
137impl fmt::Display for TreeConstructionFailed {
138 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
139 use TreeConstructionFailed::*;
140
141 match self {
142 Protobuf(e) => write!(fmt, "serialization failed: {}", e),
143 TooLargeBlock(size) => write!(fmt, "attempted to create block of {} bytes", size),
144 }
145 }
146}
147
148impl std::error::Error for TreeConstructionFailed {}
149
150#[derive(Debug)]
151struct NamedLeaf(String, Cid, u64);