scirs2-ndimage 0.4.2

N-dimensional image processing module for SciRS2 (scirs2-ndimage)
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! Core Documentation Types and Structures
//!
//! This module contains all the fundamental data structures used for documentation generation,
//! including site structure, module documentation, function documentation, tutorials, and examples.

use std::collections::HashMap;

/// Result type alias for documentation operations
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

/// Main documentation site structure
#[derive(Debug, Clone, serde::Serialize)]
pub struct DocumentationSite {
    /// Site title
    pub title: String,
    /// Site description
    pub description: String,
    /// Documentation version
    pub version: String,
    /// Base URL for the documentation
    pub base_url: String,
    /// Module documentation entries
    pub modules: Vec<ModuleDoc>,
    /// Tutorial entries
    pub tutorials: Vec<Tutorial>,
    /// Code examples
    pub examples: Vec<Example>,
}

/// Documentation for a single module
#[derive(Debug, Clone)]
pub struct ModuleDoc {
    /// Module name
    pub name: String,
    /// Module description
    pub description: String,
    /// Function documentation for this module
    pub functions: Vec<FunctionDoc>,
    /// Module-level examples
    pub examples: Vec<String>,
}

/// Documentation for a single function
#[derive(Debug, Clone)]
pub struct FunctionDoc {
    /// Function name
    pub name: String,
    /// Function signature
    pub signature: String,
    /// Function description
    pub description: String,
    /// Function parameters
    pub parameters: Vec<Parameter>,
    /// Return type and description
    pub returns: String,
    /// Usage examples
    pub examples: Vec<String>,
    /// Additional notes
    pub notes: Vec<String>,
}

/// Function parameter documentation
#[derive(Debug, Clone)]
pub struct Parameter {
    /// Parameter name
    pub name: String,
    /// Parameter type
    pub param_type: String,
    /// Parameter description
    pub description: String,
    /// Whether parameter is optional
    pub optional: bool,
}

/// Tutorial content structure
#[derive(Debug, Clone)]
pub struct Tutorial {
    /// Tutorial title
    pub title: String,
    /// Tutorial description
    pub description: String,
    /// Tutorial content (markdown format)
    pub content: String,
    /// Code examples within the tutorial
    pub code_examples: Vec<String>,
    /// Difficulty level (Beginner, Intermediate, Advanced)
    pub difficulty: String,
}

/// Code example structure
#[derive(Debug, Clone)]
pub struct Example {
    /// Example title
    pub title: String,
    /// Example description
    pub description: String,
    /// Example code
    pub code: String,
    /// Expected output (if applicable)
    pub expected_output: Option<String>,
    /// Example category
    pub category: String,
}

impl DocumentationSite {
    /// Create a new documentation site with default values
    pub fn new() -> Self {
        Self {
            title: "SciRS2 NDImage Documentation".to_string(),
            description:
                "Comprehensive documentation for SciRS2 N-dimensional image processing library"
                    .to_string(),
            version: "0.1.0".to_string(),
            base_url: "https://scirs2.github.io/ndimage".to_string(),
            modules: Vec::new(),
            tutorials: Vec::new(),
            examples: Vec::new(),
        }
    }

    /// Build comprehensive documentation by calling all builders
    pub fn build_comprehensive_documentation(&mut self) -> Result<()> {
        self.build_module_documentation()?;
        self.build_tutorials()?;
        self.build_examples()?;
        Ok(())
    }
}

impl Default for DocumentationSite {
    fn default() -> Self {
        Self::new()
    }
}

impl ModuleDoc {
    /// Create a new module documentation entry
    pub fn new(name: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: description.into(),
            functions: Vec::new(),
            examples: Vec::new(),
        }
    }

    /// Add a function to this module's documentation
    pub fn add_function(&mut self, function: FunctionDoc) {
        self.functions.push(function);
    }

    /// Add an example to this module
    pub fn add_example(&mut self, example: impl Into<String>) {
        self.examples.push(example.into());
    }
}

impl FunctionDoc {
    /// Create a new function documentation entry
    pub fn new(
        name: impl Into<String>,
        signature: impl Into<String>,
        description: impl Into<String>,
        returns: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            signature: signature.into(),
            description: description.into(),
            returns: returns.into(),
            parameters: Vec::new(),
            examples: Vec::new(),
            notes: Vec::new(),
        }
    }

    /// Add a parameter to this function
    pub fn add_parameter(&mut self, parameter: Parameter) {
        self.parameters.push(parameter);
    }

    /// Add an example to this function
    pub fn add_example(&mut self, example: impl Into<String>) {
        self.examples.push(example.into());
    }

    /// Add a note to this function
    pub fn add_note(&mut self, note: impl Into<String>) {
        self.notes.push(note.into());
    }
}

impl Parameter {
    /// Create a new parameter documentation entry
    pub fn new(
        name: impl Into<String>,
        param_type: impl Into<String>,
        description: impl Into<String>,
        optional: bool,
    ) -> Self {
        Self {
            name: name.into(),
            param_type: param_type.into(),
            description: description.into(),
            optional,
        }
    }

