abbaye/version_extractors/
mod.rs1use chrono::{DateTime, Utc};
2use miette::{Result, bail};
3use serde::{Deserialize, Serialize};
4
5pub mod cargo;
6pub mod git;
7
8use cargo::{CargoVersion, CargoVersionConfig};
9use git::{GitVersion, GitVersionConfig};
10
11#[derive(Debug, Clone)]
17pub struct VersionInfo {
18 pub version: String,
20 pub date: Option<DateTime<Utc>>,
22}
23
24#[allow(async_fn_in_trait)]
25pub trait VersionExtractor {
26 type ConfigType: Default + for<'de> Deserialize<'de> + Clone;
27
28 async fn get_last_version(&self, config: Self::ConfigType) -> Result<VersionInfo>;
29
30 async fn get_all_versions(&self, _config: Self::ConfigType) -> Result<Vec<VersionInfo>> {
31 bail!("get_all_versions is not supported by this version extractor")
32 }
33}
34
35#[derive(Debug, Clone, Deserialize, Serialize)]
36#[serde(tag = "type", rename_all = "snake_case")]
37pub enum AnyVersionExtractor {
38 Cargo(CargoVersionConfig),
47
48 Git(GitVersionConfig),
60}
61
62impl AnyVersionExtractor {
63 pub async fn extract(&self) -> Result<VersionInfo> {
64 match self {
65 Self::Cargo(config) => CargoVersion.get_last_version(config.clone()).await,
66 Self::Git(config) => GitVersion.get_last_version(config.clone()).await,
67 }
68 }
69
70 pub async fn extract_all(&self) -> Result<Vec<VersionInfo>> {
71 match self {
72 Self::Cargo(config) => CargoVersion.get_all_versions(config.clone()).await,
73 Self::Git(config) => GitVersion.get_all_versions(config.clone()).await,
74 }
75 }
76
77 pub fn tag_name(&self, version: &str) -> String {
83 match self {
84 Self::Git(config) => match &config.tag_prefix {
85 Some(prefix) => format!("{prefix}{version}"),
86 None => version.to_owned(),
87 },
88 Self::Cargo(_) => version.to_owned(),
89 }
90 }
91}