entrenar/cli/commands/research/
cite.rs1use crate::cli::logging::log;
4use crate::cli::LogLevel;
5use crate::config::{CitationFormat, CiteArgs};
6use crate::research::{CitationMetadata, ResearchArtifact};
7
8pub fn run_research_cite(args: CiteArgs, level: LogLevel) -> Result<(), String> {
9 log(level, LogLevel::Normal, &format!("Generating citation from: {}", args.artifact.display()));
10
11 let yaml = std::fs::read_to_string(&args.artifact)
13 .map_err(|e| format!("Failed to read artifact: {e}"))?;
14
15 let artifact: ResearchArtifact =
16 serde_yaml::from_str(&yaml).map_err(|e| format!("Failed to parse artifact: {e}"))?;
17
18 let mut citation = CitationMetadata::new(artifact, args.year);
20
21 if let Some(journal) = &args.journal {
22 citation = citation.with_journal(journal);
23 }
24 if let Some(volume) = &args.volume {
25 citation = citation.with_volume(volume);
26 }
27 if let Some(pages) = &args.pages {
28 citation = citation.with_pages(pages);
29 }
30 if let Some(url) = &args.url {
31 citation = citation.with_url(url);
32 }
33
34 let output = match args.format {
36 CitationFormat::Bibtex => citation.to_bibtex(),
37 CitationFormat::Cff => citation.to_cff(),
38 CitationFormat::Json => {
39 serde_json::to_string_pretty(&citation).map_err(|e| format!("JSON error: {e}"))?
40 }
41 };
42
43 if let Some(output_path) = &args.output {
45 std::fs::write(output_path, &output).map_err(|e| format!("Failed to write file: {e}"))?;
46 log(level, LogLevel::Normal, &format!("Citation saved to: {}", output_path.display()));
47 } else {
48 println!("{output}");
49 }
50
51 Ok(())
52}