Skip to main content

khora_io/asset/
io.rs

1// Copyright 2025 eraflo
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Abstraction over asset I/O backends.
16
17use anyhow::Result;
18use khora_core::asset::AssetSource;
19use std::path::Path;
20
21/// Trait for asset I/O backends (file system or pack archive).
22///
23/// Implementations handle the low-level reading of raw bytes from storage.
24/// The `AssetService` dispatches to this trait based on the `AssetSource` variant.
25pub trait AssetIo: Send + Sync {
26    /// Loads raw bytes from the given asset source.
27    fn load_bytes(&mut self, source: &AssetSource) -> Result<Vec<u8>>;
28}
29
30/// Sibling trait to [`AssetIo`] for editor-time *writing* of assets back to
31/// storage. Implemented only by [`crate::asset::FileLoader`] — release builds
32/// (`PackLoader`) are intentionally read-only, which is why `AssetWriter`
33/// lives separately rather than extending `AssetIo`.
34///
35/// Used by the editor's `ProjectVfs` to persist scene saves and any other
36/// asset mutation through the same root path the FileLoader reads from.
37pub trait AssetWriter: Send + Sync {
38    /// Writes `bytes` to a path **relative to the loader's root**. Creates
39    /// intermediate directories as needed. The relative path is the same
40    /// shape that `AssetSource::Path(rel)` records — `IndexBuilder` will
41    /// see the new file on the next scan and assign it a stable UUID.
42    fn write_bytes(&self, rel_path: &Path, bytes: &[u8]) -> Result<()>;
43}