1use std::fs;
4use std::path::PathBuf;
5use crate::utils::me_error::MeError;
6#[cfg(feature = "sqlite")]
7use rusqlite;
8#[derive(Debug)]
9pub struct MeSummary {
10 pub username: String,
11 pub path: PathBuf,
12}
13
14#[cfg(feature = "sqlite")]
16pub fn load_public(username: &str) -> Result<(String, String), MeError> {
17 let home = dirs::home_dir().ok_or_else(|| MeError::Validation("No HOME dir".to_string()))?;
18 let db_path = home.join(".this").join("me").join(username).join(format!("{}.db", username));
19 if !db_path.exists() {
20 return Err(MeError::Validation("Identity does not exist.".to_string()));
21 }
22
23 let conn = rusqlite::Connection::open(&db_path).map_err(MeError::Database)?;
24 let mut stmt = conn.prepare("SELECT public_key FROM identity_keys LIMIT 1")
25 .map_err(MeError::Database)?;
26 let mut rows = stmt.query([]).map_err(MeError::Database)?;
27 if let Some(row) = rows.next().map_err(MeError::Database)? {
28 let public_key: String = row.get(0).map_err(MeError::Database)?;
29 Ok((username.to_string(), public_key))
30 } else {
31 Err(MeError::Validation("Public key not found.".to_string()))
32 }
33}
34
35pub fn list_us() -> Result<Vec<MeSummary>, MeError> {
36 let home = dirs::home_dir().ok_or_else(|| MeError::Validation("No HOME dir".to_string()))?;
37 let base_path = home.join(".this").join("me");
38 let mut list = vec![];
39 if let Ok(entries) = fs::read_dir(base_path) {
40 for entry in entries.flatten() {
41 if entry.path().is_dir() {
42 if let Some(username) = entry.file_name().to_str() {
43 list.push(MeSummary {
44 username: username.to_string(),
45 path: entry.path(),
46 });
47 }
48 }
49 }
50 }
51
52 Ok(list)
53}