orinium_browser/platform/io/
mod.rs

1//! ファイルI/O関連の機能を提供するモジュール
2
3use anyhow::{Context, Result};
4use std::fs;
5use std::path::PathBuf;
6
7#[allow(dead_code)]
8pub fn load_local_file(path: &str) -> Result<Vec<u8>> {
9    fs::read(path).with_context(|| format!("Failed to read file: {path}"))
10}
11
12/// リソースファイルを探して読み込む。
13/// 順序は以下の通り:
14/// - ./resource/<rel_path>
15/// - 実行ファイルのあるディレクトリ/resource/<rel_path>
16/// - カレントディレクトリ/resource/<rel_path>
17pub fn load_resource(rel_path: &str) -> Result<Vec<u8>> {
18    let mut candidates: Vec<PathBuf> = Vec::new();
19
20    // ./resource/<rel_path>
21    candidates.push(PathBuf::from("resource").join(rel_path));
22
23    // executable directory/resource/<rel_path>
24    if let Ok(exe) = std::env::current_exe()
25        && let Some(dir) = exe.parent()
26    {
27        candidates.push(dir.join("resource").join(rel_path));
28    }
29
30    // current_dir()/resource/<rel_path>
31    if let Ok(cd) = std::env::current_dir() {
32        candidates.push(cd.join("resource").join(rel_path));
33    }
34
35    for cand in candidates {
36        if cand.is_file() {
37            return fs::read(&cand).with_context(|| format!("Failed to read resource {:?}", cand));
38        }
39    }
40
41    anyhow::bail!("Resource not found: {}", rel_path)
42}