git_bot_feedback/comments/
thread_comments.rs1#[cfg(feature = "pyo3")]
2use pyo3::prelude::*;
3
4use super::DEFAULT_MARKER;
5
6#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
10#[cfg_attr(feature = "pyo3", pyclass(module = "git_bot_feedback", from_py_object))]
11pub enum CommentKind {
12 #[default]
14 Concerns,
15
16 Lgtm,
18}
19
20#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
24#[cfg_attr(feature = "pyo3", pyclass(module = "git_bot_feedback", from_py_object))]
25pub enum CommentPolicy {
26 Anew,
31
32 #[default]
36 Update,
37}
38
39#[derive(Debug, Clone)]
41#[cfg_attr(
42 feature = "pyo3",
43 pyclass(module = "git_bot_feedback", from_py_object, get_all, set_all)
44)]
45pub struct ThreadCommentOptions {
46 pub policy: CommentPolicy,
50
51 pub comment: String,
53
54 pub kind: CommentKind,
56
57 pub marker: String,
74
75 pub no_lgtm: bool,
80}
81
82impl Default for ThreadCommentOptions {
83 fn default() -> Self {
84 Self {
85 policy: Default::default(),
86 comment: Default::default(),
87 kind: Default::default(),
88 marker: DEFAULT_MARKER.to_string(),
89 no_lgtm: Default::default(),
90 }
91 }
92}
93
94impl ThreadCommentOptions {
95 pub fn mark_comment(&self) -> String {
102 if !self.comment.starts_with(&self.marker) {
103 return format!("{}{}", self.marker, self.comment);
104 }
105 self.comment.clone()
106 }
107}
108
109#[cfg(feature = "pyo3")]
110#[pymethods]
111impl ThreadCommentOptions {
112 #[new]
114 #[pyo3(
115 signature = (
116 policy = None,
117 comment = None,
118 kind = None,
119 marker = None,
120 no_lgtm = None,
121 ),
122 text_signature = "(policy: CommentPolicy | None = None, comment: str | None = None, kind: CommentKind | None = None, marker: str | None = None, no_lgtm: bool = False)",
123 )]
124 pub fn new(
125 policy: Option<CommentPolicy>,
126 comment: Option<String>,
127 kind: Option<CommentKind>,
128 marker: Option<String>,
129 no_lgtm: Option<bool>,
130 ) -> Self {
131 Self {
132 policy: policy.unwrap_or_default(),
133 comment: comment.unwrap_or_default(),
134 kind: kind.unwrap_or_default(),
135 marker: marker.unwrap_or_else(|| DEFAULT_MARKER.to_string()),
136 no_lgtm: no_lgtm.unwrap_or_default(),
137 }
138 }
139}
140
141#[cfg(test)]
142mod test {
143 #![allow(clippy::unwrap_used)]
144
145 use super::{DEFAULT_MARKER, ThreadCommentOptions};
146 use chrono::NaiveDateTime;
147
148 #[test]
149 fn default_marker() {
150 let mut opts = ThreadCommentOptions::default();
151 assert_eq!(opts.marker, DEFAULT_MARKER);
152 let datetime_start = concat!(
153 "<!-- ",
154 env!("CARGO_CRATE_NAME"),
155 "/",
156 env!("CARGO_PKG_VERSION"),
157 "/",
158 )
159 .len();
160 let datetime_end = DEFAULT_MARKER.len() - 5;
161 let datetime_str = &DEFAULT_MARKER[datetime_start..datetime_end];
162 NaiveDateTime::parse_from_str(datetime_str, "%b-%d-%Y_%H-%M").unwrap();
163 assert_eq!(opts.mark_comment(), DEFAULT_MARKER);
164 let comment = format!("{DEFAULT_MARKER}Some text data.");
165 opts.comment = comment.clone();
166 assert_eq!(opts.mark_comment(), comment);
167 }
168}