stern4rust/finding/model/implemented_type.rs
1// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
2// Licensed under the MIT License
3// SPDX-License-Identifier: MIT
4
5// A type a file both declares and gives behaviour to -- one of the file's
6// subjects.
7//
8// The line is the declaration's, not the impl block's, because the declaration
9// is what a reader moves when the file turns out to have two subjects.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct ImplementedType {
12 pub name: String,
13 pub line: usize,
14}
15
16impl ImplementedType {
17 pub fn new(name: &str, line: usize) -> Self {
18 Self {
19 name: name.to_string(),
20 line,
21 }
22 }
23
24 // The file this type would live in on its own: PascalCase to snake_case, so
25 // the correction names a path rather than describing a convention.
26 pub fn suggested_file(&self) -> String {
27 let mut file = String::new();
28 for (index, character) in self.name.chars().enumerate() {
29 if character.is_uppercase() && index > 0 {
30 file.push('_');
31 }
32 file.extend(character.to_lowercase());
33 }
34 file.push_str(".rs");
35 file
36 }
37}