Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. fn main() {
  2. println!("Hello, world!");
  3. use Template::*;
  4.  
  5. enum Action {
  6. Increment,
  7. Decrement,
  8. }
  9.  
  10. let mut foo: Widget<i32, _, _, Action> = Widget {
  11. model: 0,
  12. update: |x, a| match a {
  13. Action::Increment => x + 1,
  14. Action::Decrement => x - 1,
  15. },
  16. template: |x| {
  17. let children = if x > 0 {
  18. text("Greater than Zero".to_string())
  19. } else {
  20. text(x.to_string())
  21. };
  22.  
  23. div{
  24. children: vec![children],
  25. on_click: Action::Increment,
  26. }
  27. },
  28. inbox: Vec::new(),
  29. };
  30. println!("{}", foo.render().to_string());
  31. foo.tick(Action::Increment);
  32. println!("{}", foo.render().to_string());
  33. foo.tick(Action::Increment);
  34. println!("{}", foo.render().to_string());
  35. foo.tick(Action::Decrement);
  36. println!("{}", foo.render().to_string());
  37. foo.tick(Action::Decrement);
  38. println!("{}", foo.render().to_string());
  39. }
  40.  
  41. struct Widget<S, U, T, A>
  42. where S: Sized + Copy + Clone,
  43. A: Sized,
  44. U: Fn(S, A) -> S,
  45. T: FnMut(S) -> Template<A>
  46. {
  47. model: S,
  48. update: U,
  49. template: T,
  50. inbox: Vec<A>,
  51. }
  52.  
  53. impl<S, U, T, A> Widget<S, U, T, A>
  54. where S: Sized + Copy + Clone,
  55. U: Fn(S, A) -> S,
  56. T: FnMut(S) -> Template<A>,
  57. A: Sized,
  58. {
  59. fn render(&mut self) -> Template<A> {
  60. (self.template)(self.model)
  61. }
  62.  
  63. fn tick(&mut self, action: A) -> () {
  64. let value: S = (self.update)(self.model, action);
  65. self.model = value;
  66. }
  67. }
  68.  
  69. enum Template<A>
  70. where A: Sized
  71. {
  72. div {
  73. on_click: A,
  74. children: Vec<Template<A>>,
  75. },
  76. h1 {
  77. on_click: A,
  78. children: Vec<Template<A>>,
  79. },
  80. h2 {
  81. on_click: A,
  82. children: Vec<Template<A>>
  83. },
  84. text(String),
  85. }
  86.  
  87. impl<A> Template<A> where A: Sized {
  88. fn to_string(&self) -> String {
  89. match self {
  90. &Template::div{ref children,..} => {
  91. let mut buffer = String::new();
  92. buffer.push_str("<div>");
  93. for child in children.iter() {
  94. buffer.push_str(&child.to_string());
  95. }
  96. buffer.push_str("</div>");
  97. buffer
  98. }
  99. &Template::h1{ref children,..} => {
  100. let mut buffer = String::new();
  101. buffer.push_str("<h1>");
  102. for child in children.iter() {
  103. buffer.push_str(&child.to_string());
  104. }
  105. buffer.push_str("</h1>");
  106. buffer
  107. },
  108. &Template::h2{ref children, ..} => {
  109. let mut buffer = String::new();
  110. buffer.push_str("<h2>");
  111. for child in children.iter() {
  112. buffer.push_str(&child.to_string());
  113. }
  114. buffer.push_str("</h2>");
  115. buffer
  116. },
  117. &Template::text(ref value) => value.to_string(),
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement