Skip to content

Commit

Permalink
refactor: remove derivative crate and update impls
Browse files Browse the repository at this point in the history
This commit refactors the code to remove the usage of the derivative
crate. The necessary trait implementations (Debug, Default, PartialEq,
Eq, Ord, PartialOrd) are now manually added to the structs. This change
simplifies the code and removes an external dependency.
  • Loading branch information
sarub0b0 committed Oct 24, 2024
1 parent 8a4b4e7 commit 8105f85
Show file tree
Hide file tree
Showing 19 changed files with 160 additions and 103 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ base64 = "0.22.1"
# tui
async-trait = "0.1.80"
chrono = "0.4.38"
derivative = "2.2"
enum_dispatch = "0.3.13"
fuzzy-matcher = "0.3.7"
ratatui = "0.28.0"
Expand Down
1 change: 1 addition & 0 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Result;

#[derive(Debug)]
pub struct Clipboard;

impl Clipboard {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::BTreeSet;

use anyhow::{Context, Result};
use derivative::Derivative;
use futures::future::{join_all, try_join_all};
use k8s_openapi::{
api::core::v1::Namespace, apimachinery::pkg::apis::meta::v1::LabelSelector, Resource as _,
Expand All @@ -21,24 +20,39 @@ use crate::{
pub type RelatedHTTPRoutes = Vec<RelatedHTTPRoute>;

/// RelatedResourceHTTPRouteのための
#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedHTTPRoute {
pub name: String,

pub namespace: String,

#[derivative(PartialEq = "ignore", PartialOrd = "ignore", Ord = "ignore")]
#[serde(skip)]
pub resource: HTTPRoute,
}

impl Eq for RelatedHTTPRoute {}

impl Ord for RelatedHTTPRoute {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// nameとnamespaceが同じであれば同じリソースとして扱う
self.name
.cmp(&other.name)
.then_with(|| self.namespace.cmp(&other.namespace))
}
}

impl PartialOrd for RelatedHTTPRoute {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

impl PartialEq for RelatedHTTPRoute {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.namespace == other.namespace
}
}

impl RelatedHTTPRoute {
fn new(resource: HTTPRoute) -> Self {
Self {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use derivative::Derivative;
use futures::StreamExt as _;
use k8s_openapi::{api::core::v1::Service, Resource};
use kube::{Api, Client, ResourceExt};
Expand All @@ -14,8 +13,7 @@ use super::httproute::RelatedHTTPRoute;

pub type RelatedServices = Vec<RelatedService>;

#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedService {
/// Service Name
pub name: String,
Expand All @@ -26,11 +24,29 @@ pub struct RelatedService {
/// HTTPRoute Name
pub httproute: String,

#[derivative(PartialEq = "ignore", PartialOrd = "ignore", Ord = "ignore")]
#[serde(skip)]
pub resource: Service,
}

impl Eq for RelatedService {}

impl Ord for RelatedService {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name
.cmp(&other.name)
.then_with(|| self.namespace.cmp(&other.namespace))
.then_with(|| self.httproute.cmp(&other.httproute))
}
}

impl PartialEq for RelatedService {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.namespace == other.namespace
&& self.httproute == other.httproute
}
}

impl PartialOrd for RelatedService {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::BTreeSet;

use anyhow::{Context as _, Result};
use derivative::Derivative;
use futures::future::{join_all, try_join_all};
use k8s_openapi::{
api::core::v1::Namespace, apimachinery::pkg::apis::meta::v1::LabelSelector, Resource as _,
Expand All @@ -21,18 +20,33 @@ use crate::{
pub type RelatedHTTPRoutes = Vec<RelatedHTTPRoute>;

/// RelatedResourceHTTPRouteのための
#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedHTTPRoute {
pub name: String,

pub namespace: String,

#[derivative(PartialEq = "ignore", PartialOrd = "ignore", Ord = "ignore")]
#[serde(skip)]
pub resource: HTTPRoute,
}

impl Eq for RelatedHTTPRoute {}

impl Ord for RelatedHTTPRoute {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
// nameとnamespaceが同じであれば同じリソースとして扱う
self.name
.cmp(&other.name)
.then_with(|| self.namespace.cmp(&other.namespace))
}
}

impl PartialEq for RelatedHTTPRoute {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.namespace == other.namespace
}
}

impl PartialOrd for RelatedHTTPRoute {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use derivative::Derivative;
use futures::StreamExt;
use k8s_openapi::{api::core::v1::Service, Resource};
use kube::{Api, Client, ResourceExt};
Expand All @@ -14,8 +13,7 @@ use super::httproute::RelatedHTTPRoute;

pub type RelatedServices = Vec<RelatedService>;

#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedService {
/// Service Name
pub name: String,
Expand All @@ -26,11 +24,29 @@ pub struct RelatedService {
/// HTTPRoute Name
pub httproute: String,

#[derivative(PartialEq = "ignore", PartialOrd = "ignore", Ord = "ignore")]
#[serde(skip)]
pub resource: Service,
}

impl Eq for RelatedService {}

impl Ord for RelatedService {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name
.cmp(&other.name)
.then_with(|| self.namespace.cmp(&other.namespace))
.then_with(|| self.httproute.cmp(&other.httproute))
}
}

impl PartialEq for RelatedService {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.namespace == other.namespace
&& self.httproute == other.httproute
}
}

impl PartialOrd for RelatedService {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use derivative::Derivative;
use futures::StreamExt as _;
use k8s_openapi::{api::core::v1::Service, Resource};
use kube::{Api, Client, ResourceExt};
Expand All @@ -13,20 +12,34 @@ use crate::{

pub type RelatedServices = Vec<RelatedService>;

#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedService {
/// Service Name
pub name: String,

/// Service Namespace
pub namespace: String,

#[derivative(PartialEq = "ignore", PartialOrd = "ignore", Ord = "ignore")]
#[serde(skip)]
pub resource: Service,
}

impl Eq for RelatedService {}

impl Ord for RelatedService {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name
.cmp(&other.name)
.then_with(|| self.namespace.cmp(&other.namespace))
}
}

impl PartialEq for RelatedService {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.namespace == other.namespace
}
}

