From 4e774177954e5b2894d6ecbee0f53ff7e239a857 Mon Sep 17 00:00:00 2001 From: Jerome Humbert Date: Sat, 11 May 2024 20:45:07 +0100 Subject: [PATCH] new() -> Self --- bin/weldr/src/error.rs | 2 +- bin/weldr/src/gltf.rs | 12 ++++++------ bin/weldr/src/weldr.rs | 16 ++++++++-------- lib/src/error.rs | 8 ++++---- lib/src/lib.rs | 12 ++++++------ lib/tests/parse.rs | 4 ++-- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/bin/weldr/src/error.rs b/bin/weldr/src/error.rs index 1dd823e..dcbb83a 100644 --- a/bin/weldr/src/error.rs +++ b/bin/weldr/src/error.rs @@ -127,7 +127,7 @@ pub struct Utf8Error { impl Utf8Error { pub fn new(context: &str) -> Self { - Utf8Error { + Self { context: context.to_string(), } } diff --git a/bin/weldr/src/gltf.rs b/bin/weldr/src/gltf.rs index ec1e7df..837259a 100644 --- a/bin/weldr/src/gltf.rs +++ b/bin/weldr/src/gltf.rs @@ -216,8 +216,8 @@ pub struct Mesh { } impl Gltf { - pub fn new(asset: Asset) -> Gltf { - Gltf { + pub fn new(asset: Asset) -> Self { + Self { asset, nodes: vec![], scenes: vec![], @@ -238,8 +238,8 @@ impl Gltf { } impl Asset { - pub fn new(version: &str) -> Asset { - Asset { + pub fn new(version: &str) -> Self { + Self { version: version.to_string(), min_version: None, generator: None, @@ -270,8 +270,8 @@ impl Asset { } impl Buffer { - pub fn new(byte_length: u32) -> Buffer { - Buffer { + pub fn new(byte_length: u32) -> Self { + Self { name: None, byte_length, uri: None, diff --git a/bin/weldr/src/weldr.rs b/bin/weldr/src/weldr.rs index 6bc4df2..75dfa0d 100644 --- a/bin/weldr/src/weldr.rs +++ b/bin/weldr/src/weldr.rs @@ -59,8 +59,8 @@ struct SimpleLogger { static mut LOGGER: SimpleLogger = SimpleLogger::new(); impl SimpleLogger { - const fn new() -> SimpleLogger { - SimpleLogger { + const fn new() -> Self { + Self { log_level: log::LevelFilter::Info, color_enabled: false, emoji_enabled: false, @@ -183,11 +183,11 @@ struct DiskResolver { } impl DiskResolver { - fn new() -> DiskResolver { - DiskResolver { base_paths: vec![] } + fn new() -> Self { + Self { base_paths: vec![] } } - fn new_from_catalog>(catalog_path: P) -> Result { + fn new_from_catalog>(catalog_path: P) -> Result { let catalog_path = std::fs::canonicalize(catalog_path) .map_err(|e| Error::NotFound(format!("catalog path not found ({})", e)))?; let base_paths = vec![ @@ -195,7 +195,7 @@ impl DiskResolver { catalog_path.join("parts"), catalog_path.join("parts").join("s"), ]; - Ok(DiskResolver { base_paths }) + Ok(Self { base_paths }) } fn add_path>(&mut self, path: P) -> Result<(), Error> { @@ -280,8 +280,8 @@ struct GeometryCache { } impl GeometryCache { - fn new() -> GeometryCache { - GeometryCache { + fn new() -> Self { + Self { vertices: vec![], vertex_map: HashMap::new(), line_indices: vec![], diff --git a/lib/src/error.rs b/lib/src/error.rs index 0625d9b..59f8d43 100644 --- a/lib/src/error.rs +++ b/lib/src/error.rs @@ -36,7 +36,7 @@ pub struct ResolveError { impl ParseError { /// Create a [`ParseError`] that stems from an arbitrary error of an underlying parser. pub fn new(filename: &str, err: impl Into>) -> Self { - ParseError { + Self { filename: filename.to_string(), parse_error: Some(err.into()), } @@ -45,7 +45,7 @@ impl ParseError { /// Create a [`ParseError`] that stems from a [`nom`] parsing error, capturing the [`nom::error::ErrorKind`] /// from the underlying parser which failed. pub fn new_from_nom(filename: &str, err: &nom::Err>) -> Self { - ParseError { + Self { filename: filename.to_string(), parse_error: match err { nom::Err::Incomplete(_) => None, @@ -65,7 +65,7 @@ impl ParseError { impl ResolveError { /// Create a [`ResolveError`] that stems from an arbitrary error of an underlying resolution error. pub fn new(filename: String, err: impl Into>) -> Self { - ResolveError { + Self { filename, resolve_error: Some(err.into()), } @@ -73,7 +73,7 @@ impl ResolveError { /// Create a [`ResolveError`] without any underlying error. pub fn new_raw(filename: &str) -> Self { - ResolveError { + Self { filename: filename.to_string(), resolve_error: None, } diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 33b86df..bbf6074 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -90,8 +90,8 @@ pub struct Color { impl Color { /// Construct a new color instance from individual RGB components. - pub fn new(red: u8, green: u8, blue: u8) -> Color { - Color { red, green, blue } + pub fn new(red: u8, green: u8, blue: u8) -> Self { + Self { red, green, blue } } } @@ -393,8 +393,8 @@ pub struct CommentCmd { } impl CommentCmd { - pub fn new(text: &str) -> CommentCmd { - CommentCmd { + pub fn new(text: &str) -> Self { + Self { text: text.to_string(), } } @@ -457,8 +457,8 @@ pub struct SourceMap { impl SourceMap { /// Construct a new empty source map. - pub fn new() -> SourceMap { - SourceMap { + pub fn new() -> Self { + Self { source_files: HashMap::new(), } } diff --git a/lib/tests/parse.rs b/lib/tests/parse.rs index 3596f1a..6d93555 100644 --- a/lib/tests/parse.rs +++ b/lib/tests/parse.rs @@ -20,8 +20,8 @@ struct MemoryResolver { } impl MemoryResolver { - fn new() -> MemoryResolver { - MemoryResolver { + fn new() -> Self { + Self { file_map: HashMap::new(), } }