use std::fmt;
use {
AttributeId,
AttributeValue,
Name,
NameRef,
SvgId,
WriteBuffer,
WriteOptions,
WriteToString,
};
pub type AttributeName = Name<AttributeId>;
pub type AttributeNameRef<'a> = NameRef<'a, AttributeId>;
impl SvgId for AttributeId {
fn name(&self) -> &str { self.name() }
}
#[derive(PartialEq,Clone,Debug)]
pub struct Attribute {
pub name: AttributeName,
pub value: AttributeValue,
pub visible: bool,
}
macro_rules! impl_is_type {
($name:ident, $t:ident) => (
#[allow(missing_docs)]
pub fn $name(&self) -> bool {
match self.value {
AttributeValue::$t(_) => true,
_ => false,
}
}
)
}
impl Attribute {
pub fn new<'a, N, T>(name: N, value: T) -> Attribute
where AttributeNameRef<'a>: From<N>, AttributeValue: From<T>
{
let n = AttributeNameRef::from(name);
Attribute {
name: AttributeName::from(n),
value: AttributeValue::from(value),
visible: true,
}
}
pub fn id(&self) -> Option<AttributeId> {
match self.name {
Name::Id(id) => Some(id),
Name::Name(_) => None,
}
}
pub fn has_id(&self, id: AttributeId) -> bool {
match self.name {
Name::Id(id2) => id2 == id,
Name::Name(_) => false,
}
}
pub fn is_svg(&self) -> bool {
match self.name {
Name::Id(_) => true,
Name::Name(_) => false,
}
}
pub fn default(id: AttributeId) -> Option<Attribute> {
match AttributeValue::default_value(id) {
Some(v) => Some(Attribute::new(id, v)),
None => None,
}
}
pub fn check_is_default(&self) -> bool {
if let Name::Id(id) = self.name {
match AttributeValue::default_value(id) {
Some(v) => self.value == v,
None => false,
}
} else {
false
}
}
impl_is_type!(is_color, Color);
impl_is_type!(is_length, Length);
impl_is_type!(is_length_list, LengthList);
impl_is_type!(is_link, Link);
impl_is_type!(is_func_link, FuncLink);
impl_is_type!(is_number, Number);
impl_is_type!(is_number_list, NumberList);
impl_is_type!(is_path, Path);
impl_is_type!(is_predef_value, PredefValue);
impl_is_type!(is_string, String);
impl_is_type!(is_transform, Transform);
}
fn write_quote(opt: &WriteOptions, out: &mut Vec<u8>) {
out.push(if opt.use_single_quote { b'\'' } else { b'"' });
}
impl WriteBuffer for Attribute {
fn write_buf_opt(&self, opt: &WriteOptions, buf: &mut Vec<u8>) {
match self.name {
Name::Id(id) => buf.extend_from_slice(id.name().as_bytes()),
Name::Name(ref name) => buf.extend_from_slice(name.as_bytes()),
}
buf.push(b'=');
write_quote(opt, buf);
self.value.write_buf_opt(opt, buf);
write_quote(opt, buf);
}
}
impl_display!(Attribute);