rs-graph 0.6.3

A library for graph algorithms and combinatorial optimization
Documentation
// Copyright (c) 2016, 2017 Frank Fischer <frank-fischer@shadow-soft.de>
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see  <http://www.gnu.org/licenses/>

//! Visualizing graphs.

use Graph;

#[derive(Clone, Copy, Default, Debug)]
pub struct Point {
    pub x: f64,
    pub y: f64,
}

#[derive(Clone, Default, Debug)]
pub struct NodeInfo {
    pub name: Option<String>,
    pub point: Option<Point>,
    pub label: Option<usize>,
    pub value: Option<String>,
}

impl NodeInfo {
    pub fn update(&mut self, info: NodeInfo) {
        if info.name.is_some() {
            self.name = info.name;
        }
        if info.point.is_some() {
            self.point = info.point;
        }
        if info.label.is_some() {
            self.label = info.label;
        }
        if info.value.is_some() {
            self.value = info.value;
        }
    }
}

#[derive(Clone, Default, Debug)]
pub struct EdgeInfo {
    pub label: Option<usize>,
    pub value: Option<String>,
}

impl EdgeInfo {
    pub fn update(&mut self, info: EdgeInfo) {
        if info.label.is_some() {
            self.label = info.label;
        }
        if info.value.is_some() {
            self.value = info.value;
        }
    }
}

/// Trait for drawing sequences of graphs.
pub trait GraphDrawer<'a, G>
    where G: Graph<'a>
{
    /// Update node attributes.
    fn set_node(&mut self, u: G::Node, info: NodeInfo);

    /// Set the point of a node.
    fn set_node_point(&mut self, u: G::Node, point: Point) {
        self.set_node(u, NodeInfo { point: Some(point), ..Default::default() });
    }

    /// Set the name of a node.
    fn set_node_name(&mut self, u: G::Node, name: String) {
        self.set_node(u, NodeInfo { name: Some(name), ..Default::default() });
    }

    /// Set the label of a node.
    fn set_node_label(&mut self, u: G::Node, label: usize) {
        self.set_node(u, NodeInfo { label: Some(label), ..Default::default() });
    }

    /// Set the value of a node.
    fn set_node_value(&mut self, u: G::Node, value: String) {
        self.set_node(u, NodeInfo { value: Some(value), ..Default::default() });
    }

    /// Update edge attributes.
    fn set_edge(&mut self, a: G::Edge, info: EdgeInfo);

    /// Set the label of an edge.
    fn set_edge_label(&mut self, u: G::Edge, label: usize) {
        self.set_edge(u, EdgeInfo { label: Some(label), ..Default::default() });
    }

    /// Set the value of an edge.
    fn set_edge_value(&mut self, e: G::Edge, value: String) {
        self.set_edge(e, EdgeInfo { value: Some(value), ..Default::default() });
    }

    /// Add drawing with current attributes.
    fn add_drawing(&mut self);
}