pub fn string_to_slack_msg(raw: &str) -> Result<Event, EventDecodingError> {
    // The message should be a string representation of a JSON object
    let json = try!(Json::from_str(raw));
    let obj = match json {
        Json::Object(obj) => obj,
        _ => return Err(EventDecodingError::WrongType("root".to_owned(), "object".to_owned()))
    };

    // If the message does not have a `type`, it is a confirmation message
    if !obj.contains_key("type") {
        return Ok(Event::Other(obj))
    }

    // Get the message type. It should be a string.
    let msg = match obj.get("type").expect("obj has key type but failed to get").as_string() {
        Some(s) => s.to_owned(),
        None => return Err(EventDecodingError::WrongType("type".to_owned(), "string".to_owned()))
    };

    // Handle different message types
    match msg.as_ref() {
        // If it's a message type, try and return an Event::Message
        "message" => decode_msg_json(obj),
        // Some other type we don't explicitly handle
        _ => Ok(Event::Other(obj)),
    }
}
/// Convert a JSON string to a Event
Convert a JSON string to a Event
Convert a JSON string to a Event
pub fn string_to_slack_msg(raw: &str) -> Result<Event, EventDecodingError> {
    // The message should be a string representation of a JSON object
    let json = try!(Json::from_str(raw));
    let obj = match json {
        Json::Object(obj) => obj,
        _ => return Err(EventDecodingError::WrongType("root".to_owned(), "object".to_owned()))
    };
    // If the message does not have a `type`, it is a confirmation message
    if !obj.contains_key("type") {
        return Ok(Event::Other(obj))
    }
    // Get the message type. It should be a string.
    let msg = match obj.get("type").expect("obj has key type but failed to get").as_string() {
        Some(s) => s.to_owned(),
        None => return Err(EventDecodingError::WrongType("type".to_owned(), "string".to_owned()))
    };
    // Handle different message types
    match msg.as_ref() {
        // If it's a message type, try and return an Event::Message
        "message" => decode_msg_json(obj),
        // Some other type we don't explicitly handle
        _ => Ok(Event::Other(obj)),
    }
}
pub fn string_to_slack_msg ( raw : & str ) -> Result < Event , EventDecodingError > { // The message should be a string representation of a JSON object let json = try ! ( Json from_str ( raw ) ) ; let obj = match json { Json :: Object ( obj ) => obj , _ => return Err ( EventDecodingError :: WrongType ( " " . to_owned ( ) , " " . to_owned ( ) ) ) } ; // If the message does not have a `type`, it is a confirmation message if ! obj . contains_key ( " " ) { return Ok ( Event :: Other ( obj ) ) } // Get the message type. It should be a string. let msg = match obj . get ( " " ) . expect ( " " ) . as_string ( ) { Some ( s ) => s . to_owned ( ) , None => return Err ( EventDecodingError :: WrongType ( " " . to_owned ( ) , " " . to_owned ( ) ) ) } ; // Handle different message types match msg . as_ref ( ) { // If it's a message type, try and return an Event::Message " " => decode_msg_json ( obj ) , // Some other type we don't explicitly handle _ => Ok ( Event :: Other ( obj ) ) , } }
Convert a JSON string to a Event
Convert a JSON string to a Event
raw
&str
raw
&str