// Copyright Two Neutron Stars Incorporated and contributors
// SPDX-License-Identifier: BlueOak-1.0.0

//! Implementation of `Bluegum` and `BluegumWithState` for the `url::Url` type.
//!
//! This module is only available when the `url` feature is enabled.

use crate::{
  Bluegum,
  BluegumWithState,
};

/// Implementation of Bluegum for url::Url
///
/// Displays all components of the URL as fields, with optional fields
/// only shown when they have values.
impl Bluegum for url::Url {
  fn node(&self, b: &mut crate::Builder) {
    b.name("Url")
      .field("scheme", self.scheme())
      .option_field(
        "username",
        if self.username().is_empty() {
          None
        } else {
          Some(self.username())
        },
      )
      .option_field("password", self.password())
      .option_field("host", self.host_str())
      .option_field("port", self.port())
      .field("path", self.path())
      .option_field("query", self.query())
      .option_field("fragment", self.fragment());
  }
}

/// Stateful implementation of BluegumWithState for url::Url
///
/// Behaves identically to the stateless implementation since URL
/// rendering doesn't depend on external state.
impl<State> BluegumWithState<State> for url::Url {
  fn node_with_state(&self, b: &mut crate::Builder, _state: &State) {
    b.name("Url")
      .field("scheme", self.scheme())
      .option_field(
        "username",
        if self.username().is_empty() {
          None
        } else {
          Some(self.username())
        },
      )
      .option_field("password", self.password())
      .option_field("host", self.host_str())
      .option_field("port", self.port())
      .field("path", self.path())
      .option_field("query", self.query())
      .option_field("fragment", self.fragment());
  }
}
