pub struct Attributes<'a> { /* private fields */ }Expand description
The attributes of one node or edge, name to value, in the order they were put: the map that the
hooks of DotWriter::node_attributes and
DotWriter::edge_attributes fill.
The attributes, such as color or shape, are those of DOT (the language of Graphviz). Java’s
AttributeHook is a closure Fn(u32, &mut Attributes<'a>) here, called with the node ID or edge
index and this map. The writer passes it holding the label from
node_label or edge_label as the
attribute label, if there is one, and nothing else; the hook can add attributes, and replace or
remove the label. The writer writes the attributes in the order they were put and escapes their
values; a name that is not an identifier or is a DOT keyword makes
write panic with an invalid argument message. The writer reuses the
map for every node and edge, so a hook cannot keep it: the borrow ends with the call.
use graphity_core::GraphBuilder;
use graphity_dot::DotWriter;
let mut b = GraphBuilder::new();
b.add_nodes(2);
b.add_edge(0, 1);
let graph = b.build();
let dot = DotWriter::of(&graph)
.node_label(|node| Some(format!("v{node}").into()))
.node_attributes(|node, attributes| {
if node == 0 {
attributes.insert("shape", "box");
attributes.insert("label", "entry"); // replaces the label, keeps its place
}
})
.write_to_string();
assert!(dot.contains(" 0 [label=\"entry\", shape=\"box\"];\n"));Names and values are Cows: a string literal or a &str into the caller’s data, which lives
for the writer’s lifetime 'a, is kept without a copy, and a computed String is moved in.
Putting a name that is present replaces its value in place, as Java’s LinkedHashMap.put does;
removing an entry keeps the order of the others. Lookups search the entries linearly: an element
carries a handful of attributes, for which a vector of pairs is faster than hashing.
Implementations§
Source§impl<'a> Attributes<'a>
impl<'a> Attributes<'a>
Sourcepub fn insert(
&mut self,
name: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
) -> Option<Cow<'a, str>>
pub fn insert( &mut self, name: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>, ) -> Option<Cow<'a, str>>
Sets the value of name and returns the previous one - Java’s Map.put. A new name is appended;
a present one keeps its place.
Time: O(len).
Sourcepub fn get(&self, name: &str) -> Option<&str>
pub fn get(&self, name: &str) -> Option<&str>
Returns the value of name, or None if it is absent.
Time: O(len).
Sourcepub fn contains_key(&self, name: &str) -> bool
pub fn contains_key(&self, name: &str) -> bool
Returns whether name is present.
Time: O(len).
Trait Implementations§
Source§impl<'a> Clone for Attributes<'a>
impl<'a> Clone for Attributes<'a>
Source§fn clone(&self) -> Attributes<'a>
fn clone(&self) -> Attributes<'a>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more