impl PartialOrd for RelatedService {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::Result;
use derivative::Derivative;
use futures::StreamExt as _;
use k8s_openapi::{api::core::v1::Service, Resource};
use kube::{Api, Client, ResourceExt};
Expand All @@ -13,20 +12,34 @@ use crate::{

pub type RelatedServices = Vec<RelatedService>;

#[derive(Derivative, Debug, Clone, Serialize, Deserialize)]
#[derivative(PartialEq, Eq, Ord)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RelatedService {
/// Service Name
pub name: String,

/// Service Namespace
pub namespace: String,

#[derivative(PartialEq = "ignore", PartialOrd = "ignore", Ord = "ignore")]
#[serde(skip)]
pub resource: Service,
}

impl Eq for RelatedService {}

impl Ord for RelatedService {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.name
.cmp(&other.name)
.then_with(|| self.namespace.cmp(&other.namespace))
}
}

impl PartialEq for RelatedService {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.namespace == other.namespace
}
}

impl PartialOrd for RelatedService {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
Expand Down
6 changes: 6 additions & 0 deletions src/ui/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ macro_rules! define_callback {
$scope closure: std::rc::Rc<dyn [<$cb_name Fn>]>
}

impl std::fmt::Debug for $cb_name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} {{ closure: _ }}", stringify!($cb_name))
}
}

impl $cb_name {
$scope fn new<F>(cb: F) -> $cb_name
where
Expand Down
9 changes: 2 additions & 7 deletions src/ui/widget/input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::time::{Duration, Instant};

use derivative::Derivative;
use ratatui::{
crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent},
layout::{Constraint, Direction, Layout, Rect},
Expand Down Expand Up @@ -198,14 +197,12 @@ impl Content {
}
}

#[derive(Derivative)]
#[derivative(Debug, Default)]
#[derive(Debug, Default)]
pub struct InputFormBuilder {
id: String,
widget_base: WidgetBase,
prefix: Line<'static>,
suffix: Line<'static>,
#[derivative(Debug = "ignore")]
actions: Vec<(UserEvent, Callback)>,
}

Expand Down Expand Up @@ -254,8 +251,7 @@ impl InputFormBuilder {

/// 検索・フィルタリング用の入力フォーム
/// 複数行は扱わない
#[derive(Derivative)]
#[derivative(Debug, Default)]
#[derive(Debug, Default)]
pub struct InputForm {
id: String,
content: Content,
Expand All @@ -267,7 +263,6 @@ pub struct InputForm {
suffix: Line<'static>,
widget_base: WidgetBase,
scroll: usize,
#[derivative(Debug = "ignore")]
actions: Vec<(UserEvent, Callback)>,
}

Expand Down
13 changes: 3 additions & 10 deletions src/ui/widget/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use ratatui::{
Frame,
};

use derivative::*;

use super::{base::WidgetBase, Item, LiteralItem, RenderTrait, SelectedItem, WidgetTrait};

use crate::{
Expand Down Expand Up @@ -74,31 +72,26 @@ define_callback!(pub OnSelectCallback, Fn(&mut Window, &LiteralItem) -> EventRes
define_callback!(pub RenderBlockInjection, Fn(&List, bool) -> Block<'static>);

use inner_item::InnerItem;
#[derive(Derivative)]
#[derivative(Debug, Default)]

#[derive(Debug, Default)]
pub struct List<'a> {
id: String,
widget_base: WidgetBase,
items: InnerItem<'a>,
state: ListState,
chunk: Rect,
inner_chunk: Rect,
#[derivative(Debug = "ignore")]
on_select: Option<OnSelectCallback>,
#[derivative(Debug = "ignore")]
block_injection: Option<RenderBlockInjection>,
}

#[derive(Derivative)]
#[derivative(Debug, Default)]
#[derive(Debug, Default)]
pub struct ListBuilder {
id: String,
widget_base: WidgetBase,
items: Vec<LiteralItem>,
state: ListState,
#[derivative(Debug = "ignore")]
on_select: Option<OnSelectCallback>,
#[derivative(Debug = "ignore")]
block_injection: Option<RenderBlockInjection>,
}

Expand Down
Loading

0 comments on commit 8105f85

Please sign in to comment.