|
1 | | -use anyhow::Result; |
| 1 | +pub mod detect; |
| 2 | +use anyhow::{anyhow, Result}; |
| 3 | +use std::path::PathBuf; |
2 | 4 |
|
3 | | -pub fn get_platform_user_dir() -> Result<String> { |
| 5 | +/// 获取用户目录 |
| 6 | +pub fn get_platform_user_dir() -> Result<PathBuf> { |
4 | 7 | #[cfg(target_os = "windows")] |
5 | 8 | let user_dir = std::env::var("USERPROFILE") |
6 | | - .map_err(|e| anyhow::anyhow!("Failed to get USERPROFILE environment variable: {}", e))?; |
| 9 | + .map_err(|e| anyhow!("Failed to get USERPROFILE: {}", e))?; |
7 | 10 |
|
8 | | - #[cfg(target_os = "linux")] |
| 11 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
9 | 12 | let user_dir = std::env::var("HOME") |
10 | | - .map_err(|e| anyhow::anyhow!("Failed to get HOME environment variable: {}", e))?; |
| 13 | + .map_err(|e| anyhow!("Failed to get HOME: {}", e))?; |
11 | 14 |
|
12 | | - #[cfg(target_os = "macos")] |
13 | | - let user_dir = std::env::var("HOME") |
14 | | - .map_err(|e| anyhow::anyhow!("Failed to get HOME environment variable: {}", e))?; |
| 15 | + Ok(PathBuf::from(user_dir)) |
| 16 | +} |
| 17 | + |
| 18 | +/// 获取系统 temp 目录 |
| 19 | +pub fn get_os_temp_dir() -> Result<PathBuf> { |
| 20 | + #[cfg(target_os = "windows")] |
| 21 | + { |
| 22 | + let temp_dir = std::env::var("TEMP") |
| 23 | + .map_err(|e| anyhow!("Failed to get TEMP: {}", e))?; |
| 24 | + |
| 25 | + Ok(PathBuf::from(temp_dir)) |
| 26 | + } |
| 27 | + |
| 28 | + #[cfg(any(target_os = "linux", target_os = "macos"))] |
| 29 | + { |
| 30 | + Ok(PathBuf::from("/tmp")) |
| 31 | + } |
15 | 32 |
|
16 | | - Ok(user_dir) |
| 33 | + #[cfg(not(any( |
| 34 | + target_os = "windows", |
| 35 | + target_os = "linux", |
| 36 | + target_os = "macos" |
| 37 | + )))] |
| 38 | + { |
| 39 | + Err(anyhow!("Unsupported operating system")) |
| 40 | + } |
17 | 41 | } |
18 | 42 |
|
19 | | -pub fn get_platform_app_dir() -> Result<String> { |
20 | | - let user_dir = get_platform_user_dir()?; |
21 | | - let app_dir = format!("{}/.omega", user_dir); |
22 | | - std::fs::create_dir_all(&app_dir) |
23 | | - .map_err(|e| anyhow::anyhow!("Failed to create app directory '{}': {}", app_dir, e))?; |
| 43 | +/// 获取应用目录 |
| 44 | +pub fn get_platform_app_dir() -> Result<PathBuf> { |
| 45 | + let app_dir = get_platform_user_dir()?.join(".omega"); |
| 46 | + |
| 47 | + std::fs::create_dir_all(&app_dir).map_err(|e| { |
| 48 | + anyhow!( |
| 49 | + "Failed to create app directory '{}': {}", |
| 50 | + app_dir.display(), |
| 51 | + e |
| 52 | + ) |
| 53 | + })?; |
| 54 | + |
24 | 55 | Ok(app_dir) |
25 | 56 | } |
26 | 57 |
|
27 | | -pub fn get_database_path() -> Result<String> { |
28 | | - let app_dir = get_platform_app_dir()?; |
29 | | - let db_path = format!("{}/omega_code.db", app_dir); |
30 | | - Ok(db_path) |
| 58 | +/// 获取 runtime 目录 |
| 59 | +pub fn get_app_runtime_dir() -> Result<PathBuf> { |
| 60 | + let runtime_dir = get_platform_app_dir()?.join("runtime"); |
| 61 | + |
| 62 | + std::fs::create_dir_all(&runtime_dir).map_err(|e| { |
| 63 | + anyhow!( |
| 64 | + "Failed to create runtime directory '{}': {}", |
| 65 | + runtime_dir.display(), |
| 66 | + e |
| 67 | + ) |
| 68 | + })?; |
| 69 | + |
| 70 | + Ok(runtime_dir) |
| 71 | +} |
| 72 | + |
| 73 | +/// 获取数据库路径 |
| 74 | +pub fn get_database_path() -> Result<PathBuf> { |
| 75 | + Ok( |
| 76 | + get_platform_app_dir()? |
| 77 | + .join("omega_code.db") |
| 78 | + ) |
31 | 79 | } |
32 | 80 |
|
33 | 81 | #[macro_export] |
|
0 commit comments