1use std::fmt::Display;
2
3use crate::{
4 git::url::RemoteGitUrl,
5 lua_rockspec::{DisplayAsLuaKV, DisplayAsLuaValue, DisplayLuaKV, DisplayLuaValue},
6};
7
8pub mod shorthand;
9pub mod url;
10pub mod utils;
11
12impl DisplayAsLuaValue for RemoteGitUrl {
13 fn display_lua_value(&self) -> DisplayLuaValue {
14 DisplayLuaValue::String(self.to_string())
15 }
16}
17
18#[derive(Debug, PartialEq, Eq, Hash, Clone)]
20pub enum GitRef {
21 Tag(String),
22 Branch(String),
23}
24
25#[derive(Debug, PartialEq, Eq, Hash, Clone)]
27pub struct GitSource {
28 pub url: RemoteGitUrl,
29 pub git_ref: Option<GitRef>,
30}
31
32impl DisplayAsLuaKV for GitSource {
33 fn display_lua(&self) -> DisplayLuaKV {
34 let mut fields = vec![DisplayLuaKV {
35 key: "url".to_string(),
36 value: DisplayLuaValue::String(self.url.to_string()),
37 }];
38
39 if let Some(git_ref) = &self.git_ref {
40 match git_ref {
41 GitRef::Tag(tag) => fields.push(DisplayLuaKV {
42 key: "tag".to_string(),
43 value: DisplayLuaValue::String(tag.clone()),
44 }),
45 GitRef::Branch(branch) => fields.push(DisplayLuaKV {
46 key: "branch".to_string(),
47 value: DisplayLuaValue::String(branch.clone()),
48 }),
49 }
50 }
51
52 DisplayLuaKV {
53 key: "source".to_string(),
54 value: DisplayLuaValue::Table(fields),
55 }
56 }
57}
58
59impl Display for GitSource {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 match &self.git_ref {
62 Some(GitRef::Tag(tag)) => format!("{}@{}", self.url, tag).fmt(f),
63 Some(GitRef::Branch(branch)) => format!("{}@{}", self.url, branch).fmt(f),
64 None => self.url.fmt(f),
65 }
66 }
67}