Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 4.4 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! The short forms of repositories and accounts that lists hold: explore
5//! and search, topics, profiles, stars, watching, followers and forks. A
6//! list holds at most one page, and `next`, when there is more, is the
7//! opaque cursor that asks for the page after it (`?after=`).
8
9use serde::{Deserialize, Serialize};
10
11/// A repository, named by its owner and its name.
12#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
13pub struct RepoRef {
14 /// The owner's login, lowercase.
15 pub owner: String,
16 /// The name.
17 pub name: String,
18}
19
20/// A repository in a list.
21#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
22pub struct RepoCard {
23 /// The owner's login, lowercase.
24 pub owner: String,
25 /// The name.
26 pub name: String,
27 /// Whether only those given access can read it.
28 pub private: bool,
29 /// A description.
30 #[serde(default)]
31 pub description: String,
32 /// Its topics.
33 #[serde(default)]
34 pub topics: Vec<String>,
35 /// The accounts that starred it.
36 #[serde(default)]
37 pub stars: u64,
38 /// Its forks the caller may see.
39 #[serde(default)]
40 pub forks: u64,
41 /// When it was made.
42 pub created: String,
43 /// When a landing last moved one of its refs, if one ever did.
44 #[serde(default)]
45 pub pushed: Option<String>,
46 /// The branch the web shows.
47 pub default_branch: String,
48 /// The repository it was forked from, when the caller may read that
49 /// one.
50 #[serde(default)]
51 pub fork_of: Option<RepoRef>,
52 /// The public GitHub repository it mirrors to, if any.
53 #[serde(default)]
54 pub mirror_url: Option<String>,
55 /// Text and data mining of its content: `allowed` or `reserved`.
56 #[serde(default, skip_serializing_if = "String::is_empty")]
57 pub tdm: String,
58 /// In a list of stars: when the account starred it.
59 #[serde(default, skip_serializing_if = "Option::is_none")]
60 pub starred_at: Option<String>,
61 /// In a list of watched repositories: `all`, `participating` or
62 /// `ignore`.
63 #[serde(default, skip_serializing_if = "Option::is_none")]
64 pub level: Option<String>,
65}
66
67/// An account in a list.
68#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
69pub struct UserCard {
70 /// Its login, lowercase.
71 pub login: String,
72 /// Its display name, if any.
73 #[serde(default)]
74 pub name: Option<String>,
75 /// Since when it is in the list: when it starred, followed or was
76 /// followed.
77 pub since: String,
78}
79
80/// A page of repositories.
81#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
82pub struct RepoCards {
83 /// The repositories.
84 pub repos: Vec<RepoCard>,
85 /// The cursor of the next page, or `None` on the last one.
86 #[serde(default)]
87 pub next: Option<String>,
88}
89
90/// A page of accounts.
91#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
92pub struct UserCards {
93 /// The accounts.
94 pub users: Vec<UserCard>,
95 /// The cursor of the next page, or `None` on the last one.
96 #[serde(default)]
97 pub next: Option<String>,
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn cards_on_the_wire() {
106 let card = RepoCard {
107 owner: "ada".into(),
108 name: "demo".into(),
109 created: "2026-01-01T00:00:00Z".into(),
110 default_branch: "main".into(),
111 tdm: "allowed".into(),
112 fork_of: Some(RepoRef {
113 owner: "bob".into(),
114 name: "demo".into(),
115 }),
116 ..RepoCard::default()
117 };
118 let text = serde_json::to_string(&card).unwrap();
119 assert!(
120 !text.contains("starred_at") && !text.contains("level"),
121 "{text}"
122 );
123 assert_eq!(serde_json::from_str::<RepoCard>(&text).unwrap(), card);
124 // A short card, from a hub that says less, still reads.
125 let short: RepoCard = serde_json::from_str(
126 r#"{"owner":"ada","name":"x","private":false,"created":"t","default_branch":"main"}"#,
127 )
128 .unwrap();
129 assert_eq!((short.stars, short.tdm.as_str()), (0, ""));
130 let page: UserCards =
131 serde_json::from_str(r#"{"users":[{"login":"eve","since":"t"}]}"#).unwrap();
132 assert_eq!((page.users[0].name.as_deref(), page.next), (None, None));
133 }
134}