sqry_core/schema/
duplicate.rs1use serde::{Deserialize, Serialize};
6use std::fmt;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
29#[serde(rename_all = "lowercase")]
30#[derive(Default)]
31pub enum DuplicateKind {
32 #[default]
37 Body,
38
39 Signature,
44
45 Struct,
50}
51
52impl DuplicateKind {
53 #[must_use]
55 pub const fn all() -> &'static [Self] {
56 &[Self::Body, Self::Signature, Self::Struct]
57 }
58
59 #[must_use]
61 pub const fn as_str(self) -> &'static str {
62 match self {
63 Self::Body => "body",
64 Self::Signature => "signature",
65 Self::Struct => "struct",
66 }
67 }
68
69 #[must_use]
74 pub fn parse(s: &str) -> Option<Self> {
75 match s.to_lowercase().as_str() {
76 "body" | "implementation" | "impl" => Some(Self::Body),
77 "signature" | "sig" => Some(Self::Signature),
78 "struct" | "class" | "type" => Some(Self::Struct),
79 _ => None,
80 }
81 }
82
83 #[must_use]
85 pub const fn description(self) -> &'static str {
86 match self {
87 Self::Body => "function/method body duplicates",
88 Self::Signature => "function signature duplicates",
89 Self::Struct => "struct/class definition duplicates",
90 }
91 }
92}
93
94impl fmt::Display for DuplicateKind {
95 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96 f.write_str(self.as_str())
97 }
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn test_as_str() {
106 assert_eq!(DuplicateKind::Body.as_str(), "body");
107 assert_eq!(DuplicateKind::Signature.as_str(), "signature");
108 assert_eq!(DuplicateKind::Struct.as_str(), "struct");
109 }
110
111 #[test]
112 fn test_parse() {
113 assert_eq!(DuplicateKind::parse("body"), Some(DuplicateKind::Body));
114 assert_eq!(
115 DuplicateKind::parse("SIGNATURE"),
116 Some(DuplicateKind::Signature)
117 );
118 assert_eq!(
119 DuplicateKind::parse("implementation"),
120 Some(DuplicateKind::Body)
121 );
122 assert_eq!(DuplicateKind::parse("class"), Some(DuplicateKind::Struct));
123 assert_eq!(DuplicateKind::parse("unknown"), None);
124 }
125
126 #[test]
127 fn test_display() {
128 assert_eq!(format!("{}", DuplicateKind::Body), "body");
129 assert_eq!(format!("{}", DuplicateKind::Struct), "struct");
130 }
131
132 #[test]
133 fn test_serde_roundtrip() {
134 for kind in DuplicateKind::all() {
135 let json = serde_json::to_string(kind).unwrap();
136 let deserialized: DuplicateKind = serde_json::from_str(&json).unwrap();
137 assert_eq!(*kind, deserialized);
138 }
139 }
140
141 #[test]
142 fn test_default() {
143 assert_eq!(DuplicateKind::default(), DuplicateKind::Body);
144 }
145
146 #[test]
147 fn test_description() {
148 assert!(DuplicateKind::Body.description().contains("body"));
149 assert!(DuplicateKind::Signature.description().contains("signature"));
150 assert!(DuplicateKind::Struct.description().contains("struct"));
151 }
152}