intermodal/bin/gen/src/metadata.rs
Casey Rodarmor 04338e3501
Merge documentation and changelog generation
Merge documentation generation into a single binary, `bin/gen`. This
includes: The changelog, man pages, the readme, and the book.

type: reform
2020-04-17 21:31:54 -07:00

58 lines
1.1 KiB
Rust

use crate::common::*;
#[derive(Deserialize, Serialize, Clone)]
pub(crate) struct Metadata {
#[serde(rename = "type")]
pub(crate) kind: Kind,
#[serde(default)]
pub(crate) pr: Option<Url>,
#[serde(default)]
pub(crate) fixes: Vec<Url>,
}
impl Metadata {
#[throws]
pub(crate) fn from_commit(commit: &Commit) -> Metadata {
const BLANK: &str = "\n\n";
let message = String::from_utf8_lossy(commit.message_bytes());
let blank = message
.rfind(BLANK)
.ok_or_else(|| anyhow!("Commit message missing metadata: {}", message))?;
let yaml = &message[blank + BLANK.len()..];
let metadata = serde_yaml::from_str(yaml)?;
metadata
}
}
impl Default for Metadata {
fn default() -> Metadata {
Metadata {
kind: Kind::Changed,
pr: None,
fixes: Vec::new(),
}
}
}
impl Display for Metadata {
#[throws(fmt::Error)]
fn fmt(&self, f: &mut Formatter) {
writeln!(f)?;
writeln!(
f,
"{}",
serde_yaml::to_string(&self)
.unwrap()
.split("---")
.last()
.unwrap()
.trim()
)?;
}
}