vis/src/render/svglib/circle.rs
2025-01-25 13:35:08 +01:00

79 lines
1.7 KiB
Rust

use crate::render::svglib::{att, att_to_string, Att, SvgElement, ElementType, Shape, Value};
use std::fmt::Display;
pub fn circle() -> Circle {
Circle::default()
}
#[derive(Debug, Default, Clone)]
pub struct Circle(Vec<Att>);
impl Circle {
pub fn id<V: Into<Value>>(mut self, id: V) -> Self {
self.0.push(att("id", id));
self
}
pub fn cx<V: Into<Value>>(mut self, cx: V) -> Self {
self.0.push(att("cx", cx));
self
}
pub fn cy<V: Into<Value>>(mut self, cy: V) -> Self {
self.0.push(att("cy", cy));
self
}
pub fn r<V: Into<Value>>(mut self, r: V) -> Self {
self.0.push(att("r", r));
self
}
}
impl Shape for Circle {
fn fill<V: Into<Value>>(mut self, value: V) -> Self {
self.0.push(att("fill", value));
self
}
fn stroke<V: Into<Value>>(mut self, value: V) -> Self {
self.0.push(att("stroke", value));
self
}
fn transform<V: Into<Value>>(mut self, value: V) -> Self {
self.0.push(att("transform", value));
self
}
}
impl Display for Circle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
r#"<circle{} />"#,
self.0.iter().map(att_to_string).collect::<String>()
)
}
}
impl SvgElement for Circle {
fn get_type(&self) -> ElementType {
ElementType::Circle
}
fn atts(&self) -> &[Att] {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_circle() {
let circle = circle().cx("1em").cy(0).r("10").id("c");
assert_eq!(
r#"<circle cx="1em" cy="0" r="10" id="c" />"#,
circle.to_string()
)
}
}