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