ya_runtime_sdk/
env.rs

1use crate::cli::{parse_cli, CommandCli};
2use std::path::PathBuf;
3
4/// Runtime environment configuration
5pub trait Env<C: CommandCli> {
6    /// Runtime name. Return `None` for project name
7    fn runtime_name(&self) -> Option<String> {
8        None
9    }
10
11    /// Directory to store the configuration, caches and state at
12    fn data_directory(&self, runtime_name: &str) -> anyhow::Result<PathBuf> {
13        Ok(directories::ProjectDirs::from("", "", runtime_name)
14            .map(|dirs| dirs.data_dir().into())
15            .unwrap_or_else(|| PathBuf::from(runtime_name)))
16    }
17
18    /// Command line arguments
19    fn args(&self) -> Box<dyn Iterator<Item = String>> {
20        Box::new(std::env::args())
21    }
22
23    /// Parse command line arguments
24    fn cli(&mut self, project_name: &str, project_version: &str) -> anyhow::Result<C> {
25        let name = self
26            .runtime_name()
27            .unwrap_or_else(|| project_name.to_string());
28        parse_cli(&name, project_version, self.args())
29    }
30}
31
32/// Default runtime environment provider.
33///
34/// - data directory is located in the home user folder
35/// - provides unaltered command line arguments
36#[derive(Clone, Copy, Debug)]
37pub struct DefaultEnv<C> {
38    phantom: std::marker::PhantomData<C>,
39}
40
41impl<C> Default for DefaultEnv<C> {
42    fn default() -> Self {
43        Self {
44            phantom: std::marker::PhantomData,
45        }
46    }
47}
48
49impl<C: CommandCli> Env<C> for DefaultEnv<C> {}