Guest User

Untitled

a guest
May 27th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. trait FsmState {
  2. fn step(&self, c: char) -> Box<FsmState>;
  3.  
  4. fn is_terminal(&self) -> bool {
  5. false
  6. }
  7.  
  8. fn is_error(&self) -> bool {
  9. false
  10. }
  11.  
  12. fn is_valid_end(&self) -> bool {
  13. self.is_terminal() && !self.is_error()
  14. }
  15.  
  16. fn upcast(&self) -> Box<FsmState>;
  17. }
  18.  
  19.  
  20. #[derive(Clone)]
  21. struct A {}
  22. trait AEgress : FsmState { }
  23. impl AEgress for A {}
  24. impl AEgress for B {}
  25. impl AEgress for Error {}
  26.  
  27. #[derive(Clone)]
  28. struct B {}
  29. trait BEgress : FsmState {}
  30. impl BEgress for B {}
  31. impl BEgress for C {}
  32. impl BEgress for Error {}
  33.  
  34.  
  35. #[derive(Clone)]
  36. struct C {}
  37. trait CEgress : FsmState {}
  38. impl CEgress for Error {}
  39.  
  40.  
  41. #[derive(Clone)]
  42. struct Error {}
  43. trait ErrorEgress : FsmState {}
  44. impl ErrorEgress for Error {}
  45.  
  46.  
  47. impl FsmState for A {
  48. fn step(&self, c: char) -> Box<FsmState> {
  49. fn doit(c: char) -> Box<AEgress> {
  50. match c {
  51. 'a' => Box::new(A {}),
  52. 'b' => Box::new(B {}),
  53. _ => Box::new(Error {}),
  54. }
  55. }
  56.  
  57. doit(c).upcast()
  58. }
  59.  
  60. fn upcast(&self) -> Box<FsmState> {
  61. Box::new(self.clone())
  62. }
  63. }
  64.  
  65. impl FsmState for B {
  66. fn step(&self, c: char) -> Box<FsmState> {
  67. fn doit(c: char) -> Box<BEgress> {
  68. match c {
  69. 'b' => Box::new(B {}),
  70. 'c' => Box::new(C {}),
  71. _ => Box::new(Error {}),
  72. }
  73. }
  74.  
  75. doit(c).upcast()
  76. }
  77.  
  78. fn upcast(&self) -> Box<FsmState> {
  79. Box::new(self.clone())
  80. }
  81. }
  82.  
  83. impl FsmState for C {
  84. fn step(&self, c: char) -> Box<FsmState> {
  85. fn doit(_c: char) -> Box<CEgress> {
  86. Box::new(Error {})
  87. }
  88.  
  89. doit(c).upcast()
  90. }
  91.  
  92. fn is_terminal(&self) -> bool {
  93. true
  94. }
  95.  
  96. fn upcast(&self) -> Box<FsmState> {
  97. Box::new(self.clone())
  98. }
  99. }
  100.  
  101. impl FsmState for Error {
  102. fn step(&self, c: char) -> Box<FsmState> {
  103. fn doit(_c: char) -> Box<CEgress> {
  104. Box::new(Error {})
  105. }
  106.  
  107. doit(c).upcast()
  108. }
  109.  
  110. fn is_error(&self) -> bool {
  111. true
  112. }
  113.  
  114. fn upcast(&self) -> Box<FsmState> {
  115. Box::new(self.clone())
  116. }
  117. }
  118.  
  119.  
  120. fn accept(s: &str) -> bool {
  121. let start : Box<FsmState> = Box::new(A {});
  122. s.chars()
  123. .fold(start, |st, c| st.step(c))
  124. .is_valid_end()
  125. }
  126.  
  127. #[cfg(test)]
  128. mod tests {
  129. use super::*;
  130.  
  131. #[test]
  132. fn test_abc() {
  133. assert!(accept("abc"));
  134. assert!(accept("aaaabc"));
  135. assert!(accept("abbbbbc"));
  136. assert!(accept("aaaaabbbbbc"));
  137. }
  138. }
Add Comment
Please, Sign In to add comment