Guest User

Untitled

a guest
Jun 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. use std::fmt::{Display,Formatter};
  2. use std::fmt;
  3.  
  4.  
  5.  
  6. #[derive(Copy,Clone)]
  7. enum AType {
  8. ARG,
  9. STYLE,
  10. }
  11.  
  12. use self::AType::*;
  13.  
  14.  
  15.  
  16. #[derive(Clone)]
  17. struct Arg {
  18. k:String,
  19. v:String,
  20. tp:AType,
  21. }
  22.  
  23. #[derive(Clone)]
  24. pub struct SArg{
  25. items :Vec<Arg>,
  26. }
  27.  
  28.  
  29. /// # example
  30. ///
  31. /// ```
  32. /// let a = SArg::new().arg("f",7).style("p","rt");
  33. /// assert_eq!(r#"f="6" style="p:rt;""#,&format!("{}",a));
  34. /// ```
  35. impl SArg {
  36. pub fn new()->SArg{
  37. SArg{
  38. items:Vec::new(),
  39. }
  40. }
  41. }
  42.  
  43. impl Display for SArg {
  44. fn fmt(&self, f: &mut Formatter) -> fmt::Result {
  45. let mut astr = "".to_string();
  46. let mut sstr = "".to_string();
  47. for a in &self.items {
  48. match a.tp {
  49. ARG => astr.push_str(&format!(r#"{}="{}" "#,&a.k,a.v)),
  50. STYLE => sstr.push_str(&format!("{}:{};",&a.k,a.v)),
  51. }
  52. }
  53. if sstr.len() > 0 {
  54. astr.push_str(&format!(r#"style="{}" "#,&sstr));
  55. }
  56. write!(f, "{}",astr)
  57. }
  58. }
  59.  
  60. impl SvgArg for SArg{
  61. fn arg<T:Display+Clone>(&mut self,k:&str,v:T)->&mut Self{
  62. self.items.push(Arg{k:k.to_string(),v:format!("{}",v),tp:ARG});
  63. self
  64. }
  65. fn style<T:Display+Clone>(&mut self,k:&str,v:T)->&mut Self{
  66. self.items.push(Arg{k:k.to_string(),v:format!("{}",v),tp:STYLE});
  67. self
  68. }
  69. }
  70.  
  71. pub trait SvgArg {
  72. fn arg<T:Display+Clone>(&mut self,k:&str,v:T)->&mut Self;
  73. fn style<T:Display+Clone>(&mut self, k:&str,v:T)->&mut Self;
  74.  
  75. }
Add Comment
Please, Sign In to add comment