pub mod cite;
pub mod download;
pub mod get;
pub mod read;
pub mod search;
pub mod sources;
use std::path::Path;
use crate::cli::OutputFormat;
use crate::output;
use crate::sources::Paper;
#[derive(Debug)]
pub enum CommandError {
Failed(String),
NotFound(String),
AlreadyExists(String),
}
impl CommandError {
pub fn exit_code(&self) -> i32 {
match self {
CommandError::Failed(_) => 1,
CommandError::NotFound(_) => 4,
CommandError::AlreadyExists(_) => 0,
}
}
pub fn message(&self) -> &str {
match self {
CommandError::Failed(m)
| CommandError::NotFound(m)
| CommandError::AlreadyExists(m) => m,
}
}
}
pub type CommandResult = Result<(), CommandError>;
pub fn failed(message: impl Into<String>) -> CommandError {
CommandError::Failed(message.into())
}
pub fn render(papers: &[Paper], format: OutputFormat) -> String {
match format {
OutputFormat::Json => output::to_json(papers),
OutputFormat::Jsonl => output::to_jsonl(papers),
OutputFormat::Csv => output::to_csv(papers),
OutputFormat::Bibtex => output::to_bibtex(papers),
OutputFormat::Table => output::to_table(papers),
}
}
pub fn emit(text: &str, output_path: Option<&Path>) -> CommandResult {
match output_path {
Some(path) => std::fs::write(path, text)
.map_err(|e| failed(format!("Failed to write {}: {}", path.display(), e))),
None => {
print!("{}", text);
Ok(())
}
}
}