Skip to content

Commit

Permalink
dot-dsl - added a solution
Browse files Browse the repository at this point in the history
  • Loading branch information
m4drat committed Jan 8, 2024
1 parent 1358f32 commit efd39ef
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ A collection of resources and code snippets for learning rust.
1. [Accumulate](exercism-solutions/rust/accumulate/src/lib.rs)
2. [Custom Set](exercism-solutions/rust/custom-set/src/lib.rs)
3. [Dominoes](exercism-solutions/rust/dominoes/src/lib.rs)
4. [Dot DSL](exercism-solutions/rust/dot-dsl/src/lib.rs)
4. [Dot DSL](exercism-solutions/rust/dot-dsl/src/lib.rs) ✔️
5. [Fizzy](exercism-solutions/rust/fizzy/src/lib.rs) ✔️
6. [Grep](exercism-solutions/rust/grep/src/lib.rs)
7. [Hamming](exercism-solutions/rust/hamming/src/lib.rs) ✔️
Expand Down
106 changes: 104 additions & 2 deletions exercism-solutions/rust/dot-dsl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,111 @@
pub mod graph {
pub struct Graph;
use std::collections::HashMap;

use self::graph_items::edge::Edge;
use self::graph_items::node::Node;

#[derive(Clone)]
pub struct Graph {
pub nodes: Vec<Node>,
pub edges: Vec<Edge>,
pub attrs: HashMap<String, String>,
}

impl Graph {
pub fn new() -> Self {
todo!("Construct a new Graph struct.");
Self {
nodes: Vec::default(),
edges: Vec::default(),
attrs: HashMap::default(),
}
}

pub fn with_nodes(mut self, nodes: &[Node]) -> Self {
self.nodes.append(&mut nodes.to_owned());
self
}

pub fn with_edges(mut self, edges: &[Edge]) -> Self {
self.edges.append(&mut edges.to_owned());
self
}

pub fn with_attrs(mut self, attrs: &[(&'static str, &'static str)]) -> Self {
self.attrs
.extend(attrs.iter().map(|(k, v)| (k.to_string(), v.to_string())));
self
}

pub fn node(&self, node_name: &str) -> Option<&Node> {
self.nodes.iter().find(|node| node.name == node_name)
}
}

impl Default for Graph {
fn default() -> Self {
Self::new()
}
}

pub mod graph_items {
pub mod edge {
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq)]
pub struct Edge {
pub attrs: HashMap<String, String>,
pub src: String,
pub dst: String,
}

impl Edge {
pub fn new(src: &str, dst: &str) -> Self {
Self {
attrs: HashMap::default(),
src: src.to_string(),
dst: dst.to_string(),
}
}

pub fn with_attrs(mut self, attrs: &[(&'static str, &'static str)]) -> Self {
self.attrs
.extend(attrs.iter().map(|(k, v)| (k.to_string(), v.to_string())));
self
}

pub fn attr(&self, attr: &str) -> Option<&str> {
self.attrs.get(&attr.to_string()).map(|x| x.as_str())
}
}
}

pub mod node {
use std::collections::HashMap;

#[derive(Clone, Debug, PartialEq)]
pub struct Node {
pub attrs: HashMap<String, String>,
pub name: String,
}

impl Node {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
attrs: HashMap::default(),
}
}

pub fn with_attrs(mut self, attrs: &[(&'static str, &'static str)]) -> Self {
self.attrs
.extend(attrs.iter().map(|(k, v)| (k.to_string(), v.to_string())));
self
}

pub fn attr(&self, attr_name: &str) -> Option<&str> {
self.attrs.get(&attr_name.to_string()).map(|x| x.as_str())
}
}
}
}
}
2 changes: 2 additions & 0 deletions learning-rust.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
"./exercism-solutions/rust/hamming/Cargo.toml",
"./exercism-solutions/rust/allergies/Cargo.toml",
"./exercism-solutions/rust/fizzy/Cargo.toml",
"./exercism-solutions/rust/dot-dsl/Cargo.toml",
"./exercism-solutions/rust/macros/Cargo.toml",
"./rust-book/book/Cargo.toml",
"./rust-book/minigrep/Cargo.toml",
"./rust-book/web-server/Cargo.toml",
Expand Down

0 comments on commit efd39ef

Please sign in to comment.