Replace all uses and implementations of new() with default()
This commit is contained in:
parent
315956e20d
commit
6b739d93c2
15 changed files with 100 additions and 84 deletions
|
|
@ -2,7 +2,7 @@ use std::path::PathBuf;
|
|||
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct Arguments {
|
||||
pub hostname: String,
|
||||
pub port: u16,
|
||||
|
|
@ -14,19 +14,21 @@ impl Arguments {
|
|||
format!("{}:{}", self.hostname, self.port)
|
||||
}
|
||||
|
||||
pub fn new() -> Arguments {
|
||||
#[must_use]
|
||||
pub fn parse(&self) -> Arguments {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
parse(self, &args)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Arguments {
|
||||
fn default() -> Arguments {
|
||||
Arguments {
|
||||
hostname: String::from("0.0.0.0"),
|
||||
port: 0,
|
||||
graph_path: PathBuf::from("./static/graph.toml"),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn parse(&self) -> Arguments {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
parse(self, &args)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(defaults: &Arguments, args: &[String]) -> Arguments {
|
||||
|
|
@ -67,7 +69,7 @@ mod tests {
|
|||
let args = Arguments {
|
||||
hostname: String::from("localhost"),
|
||||
port: 3007,
|
||||
graph_path: PathBuf::new(),
|
||||
graph_path: PathBuf::default(),
|
||||
};
|
||||
|
||||
assert_eq!(args.make_address(), "localhost:3007");
|
||||
|
|
@ -75,7 +77,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn hostname() {
|
||||
let defaults = Arguments::new();
|
||||
let defaults = Arguments::default();
|
||||
|
||||
let payload = String::from("olUCu7vWcUAsumv2xpj2Z55EDheWLTEu");
|
||||
let args =
|
||||
|
|
@ -85,7 +87,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn port() {
|
||||
let defaults = Arguments::new();
|
||||
let defaults = Arguments::default();
|
||||
|
||||
let payload = 3901;
|
||||
let args = parse(&defaults, &[String::from("-p"), payload.to_string()]);
|
||||
|
|
@ -94,7 +96,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn graph_path() {
|
||||
let defaults = Arguments::new();
|
||||
let defaults = Arguments::default();
|
||||
|
||||
let payload = PathBuf::from("/tmp/");
|
||||
let args = parse(
|
||||
|
|
@ -106,7 +108,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn empty() {
|
||||
let defaults = Arguments::new();
|
||||
let defaults = Arguments::default();
|
||||
|
||||
let args = parse(&defaults, &[]);
|
||||
assert_eq!(defaults, args);
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ const LEXMAP: LexMap = &[
|
|||
];
|
||||
|
||||
fn lex(text: &str, map: LexMap, config: &Config) -> Vec<Token> {
|
||||
let mut tokens: Vec<Token> = Vec::new();
|
||||
let mut tokens: Vec<Token> = Vec::default();
|
||||
let mut state = state::State::default();
|
||||
|
||||
let segments = segment::segment(text);
|
||||
|
|
|
|||
|
|
@ -138,7 +138,7 @@ impl Lexeme {
|
|||
let mut iterator = raw_strings.iter().peekable();
|
||||
|
||||
while let Some(raw) = iterator.next() {
|
||||
let mut next = String::new();
|
||||
let mut next = String::default();
|
||||
let mut last = false;
|
||||
if let Some(peeked) = iterator.peek() {
|
||||
next.clone_from(*peeked);
|
||||
|
|
|
|||
|
|
@ -33,20 +33,20 @@ pub struct AnchorBuffer {
|
|||
impl AnchorBuffer {
|
||||
pub fn clear(&mut self) {
|
||||
self.candidate = Anchor::default();
|
||||
self.text = String::new();
|
||||
self.destination = String::new();
|
||||
self.text = String::default();
|
||||
self.destination = String::default();
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for AnchorBuffer {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
let display_text = if self.text.is_empty() {
|
||||
String::new()
|
||||
String::default()
|
||||
} else {
|
||||
format!("text: {:?}", self.text)
|
||||
};
|
||||
let display_destination = if self.destination.is_empty() {
|
||||
String::new()
|
||||
String::default()
|
||||
} else {
|
||||
format!(", dest: {:?}", self.destination)
|
||||
};
|
||||
|
|
@ -69,13 +69,13 @@ impl Default for State {
|
|||
inline: Inline::None,
|
||||
block: Block::None,
|
||||
},
|
||||
dom_ids: HashMap::new(),
|
||||
dom_ids: HashMap::default(),
|
||||
switches: Switches { oblique: false },
|
||||
buffers: Buffers {
|
||||
anchor: AnchorBuffer {
|
||||
candidate: Anchor::default(),
|
||||
text: String::new(),
|
||||
destination: String::new(),
|
||||
text: String::default(),
|
||||
destination: String::default(),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ impl std::fmt::Display for Anchor {
|
|||
None => "<unknown>",
|
||||
};
|
||||
|
||||
let mut tail = String::new();
|
||||
let mut tail = String::default();
|
||||
|
||||
if self.leading {
|
||||
tail.push_str(" [Leading]");
|
||||
|
|
|
|||
|
|
@ -122,7 +122,7 @@ impl std::fmt::Display for Header {
|
|||
|
||||
let display_dom_id = match self.dom_id {
|
||||
Some(ref dom_id) => format!(" DOM ID {dom_id}"),
|
||||
None => String::new(),
|
||||
None => String::default(),
|
||||
};
|
||||
|
||||
write!(
|
||||
|
|
@ -193,7 +193,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn make_id() {
|
||||
let mut map: HashMap<String, Vec<String>> = HashMap::new();
|
||||
let mut map: HashMap<String, Vec<String>> = HashMap::default();
|
||||
let id = Header::make_id(
|
||||
&Config::default(),
|
||||
&Lexeme::new("##", "Title"),
|
||||
|
|
@ -204,33 +204,33 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn ascii_ids_set() {
|
||||
let mut config = Config::new();
|
||||
let mut config = Config::default();
|
||||
config.ascii_dom_ids = true;
|
||||
|
||||
let id = Header::make_id(
|
||||
&config,
|
||||
&Lexeme::new("##", "駄目!"),
|
||||
&mut HashMap::new(),
|
||||
&mut HashMap::default(),
|
||||
);
|
||||
assert_eq!(id, "h");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ascii_ids_unset() {
|
||||
let mut config = Config::new();
|
||||
let mut config = Config::default();
|
||||
config.ascii_dom_ids = false;
|
||||
|
||||
let id = Header::make_id(
|
||||
&config,
|
||||
&Lexeme::new("##", "駄目!"),
|
||||
&mut HashMap::new(),
|
||||
&mut HashMap::default(),
|
||||
);
|
||||
assert_eq!(id, "駄目!");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_deduplication() {
|
||||
let mut map: HashMap<String, Vec<String>> = HashMap::new();
|
||||
let mut map: HashMap<String, Vec<String>> = HashMap::default();
|
||||
let config = Config::default();
|
||||
let id =
|
||||
Header::make_id(&config, &Lexeme::new("##", "UVrcCUjoQ"), &mut map);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use crate::{
|
|||
};
|
||||
|
||||
pub fn populate_graph() -> Graph {
|
||||
let args = Arguments::new().parse();
|
||||
let args = Arguments::default().parse();
|
||||
let toml_source = match std::fs::read_to_string(args.graph_path) {
|
||||
Ok(s) => s,
|
||||
Err(e) => format!("Error: {e}"),
|
||||
|
|
@ -32,7 +32,7 @@ fn modulate_graph(graph: &Graph) -> Graph {
|
|||
}
|
||||
|
||||
fn modulate_nodes(old_nodes: &HashMap<String, Node>) -> HashMap<String, Node> {
|
||||
let mut nodes: HashMap<String, Node> = HashMap::new();
|
||||
let mut nodes: HashMap<String, Node> = HashMap::default();
|
||||
|
||||
for (key, node) in old_nodes {
|
||||
let connections = node.connections.clone().unwrap_or_default();
|
||||
|
|
@ -61,7 +61,7 @@ fn modulate_nodes(old_nodes: &HashMap<String, Node>) -> HashMap<String, Node> {
|
|||
new_edges.push(Edge {
|
||||
from: key.clone(),
|
||||
to: link.clone(),
|
||||
anchor: String::new(),
|
||||
anchor: String::default(),
|
||||
detached: !old_nodes.contains_key(link),
|
||||
});
|
||||
}
|
||||
|
|
@ -119,7 +119,7 @@ pub fn deserialize_graph(in_format: &Format, serial: &str) -> Graph {
|
|||
|
||||
// Construct a HashMap with incoming connections (reversed edges)
|
||||
fn make_incoming(nodes: &HashMap<String, Node>) -> HashMap<String, Vec<Edge>> {
|
||||
let mut incoming: HashMap<String, Vec<Edge>> = HashMap::new();
|
||||
let mut incoming: HashMap<String, Vec<Edge>> = HashMap::default();
|
||||
|
||||
for node in nodes.clone().into_values() {
|
||||
let empty_vec: Vec<Edge> = vec![];
|
||||
|
|
@ -137,7 +137,7 @@ fn make_incoming(nodes: &HashMap<String, Node>) -> HashMap<String, Vec<Edge>> {
|
|||
fn map_lowercase_keys(
|
||||
source_map: &HashMap<String, Node>,
|
||||
) -> HashMap<String, String> {
|
||||
let mut out_map: HashMap<String, String> = HashMap::new();
|
||||
let mut out_map: HashMap<String, String> = HashMap::default();
|
||||
let keys = source_map.keys();
|
||||
for key in keys {
|
||||
out_map.insert(key.clone().to_lowercase(), key.clone());
|
||||
|
|
@ -183,12 +183,12 @@ mod tests {
|
|||
let mut node = Node::new(None);
|
||||
node.connections = Some(vec![Edge {
|
||||
anchor: String::from("SomeAnchor"),
|
||||
from: String::new(),
|
||||
to: String::new(),
|
||||
from: String::default(),
|
||||
to: String::default(),
|
||||
detached: false,
|
||||
}]);
|
||||
|
||||
let mut map: HashMap<String, Node> = HashMap::new();
|
||||
let mut map: HashMap<String, Node> = HashMap::default();
|
||||
map.insert(String::from("SomeNode"), node);
|
||||
|
||||
let modulated_map = modulate_nodes(&map);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue