Advertisement
Guest User

Untitled

a guest
May 27th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. pub struct Message<'a> {
  2. // Prefix
  3. pub prefix: Option<&'a str>,
  4. // Command or reply
  5. pub command: &'a str,
  6. // Arguments
  7. pub args: Vec<&'a str>,
  8. // Suffix
  9. pub suffix: Option<&'a str>,
  10. }
  11.  
  12. pub enum ParseError {
  13. EmptyCommand,
  14. EmptyMessage,
  15. UnexpectedEnd,
  16. }
  17.  
  18. impl<'a> Message<'a> {
  19.  
  20. pub fn parse<'b>(line: &'b str) -> Result<Message<'b>, ParseError> {
  21. if line.len() == 0 {
  22. return Err(ParseError::EmptyCommand);
  23. }
  24.  
  25. let mut state = &line[..];
  26. let mut prefix: Option<&str> = None;
  27. let mut command: Option<&str> = None;
  28. let mut args: Vec<&str> = Vec::new();
  29. let mut suffix: Option<&str> = None;
  30.  
  31. // Look for a prefix
  32. if state.starts_with(":") {
  33. match state.find(" ") {
  34. None => return Err(ParseError::UnexpectedEnd),
  35. Some(idx) => {
  36. prefix = Some(&state[1..idx]);
  37. state = &state[idx + 1..];
  38. }
  39. }
  40. }
  41.  
  42. // Look for the command/reply
  43. match state.find(" ") {
  44. None => {
  45. if state.len() == 0 {
  46. return Err(ParseError::EmptyMessage);
  47. } else {
  48. command = Some(&state[..]);
  49. state = &state[state.len()..];
  50. }
  51. }
  52. Some(idx) => {
  53. command = Some(&state[..idx]);
  54. state = &state[idx + 1..];
  55. }
  56. }
  57.  
  58. // Look for arguments and suffix
  59. if state.len() > 0 {
  60. loop {
  61. if state.starts_with(":") {
  62. suffix = Some(&state[1..]);
  63. break;
  64. } else {
  65. match state.find(" ") {
  66. None => args.push(&state[..]),
  67. Some(idx) => {
  68. args.push(&state[..idx]);
  69. state = &state[idx + 1..];
  70. }
  71. }
  72. }
  73. }
  74. }
  75.  
  76. let cmd = match command {
  77. None => return Err(ParseError::EmptyCommand),
  78. Some(cmd) => cmd,
  79. };
  80.  
  81. Ok(Message {
  82. prefix: prefix,
  83. command: cmd,
  84. args: args,
  85. suffix: suffix,
  86. })
  87. }
  88.  
  89. }
  90.  
  91. #[test]
  92. fn test_full() {
  93. let res = Message::parse(":org.prefix.cool COMMAND arg1 arg2 arg3 :suffix is pretty cool yo");
  94. assert!(res.is_ok());
  95. let msg = res.ok().unwrap();
  96. assert_eq!(msg.prefix, Some("org.prefix.cool"));
  97. assert_eq!(msg.command, "COMMAND");
  98. assert_eq!(msg.args, vec!["arg1", "arg2", "arg3"]);
  99. assert_eq!(msg.suffix, Some("suffix is pretty cool yo"));
  100. }
  101.  
  102. #[test]
  103. fn test_no_prefix() {
  104. let res = Message::parse("COMMAND arg1 arg2 arg3 :suffix is pretty cool yo");
  105. assert!(res.is_ok());
  106. let msg = res.ok().unwrap();
  107. assert_eq!(msg.prefix, None);
  108. assert_eq!(msg.command, "COMMAND");
  109. assert_eq!(msg.args, vec!["arg1", "arg2", "arg3"]);
  110. assert_eq!(msg.suffix, Some("suffix is pretty cool yo"));
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement