CLI Package - Rust Implementation

Crate: giztoy-cli

📚 Rust Documentation

Types

Config

#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Config {
    #[serde(skip)]
    pub app_name: String,
    
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub current_context: String,
    
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub contexts: HashMap<String, Context>,
    
    #[serde(skip)]
    config_path: PathBuf,
}
}

Methods:

MethodSignatureDescription
default_config_dirfn default_config_dir(app_name: &str) -> Option<PathBuf>Get default config dir
default_config_pathfn default_config_path(app_name: &str) -> Option<PathBuf>Get default config path
pathfn path(&self) -> &PathBufGet config file path
dirfn dir(&self) -> Option<&Path>Get config directory
savefn save(&self) -> Result<()>Save to disk
add_contextfn add_context(&mut self, name: &str, ctx: Context) -> Result<()>Add context
delete_contextfn delete_context(&mut self, name: &str) -> Result<()>Delete context
use_contextfn use_context(&mut self, name: &str) -> Result<()>Set current context
get_contextfn get_context(&self, name: &str) -> Option<&Context>Get specific context
get_current_contextfn get_current_context(&self) -> Option<&Context>Get current context
resolve_contextfn resolve_context(&self, name: Option<&str>) -> Option<&Context>Resolve by name or current
list_contextsfn list_contexts(&self) -> Vec<&str>List all context names

Free Functions:

FunctionSignatureDescription
load_configfn load_config(app_name: &str, custom_path: Option<&str>) -> Result<Config>Load config
save_configfn save_config(app_name: &str, config: &Config, custom_path: Option<&str>) -> Result<()>Save config
mask_api_keyfn mask_api_key(key: &str) -> StringMask API key

Context

#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Context {
    pub name: String,
    pub client: Option<ClientCredentials>,
    pub console: Option<ConsoleCredentials>,
    pub api_key: String,
    pub base_url: String,
    pub timeout: i32,
    pub max_retries: i32,
    pub default_voice: String,
    pub extra: HashMap<String, String>,
}
}

Output

#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
    #[default]
    Yaml,
    Json,
}

pub struct Output {
    pub format: OutputFormat,
    pub file: Option<String>,
}
}

Methods:

MethodSignatureDescription
newfn new(format: OutputFormat, file: Option<String>) -> SelfCreate output config
writefn write<T: Serialize>(&self, value: &T) -> Result<()>Write formatted output
write_binaryfn write_binary(&self, data: &[u8], path: &str) -> Result<()>Write binary data

Free Functions:

FunctionSignatureDescription
print_verbosefn print_verbose(enabled: bool, message: &str)Print verbose message
guess_extensionfn guess_extension(format: &str) -> &strGuess file extension

Paths

#![allow(unused)]
fn main() {
#[derive(Debug, Clone)]
pub struct Paths {
    pub app_name: String,
    pub home_dir: PathBuf,
}
}

Methods:

MethodSignatureDescription
newfn new(app_name: impl Into<String>) -> io::Result<Self>Create paths instance
base_dirfn base_dir(&self) -> PathBuf~/.giztoy
app_dirfn app_dir(&self) -> PathBuf~/.giztoy/
config_filefn config_file(&self) -> PathBuf~/.giztoy//config.yaml
cache_dirfn cache_dir(&self) -> PathBuf~/.giztoy//cache
log_dirfn log_dir(&self) -> PathBuf~/.giztoy//logs
data_dirfn data_dir(&self) -> PathBuf~/.giztoy//data
ensure_app_dirfn ensure_app_dir(&self) -> io::Result<()>Create app dir
ensure_cache_dirfn ensure_cache_dir(&self) -> io::Result<()>Create cache dir
cache_pathfn cache_path(&self, name: &str) -> PathBufPath in cache
log_pathfn log_path(&self, name: &str) -> PathBufPath in logs
data_pathfn data_path(&self, name: &str) -> PathBufPath in data

Usage

Load Configuration

#![allow(unused)]
fn main() {
use giztoy_cli::{load_config, mask_api_key};

let cfg = load_config("minimax", None)?;

if let Some(ctx) = cfg.get_current_context() {
    println!("API Key: {}", mask_api_key(&ctx.api_key));
}
}

Output Results

#![allow(unused)]
fn main() {
use giztoy_cli::{Output, OutputFormat};
use serde::Serialize;

#[derive(Serialize)]
struct Result {
    status: String,
    message: String,
}

let result = Result {
    status: "ok".to_string(),
    message: "done".to_string(),
};

// Output as JSON to stdout
let output = Output::new(OutputFormat::Json, None);
output.write(&result)?;

// Output to file
let output = Output::new(OutputFormat::Yaml, Some("output.yaml".to_string()));
output.write(&result)?;
}

Path Management

#![allow(unused)]
fn main() {
use giztoy_cli::Paths;

let paths = Paths::new("minimax")?;

// Ensure directories exist
paths.ensure_cache_dir()?;
paths.ensure_log_dir()?;

// Get paths
let cache_path = paths.cache_path("response.json");
let log_path = paths.log_path("2024-01-15.log");
}

Dependencies

  • serde + serde_yaml + serde_json - Serialization
  • dirs - Home directory detection
  • anyhow - Error handling

Differences from Go

AspectGoRust
Error handlingerror returnanyhow::Result
Config loadingLoadConfig(app)load_config(app, None)
Output formatsyaml, json, table, rawyaml, json only
Print helpersPrintSuccess, PrintError, etc.print_verbose only
Path returnsstringPathBuf