use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use crate::errors::Error;
use crate::types::Vec2;
#[derive(Clone, Default, Debug, PartialEq)]
pub struct Style {
pub id: Option<String>,
pub balloon: Option<BalloonStyle>,
pub icon: Option<IconStyle>,
pub label: Option<LabelStyle>,
pub line: Option<LineStyle>,
pub poly: Option<PolyStyle>,
pub list: Option<ListStyle>,
pub attrs: HashMap<String, String>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct StyleMap {
pub id: Option<String>,
pub pairs: Vec<Pair>,
pub attrs: HashMap<String, String>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Pair {
pub key: String,
pub style_url: String,
pub attrs: HashMap<String, String>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BalloonStyle {
pub id: Option<String>,
pub bg_color: Option<String>,
pub text_color: String,
pub text: Option<String>,
pub display: bool,
pub attrs: HashMap<String, String>,
}
impl Default for BalloonStyle {
fn default() -> BalloonStyle {
BalloonStyle {
id: None,
bg_color: None,
text_color: "ffffffff".to_string(),
text: None,
display: true,
attrs: HashMap::new(),
}
}
}
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub enum ColorMode {
#[default]
Normal,
Random,
}
impl FromStr for ColorMode {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"normal" => Ok(Self::Normal),
"random" => Ok(Self::Random),
v => Err(Error::InvalidColorMode(v.to_string())),
}
}
}
impl fmt::Display for ColorMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Normal => "normal",
Self::Random => "random",
}
)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct IconStyle {
pub id: Option<String>,
pub scale: f64,
pub heading: f64,
pub hot_spot: Option<Vec2>,
pub icon: Icon,
pub color: String,
pub color_mode: ColorMode,
pub attrs: HashMap<String, String>,
}
impl Default for IconStyle {
fn default() -> IconStyle {
IconStyle {
id: None,
scale: 1.0,
heading: 0.0,
hot_spot: None,
icon: Icon::default(),
color: "ffffffff".to_string(),
color_mode: ColorMode::default(),
attrs: HashMap::new(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Icon {
pub href: String,
pub attrs: HashMap<String, String>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct LabelStyle {
pub id: Option<String>,
pub color: String,
pub color_mode: ColorMode,
pub scale: f64,
pub attrs: HashMap<String, String>,
}
impl Default for LabelStyle {
fn default() -> LabelStyle {
LabelStyle {
id: None,
color: "ffffffff".to_string(),
color_mode: ColorMode::default(),
scale: 1.0,
attrs: HashMap::new(),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct LineStyle {
pub id: Option<String>,
pub color: String,
pub color_mode: ColorMode,
pub width: f64,
pub attrs: HashMap<String, String>,
}
impl Default for LineStyle {
fn default() -> LineStyle {
LineStyle {
id: None,
color: "ffffffff".to_string(),
color_mode: ColorMode::default(),
width: 1.0,
attrs: HashMap::new(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PolyStyle {
pub id: Option<String>,
pub color: String,
pub color_mode: ColorMode,
pub fill: bool,
pub outline: bool,
pub attrs: HashMap<String, String>,
}
impl Default for PolyStyle {
fn default() -> PolyStyle {
PolyStyle {
id: None,
color: "ffffffff".to_string(),
color_mode: ColorMode::default(),
fill: true,
outline: true,
attrs: HashMap::new(),
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum ListItemType {
#[default]
Check,
CheckOffOnly,
CheckHideChildren,
RadioFolder,
}
impl FromStr for ListItemType {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"check" => Ok(Self::Check),
"checkOffOnly" => Ok(Self::CheckOffOnly),
"checkHideChildren" => Ok(Self::CheckHideChildren),
"radioFolder" => Ok(Self::RadioFolder),
v => Err(Error::InvalidListItemType(v.to_string())),
}
}
}
impl fmt::Display for ListItemType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Self::Check => "check",
Self::CheckOffOnly => "checkOffOnly",
Self::CheckHideChildren => "checkHideChildren",
Self::RadioFolder => "radioFolder",
}
)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ListStyle {
pub id: Option<String>,
pub bg_color: String,
pub max_snippet_lines: u32,
pub list_item_type: ListItemType,
pub attrs: HashMap<String, String>,
}
impl Default for ListStyle {
fn default() -> ListStyle {
ListStyle {
id: None,
bg_color: "ffffffff".to_string(),
max_snippet_lines: 2,
list_item_type: ListItemType::default(),
attrs: HashMap::new(),
}
}
}