1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use models::Rectangle;

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum ImageActionType {
    Uri     {
        #[serde(rename = "linkUri")]
        link_uri: String,
    },
    Message {
        text : String
    },
}

#[derive(Serialize, Deserialize, Clone)]
pub struct ImagemapAction {
    #[serde(rename="type", flatten)]
    kind: ImageActionType,
    #[serde(default)]
    label   : String,
    area: Rectangle,
}

impl ImagemapAction {
    pub fn new(kind: ImageActionType, label: &str, area: Rectangle) -> ImagemapAction {
        ImagemapAction { kind: kind, label: String::from(label), area: area}
    }

    pub fn create_imagemap_uri_action(label: &str, link_uri: &str, x: u64, y: u64, width: u64, height: u64) -> ImagemapAction {
        ImagemapAction {
            kind: ImageActionType::Uri { link_uri: String::from(link_uri) },
            label: String::from(label),
            area: Rectangle { x, y, width, height }
        }
    }

    pub fn create_imagemap_message_action(label: &str, text: &str, x: u64, y: u64, width: u64, height: u64) -> ImagemapAction {
        ImagemapAction {
            kind: ImageActionType::Message { text: String::from(text) },
            label: String::from(label),
            area: Rectangle { x, y, width, height }
        }
    }
}

#[derive(Serialize, Deserialize, Clone)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum ActionType {
    Message { text: String },
    Uri { uri: String },
    Postback { 
        data: String,
        #[serde(skip_serializing_if = "String::is_empty")]
        display_text: String,
        #[serde(skip_serializing_if = "String::is_empty")]
        text: String,
    },
    Datetimepicker {
        data   : String,
        mode   : String,
        initial: String,
        max    : String,
        min    : String,
    },
    Empty,
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Action {
    #[serde(flatten)]
    kind:  ActionType,
    label: String,
}

impl Action {
    pub fn new(kind: ActionType, label: &str) -> Action { Action { kind, label: String::from(label) } }

    pub fn create_empty() -> Action { Action { kind: ActionType::Empty, label: String::new() }}

    pub fn is_empty(&self) -> bool {
        match self.kind {
            ActionType::Empty => true,
            _                 => false,
        }
    }

    pub fn create_message(label: &str, text: &str) -> Action {
        Action { label: String::from(label), kind: ActionType::Message { text: String::from(text)} }
    }

    pub fn create_uri(label: &str, uri: &str) -> Action {
        Action { label: String::from(label), kind: ActionType::Uri { uri: String::from(uri)} }
    }

    pub fn create_postback(label: &str, data: &str) -> Action {
        Action {
            label: String::from(label),
            kind: ActionType::Postback {
                data        : String::from(data),
                display_text: String::new(),
                text        : String::new(),
            }
        }
    }
    pub fn create_postback_with_display_text(label: &str, data: &str, display_text: &str, text: &str) -> Action {
        Action {
            label: String::from(label),
            kind: ActionType::Postback {
                data        : String::from(data),
                display_text: String::from(display_text),
                text        : String::new(),
            }
        }
    }
    pub fn create_postback_with_text(label: &str, data: &str, text: &str) -> Action {
        Action {
            label: String::from(label),
            kind: ActionType::Postback {
                data        : String::from(data),
                display_text: String::new(),
                text        : String::from(text),
            }
        }
    }

    pub fn create_datetimepicker(label: &str, data: &str, mode: &str, initial: &str, max: &str, min: &str) -> Action {
        Action {
            label: String::from(label),
            kind: ActionType::Datetimepicker {
                data   : String::from(data),
                mode   : String::from(mode),
                initial: String::from(initial),
                max    : String::from(max),
                min    : String::from(min),
            }
        }
    }

}