Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 2.6 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Stars, watching and following (section 17): how an account watches a
5//! repository. The lists of this section, of stars, stargazers, watched
6//! repositories and follows, are [`RepoCards`](crate::cards::RepoCards)
7//! and [`UserCards`](crate::cards::UserCards).
8
9use serde::{Deserialize, Serialize};
10
11/// Watching everything that happens in a repository: what its owner
12/// watches unless it chooses otherwise.
13pub const ALL: &str = "all";
14
15/// Watching only the threads one takes part in or is mentioned in: what
16/// everyone but the owner watches unless they choose otherwise.
17pub const PARTICIPATING: &str = "participating";
18
19/// Watching nothing of a repository, not even the threads one takes part
20/// in.
21pub const IGNORE: &str = "ignore";
22
23/// The levels an account may watch a repository at.
24pub const LEVELS: [&str; 3] = [ALL, PARTICIPATING, IGNORE];
25
26/// Whether `text` is one of the [`LEVELS`].
27pub fn is_level(text: &str) -> bool {
28 LEVELS.contains(&text)
29}
30
31/// `GET …/subscription`, the answer of `PUT …/subscription`, and its body
32/// (where `since` is left out): how the caller watches a repository.
33#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
34pub struct Subscription {
35 /// `all`, `participating` or `ignore`.
36 pub level: String,
37 /// When the caller chose this level; absent while it watches as an
38 /// account does by default (see [`ALL`] and [`PARTICIPATING`]).
39 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub since: Option<String>,
41}
42
43#[cfg(test)]
44mod tests {
45 use super::*;
46
47 #[test]
48 fn levels_and_subscriptions_on_the_wire() {
49 for level in LEVELS {
50 assert!(is_level(level));
51 }
52 for other in ["", "All", "none", "watching", "all "] {
53 assert!(!is_level(other), "{other:?}");
54 }
55 let chosen = Subscription {
56 level: IGNORE.into(),
57 since: Some("2026-10-18T12:00:00Z".into()),
58 };
59 let text = serde_json::to_string(&chosen).unwrap();
60 assert_eq!(serde_json::from_str::<Subscription>(&text).unwrap(), chosen);
61 // A body that names the level alone, as a client sends it.
62 let body: Subscription = serde_json::from_str(r#"{"level":"all"}"#).unwrap();
63 assert_eq!((body.level.as_str(), body.since), (ALL, None));
64 let default = Subscription {
65 level: PARTICIPATING.into(),
66 since: None,
67 };
68 assert_eq!(
69 serde_json::to_string(&default).unwrap(),
70 r#"{"level":"participating"}"#
71 );
72 }
73}