hugging_face_client/
repo.rs

1use std::fmt::{Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct Repo {
7  #[serde(rename = "_id")]
8  pub id: String,
9
10  #[serde(rename = "id")]
11  pub name: String,
12
13  pub author: String,
14  pub private: bool,
15  pub likes: usize,
16  pub tags: Option<Vec<String>>,
17  pub sha: Option<String>,
18
19  #[serde(rename = "lastModified")]
20  pub last_modified: Option<String>,
21
22  pub pipeline_tag: Option<String>,
23
24  pub library_name: Option<String>,
25}
26
27#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
28pub enum RepoType {
29  #[serde(rename = "dataset")]
30  Dataset,
31
32  #[serde(rename = "space")]
33  Space,
34
35  #[serde(rename = "model")]
36  Model,
37
38  #[serde(rename = "paper")]
39  Paper,
40}
41
42impl Default for RepoType {
43  fn default() -> Self {
44    RepoType::Model
45  }
46}
47
48impl Display for RepoType {
49  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
50    match self {
51      RepoType::Dataset => f.write_str("dataset"),
52      RepoType::Space => f.write_str("space"),
53      RepoType::Model => f.write_str("model"),
54      RepoType::Paper => f.write_str("paper"),
55    }
56  }
57}
58
59#[cfg(test)]
60mod test {
61  use std::assert_matches::assert_matches;
62
63  use crate::repo::RepoType;
64
65  #[test]
66  fn test_serde_repo_type() {
67    let repo_type = RepoType::Dataset;
68    let json = serde_json::to_string(&repo_type);
69    assert_matches!(json, Ok(v) if v == "\"dataset\"");
70
71    let json = "\"dataset\"";
72    let sdk = serde_json::from_str::<RepoType>(json);
73    assert_matches!(sdk, Ok(RepoType::Dataset));
74
75    let repo_type = RepoType::Space;
76    let json = serde_json::to_string(&repo_type);
77    assert_matches!(json, Ok(v) if v == "\"space\"");
78
79    let json = "\"space\"";
80    let sdk = serde_json::from_str::<RepoType>(json);
81    assert_matches!(sdk, Ok(RepoType::Space));
82
83    let repo_type = RepoType::Model;
84    let json = serde_json::to_string(&repo_type);
85    assert_matches!(json, Ok(v) if v == "\"model\"");
86
87    let json = "\"model\"";
88    let sdk = serde_json::from_str::<RepoType>(json);
89    assert_matches!(sdk, Ok(RepoType::Model));
90  }
91}