Skip to content

Commit ed640a9

Browse files
committed
Define ConfigDirectory#default_profile().
1 parent 8d8562a commit ed640a9

10 files changed

Lines changed: 122 additions & 7 deletions

File tree

lib/asimov-directory/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ use asimov_directory::{ModuleNameIterator, fs::StateDirectory};
9292

9393
#[tokio::main]
9494
async fn main() -> Result<(), Box<dyn std::error::Error>> {
95-
let modules_dir = StateDirectory::home()?.modules()?;
96-
let mut module_names = modules_dir.iter_installed().await?;
95+
let modules = StateDirectory::home()?.modules()?;
96+
let mut module_names = modules.iter_installed().await?;
9797
while let Some(module_name) = module_names.next().await {
9898
println!("{}", module_name);
9999
}

lib/asimov-directory/examples/list_configs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use asimov_directory::fs::StateDirectory;
44

55
#[tokio::main]
66
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7-
let _configs_dir = StateDirectory::home()?.configs()?;
7+
let _configs = StateDirectory::home()?.configs()?;
88
// TODO
99
Ok(())
1010
}

lib/asimov-directory/examples/list_modules.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use asimov_directory::{ModuleNameIterator, fs::StateDirectory};
44

55
#[tokio::main]
66
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7-
let modules_dir = StateDirectory::home()?.modules()?;
8-
let mut module_names = modules_dir.iter_installed().await?;
7+
let modules = StateDirectory::home()?.modules()?;
8+
let mut module_names = modules.iter_installed().await?;
99
while let Some(module_name) = module_names.next().await {
1010
println!("{}", module_name);
1111
}

lib/asimov-directory/examples/list_programs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use asimov_directory::fs::StateDirectory;
44

55
#[tokio::main]
66
async fn main() -> Result<(), Box<dyn std::error::Error>> {
7-
let _programs_dir = StateDirectory::home()?.programs()?;
7+
let _programs = StateDirectory::home()?.programs()?;
88
// TODO
99
Ok(())
1010
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// cargo run --package asimov-directory --example show_config
2+
3+
use asimov_directory::fs::StateDirectory;
4+
5+
fn main() -> Result<(), Box<dyn std::error::Error>> {
6+
let configs = StateDirectory::home()?.configs()?;
7+
let profile = configs.default_profile()?;
8+
println!("{:?}", profile); // TODO
9+
Ok(())
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// This is free and unencumbered software released into the public domain.
2+
3+
use alloc::string::String;
4+
use asimov_core::Named;
5+
6+
/// A configuration profile.
7+
pub trait ConfigProfile: Named {
8+
/// Returns the system prompt, if any, for the configuration profile.
9+
fn prompt(&self) -> Option<String> {
10+
None
11+
}
12+
}

lib/asimov-directory/src/fs/config_directory.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// This is free and unencumbered software released into the public domain.
22

3-
use super::StateDirectory;
3+
use super::{ConfigProfile, StateDirectory};
44
use alloc::format;
55
use camino::Utf8PathBuf;
66
use derive_more::Display;
@@ -39,6 +39,12 @@ impl ConfigDirectory {
3939
Ok(ConfigDirectory { path })
4040
}
4141

42+
/// Returns the default configuration profile (e.g., at
43+
/// `$HOME/.asimov/configs/default/`).
44+
pub fn default_profile(&self) -> Result<ConfigProfile> {
45+
ConfigProfile::open(self.path.join("default"))
46+
}
47+
4248
pub fn as_str(&self) -> &str {
4349
self.path.as_str()
4450
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// This is free and unencumbered software released into the public domain.
2+
3+
use alloc::{borrow::Cow, format, string::String};
4+
use asimov_core::Named;
5+
use camino::Utf8PathBuf;
6+
use derive_more::Display;
7+
use std::{
8+
io::{Error, ErrorKind, Result},
9+
path::Path,
10+
};
11+
12+
/// A configuration profile stored on a file system (e.g., `$HOME/.asimov/configs/default/`).
13+
#[derive(Debug, Display)]
14+
#[display("ConfigProfile({:?})", path)]
15+
pub struct ConfigProfile {
16+
path: Utf8PathBuf,
17+
}
18+
19+
impl ConfigProfile {
20+
/// Opens a configuration profile from a file system path.
21+
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
22+
let path = path.as_ref();
23+
if !path.exists() {
24+
std::fs::create_dir_all(path)?;
25+
}
26+
let path = Utf8PathBuf::from_path_buf(path.to_path_buf()).map_err(|e| {
27+
Error::new(
28+
ErrorKind::InvalidFilename,
29+
format!("failed to open non-UTF-8 path: {}", e.display()),
30+
)
31+
})?;
32+
Ok(ConfigProfile { path })
33+
}
34+
35+
/// The name of the configuration profile (e.g., "default").
36+
fn name(&self) -> Cow<'_, str> {
37+
if let Some(name) = self.path.file_name() {
38+
return Cow::Borrowed(name);
39+
}
40+
self.path
41+
.canonicalize_utf8()
42+
.ok()
43+
.and_then(|p| p.file_name().map(String::from))
44+
.map_or(Cow::default(), Cow::Owned)
45+
}
46+
47+
pub fn as_str(&self) -> &str {
48+
self.path.as_str()
49+
}
50+
}
51+
52+
impl AsRef<str> for ConfigProfile {
53+
fn as_ref(&self) -> &str {
54+
self.path.as_str()
55+
}
56+
}
57+
58+
impl AsRef<Path> for ConfigProfile {
59+
fn as_ref(&self) -> &Path {
60+
self.path.as_std_path()
61+
}
62+
}
63+
64+
#[cfg(feature = "camino")]
65+
impl AsRef<Utf8Path> for ConfigProfile {
66+
fn as_ref(&self) -> &Utf8Path {
67+
self.path.as_path()
68+
}
69+
}
70+
71+
impl Named for ConfigProfile {
72+
fn name(&self) -> Cow<'_, str> {
73+
self.name()
74+
}
75+
}
76+
77+
impl crate::ConfigProfile for ConfigProfile {
78+
fn prompt(&self) -> Option<String> {
79+
None // TODO
80+
}
81+
}

lib/asimov-directory/src/fs/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
mod config_directory;
44
pub use config_directory::*;
55

6+
mod config_profile;
7+
pub use config_profile::*;
8+
69
mod module_directory;
710
pub use module_directory::*;
811

lib/asimov-directory/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ pub mod fs;
1414
mod config_directory;
1515
pub use config_directory::*;
1616

17+
mod config_profile;
18+
pub use config_profile::*;
19+
1720
mod module_directory;
1821
pub use module_directory::*;
1922

0 commit comments

Comments
 (0)