Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::{Display,Formatter};
- use std::fmt;
- #[derive(Copy,Clone)]
- enum AType {
- ARG,
- STYLE,
- }
- use self::AType::*;
- #[derive(Clone)]
- struct Arg {
- k:String,
- v:String,
- tp:AType,
- }
- #[derive(Clone)]
- pub struct SArg{
- items :Vec<Arg>,
- }
- /// # example
- ///
- /// ```
- /// let a = SArg::new().arg("f",7).style("p","rt");
- /// assert_eq!(r#"f="6" style="p:rt;""#,&format!("{}",a));
- /// ```
- impl SArg {
- pub fn new()->SArg{
- SArg{
- items:Vec::new(),
- }
- }
- }
- impl Display for SArg {
- fn fmt(&self, f: &mut Formatter) -> fmt::Result {
- let mut astr = "".to_string();
- let mut sstr = "".to_string();
- for a in &self.items {
- match a.tp {
- ARG => astr.push_str(&format!(r#"{}="{}" "#,&a.k,a.v)),
- STYLE => sstr.push_str(&format!("{}:{};",&a.k,a.v)),
- }
- }
- if sstr.len() > 0 {
- astr.push_str(&format!(r#"style="{}" "#,&sstr));
- }
- write!(f, "{}",astr)
- }
- }
- impl SvgArg for SArg{
- fn arg<T:Display+Clone>(&mut self,k:&str,v:T)->&mut Self{
- self.items.push(Arg{k:k.to_string(),v:format!("{}",v),tp:ARG});
- self
- }
- fn style<T:Display+Clone>(&mut self,k:&str,v:T)->&mut Self{
- self.items.push(Arg{k:k.to_string(),v:format!("{}",v),tp:STYLE});
- self
- }
- }
- pub trait SvgArg {
- fn arg<T:Display+Clone>(&mut self,k:&str,v:T)->&mut Self;
- fn style<T:Display+Clone>(&mut self, k:&str,v:T)->&mut Self;
- }
Add Comment
Please, Sign In to add comment