Skip to main content

asset_io/
walk.rs

1// Copyright 2026 Adobe. All rights reserved.
2// This file is licensed to you under the Apache License,
3// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
4// or the MIT license (http://opensource.org/licenses/MIT),
5// at your option.
6
7// Unless required by applicable law or agreed to in writing,
8// this software is distributed on an "AS IS" BASIS, WITHOUT
9// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
10// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
11// specific language governing permissions and limitations under
12// each license.
13
14//! Hierarchical view of a container.
15
16use crate::{range::ByteRange, segment::Name};
17
18/// One element met while [walking](crate::Structure::walk) a container's tree.
19///
20/// Flat containers report one node per segment at depth 1. Containers that nest
21/// elements (ISO BMFF boxes) also report the nested ones, which are not segments:
22/// segments tile the file once, nodes describe its hierarchy.
23#[derive(Debug, Clone, Copy)]
24#[non_exhaustive]
25pub struct Node<'a> {
26    /// Slash-separated names from the root, for example `/moov/trak/mdia`. Valid
27    /// only during the callback.
28    pub path: &'a str,
29    /// The element's own name.
30    pub name: Name,
31    /// Whole element, header through end.
32    pub range: ByteRange,
33    /// Bytes of framing before the content: size, type, extended size and
34    /// extended type. A FullBox's version and flags are reported separately.
35    pub header_len: u32,
36    /// 1 for top-level elements.
37    pub depth: u32,
38    /// FullBox version, if the element has one.
39    pub version: Option<u8>,
40    /// FullBox flags, if the element has them.
41    pub flags: Option<u32>,
42    /// Extended type of a `uuid` box.
43    pub user_type: Option<[u8; 16]>,
44}
45
46impl<'a> Node<'a> {
47    /// A node with no FullBox fields or user type; add them with the `with_*` methods.
48    pub const fn new(
49        path: &'a str,
50        name: Name,
51        range: ByteRange,
52        header_len: u32,
53        depth: u32,
54    ) -> Self {
55        Self {
56            path,
57            name,
58            range,
59            header_len,
60            depth,
61            version: None,
62            flags: None,
63            user_type: None,
64        }
65    }
66
67    /// Sets the FullBox version and flags.
68    pub const fn with_full_box(mut self, version: u8, flags: u32) -> Self {
69        self.version = Some(version);
70        self.flags = Some(flags);
71        self
72    }
73
74    /// Sets the extended type of a `uuid` box.
75    pub const fn with_user_type(mut self, user_type: [u8; 16]) -> Self {
76        self.user_type = Some(user_type);
77        self
78    }
79}