use ImageBuilder;
use rss::Image;
use utils::string_utils;
impl ImageBuilder
{
pub fn new() -> ImageBuilder
{
ImageBuilder::default()
}
pub fn url(&mut self, url: &str) -> &mut ImageBuilder
{
self.url = url.to_owned();
self
}
pub fn title(&mut self, title: &str) -> &mut ImageBuilder
{
self.title = title.to_owned();
self
}
pub fn link(&mut self, link: &str) -> &mut ImageBuilder
{
self.link = link.to_owned();
self
}
pub fn width(&mut self, width: Option<i64>) -> &mut ImageBuilder
{
self.width = width;
self
}
pub fn height(&mut self, height: Option<i64>) -> &mut ImageBuilder
{
self.height = height;
self
}
pub fn description(&mut self, description: Option<String>) -> &mut ImageBuilder
{
self.description = description;
self
}
pub fn validate(&mut self) -> Result<&mut ImageBuilder, String>
{
let url_string = self.url.clone();
if !url_string.ends_with(".jpeg") && !url_string.ends_with(".jpg") && !url_string.ends_with(".png") &&
!url_string.ends_with(".gif")
{
return Err("Image Url must end with .jpeg, .png, or .gif".to_owned());
}
string_utils::str_to_url(url_string.as_str())?;
string_utils::str_to_url(self.link.as_str())?;
let width_opt = self.width;
if width_opt.is_some()
{
let width = width_opt.unwrap();
if width > 144
{
return Err("Image width cannot be greater than 144.".to_owned());
}
else if width < 0
{
return Err("Image width cannot be a negative value.".to_owned());
}
}
let height_opt = self.height;
if height_opt.is_some()
{
let height = height_opt.unwrap();
if height > 144
{
return Err("Image height cannot be greater than 400.".to_owned());
}
else if height < 0
{
return Err("Image height cannot be a negative value.".to_owned());
}
}
Ok(self)
}
pub fn finalize(&self) -> Result<Image, String>
{
let width = match self.width
{
Some(val) => string_utils::i64_to_option_string(val)?,
None => string_utils::i64_to_option_string(88)?,
};
let height = match self.height
{
Some(val) => string_utils::i64_to_option_string(val)?,
None => string_utils::i64_to_option_string(31)?,
};
Ok(Image {
url: self.url.clone(),
title: self.title.clone(),
link: self.link.clone(),
width: width,
height: height,
description: self.description.clone(),
})
}
}