radicle_source/
tag.rs

1// This file is part of radicle-surf
2// <https://github.com/radicle-dev/radicle-surf>
3//
4// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License version 3 or
8// later as published by the Free Software Foundation.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use std::fmt;
19
20use serde::Serialize;
21
22use radicle_surf::{git::RefScope, vcs::git::Browser};
23
24use crate::error::Error;
25
26/// Tag name representation.
27///
28/// We still need full tag support.
29#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize)]
30pub struct Tag(pub(crate) String);
31
32impl From<String> for Tag {
33    fn from(name: String) -> Self {
34        Self(name)
35    }
36}
37
38impl fmt::Display for Tag {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        write!(f, "{}", self.0)
41    }
42}
43
44/// Retrieves the list of [`Tag`] for the given project `id`.
45///
46/// # Errors
47///
48/// Will return [`Error`] if the project doesn't exist or the surf interaction
49/// fails.
50pub fn tags(browser: &Browser<'_>) -> Result<Vec<Tag>, Error> {
51    let tag_names = browser.list_tags(RefScope::Local)?;
52    let mut tags: Vec<Tag> = tag_names
53        .into_iter()
54        .map(|tag_name| Tag(tag_name.name().to_string()))
55        .collect();
56
57    tags.sort();
58
59    Ok(tags)
60}