    /// Create a required parameter
    pub fn required(
        name: impl Into<String>,
        param_type: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self::new(name, param_type, description, false)
    }

    /// Create an optional parameter
    pub fn optional(
        name: impl Into<String>,
        param_type: impl Into<String>,
        description: impl Into<String>,
    ) -> Self {
        Self::new(name, param_type, description, true)
    }
}

impl Tutorial {
    /// Create a new tutorial
    pub fn new(
        title: impl Into<String>,
        description: impl Into<String>,
        content: impl Into<String>,
        difficulty: impl Into<String>,
    ) -> Self {
        Self {
            title: title.into(),
            description: description.into(),
            content: content.into(),
            difficulty: difficulty.into(),
            code_examples: Vec::new(),
        }
    }

    /// Add a code example to this tutorial
    pub fn add_code_example(&mut self, example: impl Into<String>) {
        self.code_examples.push(example.into());
    }

    /// Create a beginner tutorial
    pub fn beginner(
        title: impl Into<String>,
        description: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::new(title, description, content, "Beginner")
    }

    /// Create an intermediate tutorial
    pub fn intermediate(
        title: impl Into<String>,
        description: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::new(title, description, content, "Intermediate")
    }

    /// Create an advanced tutorial
    pub fn advanced(
        title: impl Into<String>,
        description: impl Into<String>,
        content: impl Into<String>,
    ) -> Self {
        Self::new(title, description, content, "Advanced")
    }
}

impl Example {
    /// Create a new code example
    pub fn new(
        title: impl Into<String>,
        description: impl Into<String>,
        code: impl Into<String>,
        category: impl Into<String>,
    ) -> Self {
        Self {
            title: title.into(),
            description: description.into(),
            code: code.into(),
            category: category.into(),
            expected_output: None,
        }
    }

    /// Create a new code example with expected output
    pub fn with_output(
        title: impl Into<String>,
        description: impl Into<String>,
        code: impl Into<String>,
        category: impl Into<String>,
        expected_output: impl Into<String>,
    ) -> Self {
        Self {
            title: title.into(),
            description: description.into(),
            code: code.into(),
            category: category.into(),
            expected_output: Some(expected_output.into()),
        }
    }

    /// Set the expected output for this example
    pub fn set_expected_output(&mut self, output: impl Into<String>) {
        self.expected_output = Some(output.into());
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_documentation_site_creation() {
        let site = DocumentationSite::new();
        assert_eq!(site.title, "SciRS2 NDImage Documentation");
        assert_eq!(site.version, "0.1.0");
        assert!(site.modules.is_empty());
        assert!(site.tutorials.is_empty());
        assert!(site.examples.is_empty());
    }

    #[test]
    fn test_module_doc_creation() {
        let mut module = ModuleDoc::new("filters", "Image filtering operations");
        assert_eq!(module.name, "filters");
        assert_eq!(module.description, "Image filtering operations");
        assert!(module.functions.is_empty());

        module.add_example("Basic filtering example");
        assert_eq!(module.examples.len(), 1);
    }

    #[test]
    fn test_function_doc_creation() {
        let mut func = FunctionDoc::new(
            "gaussian_filter",
            "pub fn gaussian_filter<T>(input: &ArrayD<T>, sigma: f64) -> ArrayD<T>",
            "Apply Gaussian filter to n-dimensional array",
            "ArrayD<T> - Filtered array",
        );

        let param = Parameter::required("input", "&ArrayD<T>", "Input n-dimensional array");
        func.add_parameter(param);
        func.add_note("Uses separable convolution for efficiency");

        assert_eq!(func.name, "gaussian_filter");
        assert_eq!(func.parameters.len(), 1);
        assert_eq!(func.notes.len(), 1);
    }

    #[test]
    fn test_parameter_creation() {
        let required_param = Parameter::required("input", "&ArrayD<T>", "Input array");
        assert!(!required_param.optional);

        let optional_param = Parameter::optional("sigma", "f64", "Standard deviation");
        assert!(optional_param.optional);
    }

    #[test]
    fn test_tutorial_creation() {
        let tutorial = Tutorial::beginner(
            "Getting Started",
            "Introduction to image processing",
            "# Getting Started\n\nThis tutorial covers basic operations...",
        );

        assert_eq!(tutorial.title, "Getting Started");
        assert_eq!(tutorial.difficulty, "Beginner");
        assert!(tutorial.code_examples.is_empty());
    }

    #[test]
    fn test_example_creation() {
        let example = Example::new(
            "Basic Filtering",
            "Simple Gaussian filter example",
            "let filtered = gaussian_filter(&image, 2.0);",
            "filters",
        );

        assert_eq!(example.title, "Basic Filtering");
        assert_eq!(example.category, "filters");
        assert!(example.expected_output.is_none());

        let example_with_output = Example::with_output(
            "Math Example",
            "Simple math operation",
            "2 + 2",
            "math",
            "4",
        );

        assert!(example_with_output.expected_output.is_some());
        assert_eq!(example_with_output.expected_output.expect("Operation failed"), "4");
    }
}