zulip-api 0.0.8

Client library for interacting with the Zulip API
Documentation
use serde::{Deserialize, Serialize};

use crate::{field_access, impl_default, impl_display};

/// Represents a user's personal configuration for a specific navigation view.
///
/// Displayed most visibly at the top of the web application's left sidebar.
///
/// Navigation views can be either an override to the default behavior of a built-in view, or a custom view.
///
/// **Changes**: New in Zulip 11.0 (feature level 390).
///
/// Modified from `openapi-generator-cli` output:
///
/// ```shell,no_build,no_run
/// $ openapi-generator-cli generate -g rust -i ./zerver/openapi/zulip.yaml -o zulip-openapi --skip-validate-spec
/// ```
///
/// ```text
/// /*
/// * Zulip REST API
/// *
/// * Powerful open source group chat
/// *
/// * The version of the OpenAPI document: 1.0.0
/// *
/// * Generated by: https://openapi-generator.tech
/// */
/// ```
///
/// See [zerver/openapi/zulip.yaml](https://github.com/zulip/zulip/blob/main/zerver/openapi/zulip.yaml) for the original OpenAPI configuration.
///
/// Configuration slightly modified to work with the Rust generator of [`openapi-generator-cli`](https://openapi-generator.tech).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NavigationView {
    fragment: String,
    is_pinned: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

field_access! {
    NavigationView {
        /// A unique identifier for the view, used to determine navigation behavior when clicked.
        ///
        /// Clients should use this value to navigate to the corresponding URL hash.
        fragment: cow { str },
    }
}

field_access! {
    NavigationView {
        /// Determines whether the view appears directly in the sidebar or is hidden in the \"More Views\" menu.
        ///
        /// - `true`: Pinned and visible in the sidebar.
        /// - `false`: Hidden and accessible via the \"More Views\" menu.
        is_pinned: bool,
    }
}

field_access! {
    NavigationView {
        /// The user-facing name for custom navigation views.
        ///
        /// Omit this field for built-in views.
        name: option_cow_deref { str, String },
    }
}

impl NavigationView {
    /// Creates a new [NavigationView].
    pub const fn new() -> Self {
        Self {
            fragment: String::new(),
            is_pinned: false,
            name: None,
        }
    }
}

impl_default!(NavigationView);
impl_display!(NavigationView);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_navigation_view() {
        let fragment = "test fragment";
        let is_pinned = true;
        let name = "test name";

        let view = NavigationView::new()
            .with_fragment(fragment)
            .with_is_pinned(is_pinned)
            .with_name(name);
        let view_json = view.to_string();

        assert_eq!(serde_json::to_string(&view).unwrap(), view_json);
        assert_eq!(
            serde_json::from_str::<NavigationView>(&view_json).unwrap(),
            view
        );

        assert_eq!(view.fragment(), fragment);
        assert_eq!(view.is_pinned(), is_pinned);
        assert_eq!(view.name(), Some(name));
    }
}