use crate::{PatternIOProcessor, Prefer};
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use sysexits::Result;
#[derive(Subcommand)]
pub enum Action {
Cffreference(crate::Cffreference),
CommentChanges(crate::CommentChanges),
Rs2md {
#[arg(long = "inner")]
extract_inner: bool,
#[arg(long = "outer")]
extract_outer: bool,
#[arg(long = "input", short = 'i')]
input_file: Vec<PathBuf>,
#[arg(long = "output", short = 'o')]
output_file: Option<PathBuf>,
},
Uncrlf {
#[arg(long = "edit", short = 'e')]
file_to_edit: Option<PathBuf>,
#[arg(long = "input", short = 'i')]
input_file: Option<PathBuf>,
#[arg(long = "output", short = 'o')]
output_file: Option<PathBuf>,
},
}
impl Action {
fn rs2md(s: &str, extract_inner: bool, extract_outer: bool) -> String {
s.lines()
.map(str::trim_start)
.filter(|l| {
(extract_inner && l.starts_with("///"))
|| (extract_outer && l.starts_with("//!"))
})
.map(|l| {
if l.len() > 3 {
l.split_at(4).1.trim_end().to_string() + "\n"
} else {
"\n".to_string()
}
})
.collect::<String>()
}
pub fn run(&self) -> Result<()> {
match self {
Self::Cffreference(c) => c.main(),
Self::CommentChanges(c) => c.main(),
Self::Rs2md {
extract_inner,
extract_outer,
input_file,
output_file,
} => (|s: String| -> String {
Self::rs2md(&s, *extract_inner, *extract_outer)
})
.io(input_file, output_file),
Self::Uncrlf {
file_to_edit,
input_file,
output_file,
} => |mut s: String| -> String {
s.retain(|c| c != '\r');
s
}
.io(
input_file.prefer(file_to_edit.clone()),
output_file.prefer(file_to_edit.clone()),
),
}
}
}
#[derive(Parser)]
#[clap(about, version)]
pub struct Clap {
#[clap(subcommand)]
action: Action,
}
crate::getters!(@ref Clap { action: Action });