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
39impl Default for RepoType {
40  fn default() -> Self {
41    RepoType::Model
42  }
43}
44
45impl Display for RepoType {
46  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47    match self {
48      RepoType::Dataset => f.write_str("dataset"),
49      RepoType::Space => f.write_str("space"),
50      RepoType::Model => f.write_str("model"),
51    }
52  }
53}
54
55#[cfg(test)]
56mod test {
57  use std::assert_matches::assert_matches;
58
59  use crate::repo::RepoType;
60
61  #[test]
62  fn test_serde_repo_type() {
63    let repo_type = RepoType::Dataset;
64    let json = serde_json::to_string(&repo_type);
65    assert_matches!(json, Ok(v) if v == "\"dataset\"");
66
67    let json = "\"dataset\"";
68    let sdk = serde_json::from_str::<RepoType>(json);
69    assert_matches!(sdk, Ok(RepoType::Dataset));
70
71    let repo_type = RepoType::Space;
72    let json = serde_json::to_string(&repo_type);
73    assert_matches!(json, Ok(v) if v == "\"space\"");
74
75    let json = "\"space\"";
76    let sdk = serde_json::from_str::<RepoType>(json);
77    assert_matches!(sdk, Ok(RepoType::Space));
78
79    let repo_type = RepoType::Model;
80    let json = serde_json::to_string(&repo_type);
81    assert_matches!(json, Ok(v) if v == "\"model\"");
82
83    let json = "\"model\"";
84    let sdk = serde_json::from_str::<RepoType>(json);
85    assert_matches!(sdk, Ok(RepoType::Model));
86  }
87}