1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::{
env,
ffi::OsStr,
fs,
io,
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
/// Reprezentacja pojedynczego węzła ścieżki (buf + string)
#[derive(Debug, Clone)]
pub struct PathNode {
pub buf: PathBuf,
pub str: String,
}
fn normalize_path<P: AsRef<Path>>(p: P) -> String {
p.as_ref().to_string_lossy().trim_start_matches(r"\\?\").replace('\\', "/").replace("/./", "/")
}
impl PathNode {
pub fn new(buf: PathBuf) -> Self {
let str = normalize_path(&buf);
Self { buf, str }
}
}
/// Relacja między katalogiem wykonania a katalogiem docelowym
#[derive(Debug, Clone)]
pub struct AnchoredPathsDatum {
pub execution_dir: PathNode,
pub workspace_dir: PathNode,
}
impl AnchoredPathsDatum {
pub fn new<P: AsRef<Path>>(input: P) -> Result<Self> {
let input = input.as_ref();
let execution_dir_buf = env::current_dir().context("Nie można odczytać katalogu roboczego (CWD)")?;
let workspace_dir_buf = fs::canonicalize(input)
.with_context(|| format!("Nie można ustalić ścieżki '{}'", input.to_string_lossy()))?;
Ok(Self { execution_dir: PathNode::new(execution_dir_buf), workspace_dir: PathNode::new(workspace_dir_buf) })
}
/// Metoda obliczająca względną ścieżkę między execution_dir a workspace_dir.
pub fn to_relative_path(&self) -> String {
match self.workspace_dir.buf.strip_prefix(&self.execution_dir.buf) {
Ok(rel) => {
let s = rel.to_string_lossy().replace('\\', "/");
if s.is_empty() { "./".to_string() } else { format!("./{}/", s) }
}
Err(_) => normalize_path(&self.workspace_dir.buf),
}
}
/// Prywatna metoda pomocnicza: Wyznacza fizyczną lokalizację dla pliku raportu i gwarantuje istnienie folderów nadrzędnych.
#[allow(dead_code)]
fn build_output_file_path(&self, relpath: &str, time_tag: String, suffix: &str) -> io::Result<PathBuf> {
// Wszystkie raporty lądują relatywnie do execution_dir
let base_dir_path = self.execution_dir.buf.join(relpath);
let stem = base_dir_path.file_stem().unwrap_or_else(|| OsStr::new("output")).to_string_lossy();
// Składamy pełną nazwę pliku w jednym kroku
let file_name = format!("{}_{}{}.md", stem, time_tag, suffix);
let raw_file_path = base_dir_path.with_file_name(file_name);
let file_path = PathBuf::from(normalize_path(&raw_file_path));
if let Some(parent) = file_path.parent() {
fs::create_dir_all(parent)?;
}
Ok(file_path)
}
/*
* /// Buduje ścieżkę dla dokumentu SOTC (Structure of the content) - i zapisuje
* pub fn save_sotc_tree(&self, relpath: &str, title: Option<&str>, table_sotc_tree_cli: String) -> io::Result<()> {
* let tt: TagTime = tag_time();
* let file_path = self.build_output_file_path(relpath, tt.0.clone(), "SOTC")?;
* let title_str = title.map_or(String::new(), |t| format!("{} ", t));
* let content = format!(
* "# {}(v:{} [Structure of the content])\n{}",
* title_str,
* tt.0,
* Self::md_plaintext(table_sotc_tree_cli)
* );
* //
* Self::fs_write(&file_path, content)?;
* Ok(())
* }
*
* /// Buduje ścieżkę dla dokumentu COTS (Content of the structure) - i zapisuje
* pub fn save_cots_plot(
* &self,
* relpath: &str,
* title: Option<&str>,
* table_sotc_tree_cli: String,
* table_sotc_tree_raw: TableSotcTreeOutput,
* ) -> io::Result<()> {
* let tt: TagTime = tag_time();
* let file_path = self.build_output_file_path(relpath, tt.0.clone(), "COTS")?;
* let title_str = title.map_or(String::new(), |t| format!("{} ", t));
* let content = format!(
* "# {}(v:{} [Content of the structure])\n{}\n{}",
* title_str,
* tt.0,
* Self::md_plaintext(table_sotc_tree_cli),
* self.md_build(table_sotc_tree_raw)
* );
* Self::fs_write(&file_path, content)?;
* Ok(())
* }
*
* fn md_plaintext(raw: String) -> String { format!("\n```plaintext\n{}\n```\n", Self::md_clean_txt(raw)) }
* fn md_clean_txt(content: impl Into<String>) -> String {
* let raw = content.into();
* strip_ansi_codes(&raw).into_owned().replace('\t', " ")
* }
*
* fn md_build(&self, tabtree: TableSotcTreeOutput) -> String {
* let mut content = String::new();
*
* let (rows_to_show, index_offset) = if let Some(size) = tabtree.trim_size {
* let start = tabtree.trim_page.saturating_sub(1) * size;
* (tabtree.data.rows.iter().skip(start).take(size).collect::<Vec<_>>(), start)
* } else {
* (tabtree.data.rows.iter().collect::<Vec<_>>(), 0)
* };
*
* for (i, row) in rows_to_show.iter().enumerate() {
* let actual_idx = index_offset + i + 1;
* let p_str = &row.path;
*
* if p_str.ends_with('/') {
* continue;
* }
*
* let clean_rel = p_str.strip_prefix("./").unwrap_or(p_str);
* let absolute_path = self.workspace_dir.buf.join(clean_rel);
*
* let ext = absolute_path.extension().unwrap_or_default().to_string_lossy().to_string();
*
* let lang = LangMapper::get_md_lang(&ext);
*
* if LangMapper::is_blacklisted(&ext) {
* content.push_str(&format!(
* "## {:03}: `{}`\n\n> *(Plik binarny/graficzny - pominięto zawartość)*\n\n",
* actual_idx, p_str
* ));
* continue;
* }
*
* match fs::read_to_string(&absolute_path) {
* Ok(file_content) => {
* let safe_content = file_content.replace('\t', " ");
* content.push_str(&format!(
* "## {:03}: `{}`\n\n```{}\n{}\n```\n\n",
* actual_idx, p_str, lang, safe_content
* ));
* }
* Err(_) => {
* content.push_str(&format!(
* "## {:03}: `{}`\n\n> *(Błąd odczytu / plik nie jest UTF-8)*\n\n",
* actual_idx, p_str
* ));
* }
* }
* }
*
* content
* }
*
* fn fs_write(file_path: &PathBuf, content: String) -> std::io::Result<()> {
* fs::write(file_path, content)?;
* println!("📦 Zapisano archiwum kodu do: {}", file_path.display());
* Ok(())
* }
* */
}