zulip_api/message/navigation_view.rs
1use serde::{Deserialize, Serialize};
2
3use crate::{field_access, impl_default, impl_display};
4
5/// Represents a user's personal configuration for a specific navigation view.
6///
7/// Displayed most visibly at the top of the web application's left sidebar.
8///
9/// Navigation views can be either an override to the default behavior of a built-in view, or a custom view.
10///
11/// **Changes**: New in Zulip 11.0 (feature level 390).
12///
13/// Modified from `openapi-generator-cli` output:
14///
15/// ```shell,no_build,no_run
16/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
17/// ```
18///
19/// ```text
20/// /*
21/// * Zulip REST API
22/// *
23/// * Powerful open source group chat
24/// *
25/// * The version of the OpenAPI document: 1.0.0
26/// *
27/// * Generated by: https://openapi-generator.tech
28/// */
29/// ```
30///
31/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
32///
33/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
34#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
35pub struct NavigationView {
36 fragment: String,
37 is_pinned: bool,
38 #[serde(skip_serializing_if = "Option::is_none")]
39 name: Option<String>,
40}
41
42field_access! {
43 NavigationView {
44 /// A unique identifier for the view, used to determine navigation behavior when clicked.
45 ///
46 /// Clients should use this value to navigate to the corresponding URL hash.
47 fragment: cow { str },
48 }
49}
50
51field_access! {
52 NavigationView {
53 /// Determines whether the view appears directly in the sidebar or is hidden in the \"More Views\" menu.
54 ///
55 /// - `true`: Pinned and visible in the sidebar.
56 /// - `false`: Hidden and accessible via the \"More Views\" menu.
57 is_pinned: bool,
58 }
59}
60
61field_access! {
62 NavigationView {
63 /// The user-facing name for custom navigation views.
64 ///
65 /// Omit this field for built-in views.
66 name: option_cow_deref { str, String },
67 }
68}
69
70impl NavigationView {
71 /// Creates a new [NavigationView].
72 pub const fn new() -> Self {
73 Self {
74 fragment: String::new(),
75 is_pinned: false,
76 name: None,
77 }
78 }
79}
80
81impl_default!(NavigationView);
82impl_display!(NavigationView);
83
84#[cfg(test)]
85mod tests {
86 use super::*;
87
88 #[test]
89 fn test_navigation_view() {
90 let fragment = "test fragment";
91 let is_pinned = true;
92 let name = "test name";
93
94 let view = NavigationView::new()
95 .with_fragment(fragment)
96 .with_is_pinned(is_pinned)
97 .with_name(name);
98 let view_json = view.to_string();
99
100 assert_eq!(serde_json::to_string(&view).unwrap(), view_json);
101 assert_eq!(
102 serde_json::from_str::<NavigationView>(&view_json).unwrap(),
103 view
104 );
105
106 assert_eq!(view.fragment(), fragment);
107 assert_eq!(view.is_pinned(), is_pinned);
108 assert_eq!(view.name(), Some(name));
109 }
110}