Add and resolve an obscene amount of lints
This commit is contained in:
parent
b306f04664
commit
0d0627ba8f
5 changed files with 264 additions and 56 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use crate::types::*;
|
||||
use crate::types::{Graph, Node, Edge};
|
||||
|
||||
pub fn populate_graph() -> Graph {
|
||||
|
||||
|
|
@ -8,22 +8,22 @@ pub fn populate_graph() -> Graph {
|
|||
Ok(s) => s,
|
||||
Err(e) => format!("Error: {e}"),
|
||||
};
|
||||
let graph = deserialize_graph(Format::Toml, &toml_source);
|
||||
let graph = deserialize_graph(&Format::Toml, &toml_source);
|
||||
|
||||
let nodes = modulate_nodes(graph.nodes.clone());
|
||||
let nodes = modulate_nodes(&graph.nodes);
|
||||
|
||||
Graph {
|
||||
nodes: nodes.clone(),
|
||||
incoming: make_incoming(nodes.clone()),
|
||||
incoming: make_incoming(&nodes),
|
||||
..graph
|
||||
}
|
||||
}
|
||||
|
||||
fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
|
||||
fn modulate_nodes(old_nodes: &HashMap<String, Node>) -> HashMap<String, Node> {
|
||||
|
||||
let mut nodes: HashMap<String, Node> = HashMap::new();
|
||||
|
||||
for (key, node) in old_nodes.iter() {
|
||||
for (key, node) in old_nodes {
|
||||
|
||||
let connections = node.connections.clone().unwrap_or_default();
|
||||
let mut new_edges = connections.clone();
|
||||
|
|
@ -33,8 +33,8 @@ fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
|
|||
let mut new_edge = edge.clone();
|
||||
|
||||
// Populate empty "from" IDs in edges with node's ID
|
||||
if edge.from == "" {
|
||||
new_edge.from = key.to_string();
|
||||
if edge.from.is_empty() {
|
||||
new_edge.from.clone_from(key);
|
||||
}
|
||||
|
||||
// Flag detached edges
|
||||
|
|
@ -42,17 +42,17 @@ fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
|
|||
new_edge.detached = true;
|
||||
}
|
||||
|
||||
new_edges[i] = new_edge;
|
||||
if let Some(e) = new_edges.get_mut(i) { *e = new_edge; }
|
||||
}
|
||||
|
||||
// Create connections for each link
|
||||
for link in node.links.iter() {
|
||||
for link in &node.links {
|
||||
new_edges.push(Edge {
|
||||
from: key.to_string(),
|
||||
to: link.to_string(),
|
||||
from: key.clone(),
|
||||
to: link.clone(),
|
||||
anchor: String::new(),
|
||||
detached: !old_nodes.contains_key(link),
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
// Populate empty titles with IDs
|
||||
|
|
@ -69,22 +69,22 @@ fn modulate_nodes(old_nodes: HashMap<String, Node>) -> HashMap<String, Node> {
|
|||
..node.clone()
|
||||
};
|
||||
|
||||
nodes.insert(key.to_string(), new_node);
|
||||
nodes.insert(key.clone(), new_node);
|
||||
}
|
||||
|
||||
nodes
|
||||
}
|
||||
|
||||
// Construct a HashMap with incoming connections (reversed edges)
|
||||
fn make_incoming(nodes: HashMap<String, Node>) -> HashMap<String, Vec<Edge>> {
|
||||
fn make_incoming(nodes: &HashMap<String, Node>) -> HashMap<String, Vec<Edge>> {
|
||||
|
||||
let mut incoming: HashMap<String, Vec<Edge>> = HashMap::new();
|
||||
for node in nodes.clone().into_values() {
|
||||
|
||||
let empty_vec: Vec<Edge> = vec![];
|
||||
for edge in node.connections.clone().unwrap_or_default().iter() {
|
||||
for edge in &node.connections.clone().unwrap_or_default() {
|
||||
let mut edges = incoming.get(&edge.to.clone()).unwrap_or(&empty_vec).clone();
|
||||
edges.extend_from_slice(&[edge.clone()]);
|
||||
edges.extend_from_slice(std::slice::from_ref(edge));
|
||||
incoming.insert(edge.to.clone(), edges.clone());
|
||||
}
|
||||
}
|
||||
|
|
@ -97,9 +97,9 @@ pub enum Format {
|
|||
Json
|
||||
}
|
||||
|
||||
pub fn serialize_graph(out_format: Format, graph: &Graph) -> String {
|
||||
pub fn serialize_graph(out_format: &Format, graph: &Graph) -> String {
|
||||
|
||||
match out_format {
|
||||
match *out_format {
|
||||
Format::Toml => {
|
||||
match toml::to_string(graph) {
|
||||
Ok(s) => s,
|
||||
|
|
@ -115,14 +115,14 @@ pub fn serialize_graph(out_format: Format, graph: &Graph) -> String {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn deserialize_graph(in_format: Format, serial: &String) -> Graph {
|
||||
pub fn deserialize_graph(in_format: &Format, serial: &str) -> Graph {
|
||||
|
||||
match in_format {
|
||||
Format::Toml => { match toml::from_str(&serial) {
|
||||
match *in_format {
|
||||
Format::Toml => { match toml::from_str(serial) {
|
||||
Ok(g) => g,
|
||||
Err(error) => Graph::new(Some(error.to_string()))
|
||||
}},
|
||||
Format::Json => { match serde_json::from_str(&serial) {
|
||||
Format::Json => { match serde_json::from_str(serial) {
|
||||
Ok(g) => g,
|
||||
Err(error) => Graph::new(Some(error.to_string()))
|
||||
}}
|
||||
|
|
|
|||
55
src/main.rs
55
src/main.rs
|
|
@ -7,11 +7,12 @@ use axum::{
|
|||
Router,
|
||||
};
|
||||
|
||||
mod types;
|
||||
mod formats;
|
||||
use formats::{populate_graph, serialize_graph, Format};
|
||||
use types::Node;
|
||||
|
||||
mod formats;
|
||||
mod types;
|
||||
|
||||
use formats::*;
|
||||
use types::*;
|
||||
static ONSET: std::sync::LazyLock<std::time::Instant> =
|
||||
std::sync::LazyLock::new(std::time::Instant::now);
|
||||
|
||||
|
|
@ -59,18 +60,17 @@ async fn main() {
|
|||
|
||||
fn make_body(
|
||||
name: &str,
|
||||
context: tera::Context,
|
||||
error_code: u16,
|
||||
error_message: &str,
|
||||
) -> String {
|
||||
context: &tera::Context,
|
||||
error_message: Option<&str>,
|
||||
) -> (String, u16) {
|
||||
|
||||
let tera = match tera::Tera::new(
|
||||
concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*"),
|
||||
) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
println!("Tera parsing error: {}", e);
|
||||
::std::process::exit(1);
|
||||
println!("Tera parsing error: {e:#?}");
|
||||
panic!("{e}")
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -139,7 +139,7 @@ async fn node_view(Path(id): Path<String>) -> impl IntoResponse {
|
|||
let graph = populate_graph();
|
||||
let nodes = graph.nodes;
|
||||
let empty_node = Node::new(
|
||||
Some(format!("Could not find node with ID {}.", id)),
|
||||
Some(format!("Could not find node with ID {id}.")),
|
||||
);
|
||||
|
||||
let node: &Node = nodes.get(&id).unwrap_or(&empty_node);
|
||||
|
|
@ -205,14 +205,14 @@ async fn query(Form(query): Form<Query>) -> Redirect {
|
|||
|
||||
async fn json_graph() -> impl IntoResponse {
|
||||
let graph = populate_graph();
|
||||
let body = serialize_graph(Format::Json, &graph);
|
||||
let body = serialize_graph(&Format::Json, &graph);
|
||||
|
||||
([(header::CONTENT_TYPE, "application/json")], body)
|
||||
}
|
||||
|
||||
async fn toml_graph() -> impl IntoResponse {
|
||||
let graph = populate_graph();
|
||||
let body = serialize_graph(Format::Toml, &graph);
|
||||
let body = serialize_graph(&Format::Toml, &graph);
|
||||
|
||||
([(header::CONTENT_TYPE, "text/plain")], body)
|
||||
}
|
||||
|
|
@ -242,19 +242,17 @@ fn make_error_body(
|
|||
|
||||
let mut context = tera::Context::new();
|
||||
|
||||
let code = code.unwrap_or(501);
|
||||
let message = &message.unwrap_or("Unknown error");
|
||||
let out_code = code.unwrap_or(500);
|
||||
let out_message = &message.unwrap_or("Unknown error");
|
||||
|
||||
context.insert("title", &StatusCode::from_u16(code)
|
||||
context.insert("title", &StatusCode::from_u16(out_code)
|
||||
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR).to_string());
|
||||
context.insert("message", message);
|
||||
context.insert("status_code", &code.to_string());
|
||||
context.insert("message", out_message);
|
||||
context.insert("status_code", &out_code.to_string());
|
||||
|
||||
make_body("error.html", context, 500, &format!(
|
||||
"Failed to render template for Error {}: {}",
|
||||
code,
|
||||
message,
|
||||
))
|
||||
make_body("error.html", &context, Some(&format!(
|
||||
"Failed to render template for Error {out_code}: {out_message}"
|
||||
))).0
|
||||
}
|
||||
|
||||
fn make_error_response(
|
||||
|
|
@ -262,15 +260,16 @@ fn make_error_response(
|
|||
message: Option<&str>,
|
||||
) -> impl IntoResponse {
|
||||
|
||||
let code = code.unwrap_or(501);
|
||||
let message = &message.unwrap_or("Unknown error");
|
||||
let out_code = code.unwrap_or(500);
|
||||
let out_message = &message.unwrap_or("Unknown error");
|
||||
|
||||
let body = make_error_body(Some(code), Some(message));
|
||||
let body = make_error_body(Some(out_code), Some(out_message));
|
||||
|
||||
(
|
||||
StatusCode::from_u16(code).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
|
||||
StatusCode::from_u16(out_code)
|
||||
.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
|
||||
[(header::CONTENT_TYPE, "text/html")],
|
||||
body.to_string(),
|
||||
body.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Default, PartialEq, Eq, Debug)]
|
||||
pub struct Graph {
|
||||
pub nodes: HashMap<String, Node>,
|
||||
|
|
@ -40,10 +41,7 @@ impl Graph {
|
|||
}
|
||||
|
||||
pub fn get_root(&self) -> Option<Node> {
|
||||
match self.nodes.get(&self.root_node) {
|
||||
Some(n) => Some(n.clone()),
|
||||
None => None,
|
||||
}
|
||||
self.nodes.get(&self.root_node).cloned()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue