#![doc(html_root_url = "https://docs.rs/smart-config-commands/0.4.0-pre.4")] #![warn(missing_docs)]
use std::{
collections::HashSet,
io,
io::{StderrLock, StdoutLock},
};
use anstream::{AutoStream, stream::RawStream};
use anstyle::{AnsiColor, Color, Style};
use smart_config::{
ConfigRef,
metadata::{AliasOptions, ParamMetadata},
};
mod debug;
mod help;
mod markdown;
mod schema_ref;
mod utils;
pub use self::markdown::{EnvVarOptions, MarkdownOptions};
const CONFIG_PATH: Style = Style::new().fg_color(Some(Color::Ansi(AnsiColor::Yellow)));
#[derive(Debug)]
pub struct Printer<W: RawStream> {
writer: AutoStream<W>,
}
impl Printer<StdoutLock<'static>> {
pub fn stdout() -> Self {
Self {
writer: AutoStream::auto(io::stdout()).lock(),
}
}
}
impl Printer<StderrLock<'static>> {
pub fn stderr() -> Self {
Self {
writer: AutoStream::auto(io::stderr()).lock(),
}
}
}
impl<W: RawStream> Printer<W> {
pub fn custom(writer: AutoStream<W>) -> Self {
Self { writer }
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ParamRef<'a> {
pub config: ConfigRef<'a>,
pub param: &'static ParamMetadata,
}
impl<'a> ParamRef<'a> {
pub(crate) fn for_tag(config: ConfigRef<'a>) -> Self {
Self {
config,
param: config.metadata().tag.unwrap().param,
}
}
pub fn canonical_path(&self) -> String {
if self.config.prefix().is_empty() {
self.param.name.to_owned()
} else {
format!(
"{prefix}.{name}",
prefix = self.config.prefix(),
name = self.param.name
)
}
}
pub fn all_paths(&self) -> impl Iterator<Item = (String, AliasOptions)> + '_ {
let mut known_paths = HashSet::new();
self.config
.all_paths_for_param(self.param)
.filter(move |(name, _)| known_paths.insert(name.clone()))
}
}
#[cfg(doctest)]
doc_comment::doctest!("../README.md");