#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputBoxRequest {
pub prompt: String,
pub title: Option<String>,
pub default_response: String,
pub xpos: Option<i32>,
pub ypos: Option<i32>,
}
impl InputBoxRequest {
pub fn new(prompt: impl Into<String>) -> Self {
Self {
prompt: prompt.into(),
title: None,
default_response: String::new(),
xpos: None,
ypos: None,
}
}
pub fn with_title(mut self, title: Option<String>) -> Self {
self.title = title;
self
}
pub fn with_default(mut self, default_response: impl Into<String>) -> Self {
self.default_response = default_response.into();
self
}
pub fn with_position(mut self, xpos: i32, ypos: i32) -> Self {
self.xpos = Some(xpos);
self.ypos = Some(ypos);
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InputBoxRecord {
pub prompt: String,
pub title: Option<String>,
pub default_response: String,
pub xpos: Option<i32>,
pub ypos: Option<i32>,
}
impl InputBoxRecord {
pub fn of(request: &InputBoxRequest) -> Self {
Self {
prompt: request.prompt.clone(),
title: request.title.clone(),
default_response: request.default_response.clone(),
xpos: request.xpos,
ypos: request.ypos,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_starts_minimal() {
let request = InputBoxRequest::new("name?");
assert_eq!(request.prompt, "name?");
assert_eq!(request.title, None);
assert_eq!(request.default_response, "");
assert_eq!(request.xpos, None);
assert_eq!(request.ypos, None);
}
#[test]
fn builders_chain() {
let request = InputBoxRequest::new("age?")
.with_title(Some("Age".into()))
.with_default("18")
.with_position(1000, 2000);
assert_eq!(request.title.as_deref(), Some("Age"));
assert_eq!(request.default_response, "18");
assert_eq!(request.xpos, Some(1000));
assert_eq!(request.ypos, Some(2000));
}
#[test]
fn record_captures_display_relevant_parts() {
let request = InputBoxRequest::new("Proceed?")
.with_title(Some("App".into()))
.with_default("yes")
.with_position(-5, 10);
let record = InputBoxRecord::of(&request);
assert_eq!(record.prompt, "Proceed?");
assert_eq!(record.title.as_deref(), Some("App"));
assert_eq!(record.default_response, "yes");
assert_eq!(record.xpos, Some(-5));
assert_eq!(record.ypos, Some(10));
}
}