1use std::fmt::Display;
2
3use miette::Diagnostic;
4use thiserror::Error;
5
6use crate::{fs, git::remote::Url, rockspec};
7
8pub mod remote;
9pub mod utils;
10
11impl rockspec::display::AsLuaValue for Url {
12 fn display_lua_value(&self) -> rockspec::display::LuaValue {
13 rockspec::display::LuaValue::String(self.to_string())
14 }
15}
16
17#[derive(Debug, Error, Diagnostic)]
18pub enum Error {
19 #[error(transparent)]
20 #[diagnostic(transparent)]
21 Fs(#[from] fs::Error),
22 #[error("error initializing temporary bare git repository to fetch metadata")]
23 BareRepoInit(#[source] git2::Error),
24 #[error("error initializing remote repository '{0}' to fetch metadata")]
25 RemoteInit(String, #[source] git2::Error),
26 #[error("error fetching from remote repository '{0}'")]
27 RemoteFetch(String, #[source] git2::Error),
28 #[error("error listing remote refs for '{0}'")]
29 RemoteList(String, #[source] git2::Error),
30 #[error("could not determine latest tag or commit sha for '{0}'")]
31 NoTagOrCommitSha(String),
32}
33#[derive(Debug, PartialEq, Eq, Hash, Clone)]
35pub enum Ref {
36 Tag(String),
37 Branch(String),
38}
39
40#[derive(Debug, PartialEq, Eq, Hash, Clone)]
42pub struct Source {
43 pub url: Url,
44 pub git_ref: Option<Ref>,
45}
46
47impl rockspec::display::AsLuaKV for Source {
48 fn display_lua(&self) -> rockspec::display::LuaKV {
49 let mut fields = vec![rockspec::display::LuaKV {
50 key: "url".to_string(),
51 value: rockspec::display::LuaValue::String(self.url.to_string()),
52 }];
53
54 if let Some(git_ref) = &self.git_ref {
55 match git_ref {
56 Ref::Tag(tag) => fields.push(rockspec::display::LuaKV {
57 key: "tag".to_string(),
58 value: rockspec::display::LuaValue::String(tag.clone()),
59 }),
60 Ref::Branch(branch) => fields.push(rockspec::display::LuaKV {
61 key: "branch".to_string(),
62 value: rockspec::display::LuaValue::String(branch.clone()),
63 }),
64 }
65 }
66
67 rockspec::display::LuaKV {
68 key: "source".to_string(),
69 value: rockspec::display::LuaValue::Table(fields),
70 }
71 }
72}
73
74impl Display for Source {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 match &self.git_ref {
77 Some(Ref::Tag(tag)) => format!("{}@{}", self.url, tag).fmt(f),
78 Some(Ref::Branch(branch)) => format!("{}@{}", self.url, branch).fmt(f),
79 None => self.url.fmt(f),
80 }
81 }
82}