random_icon_lib/
lib.rs

1//! A crate for creating a random round image in
2//! the svg format. Images are always symmetrical, in
3//! order to be more recognizable. It varies between
4//! transparent and black areas.
5//! 
6//! # Warning
7//! 
8//! An icon can be completely empty (transparent)
9//! or only showing tiny areas. If you want something
10//! to always be visible, consider giving it a border
11//! or background.
12
13mod fields;
14pub mod hashing;
15mod generate;
16mod svg;
17
18use std::{
19    ffi::OsStr,
20    fs::File,
21    io::{self, Write},
22    path::{Path, PathBuf}
23};
24
25use fields::Fields;
26use generate::from_hash::HashGen;
27
28pub use hashing::hash;
29
30
31/// Converts text to an icon (via hashing) and writes
32/// it as svg data. The same text will always result
33/// in the same icon.
34/// 
35/// # Examples
36/// 
37/// ```
38/// use random_icon_lib::write_svg_from_hashed_string;
39/// 
40/// let my_string = "Lorem ipsum dolor sit amet...".to_string();
41/// let mut file = std::fs::File::create("./tests/my_svg_file.svg").unwrap();
42/// 
43/// write_svg_from_hashed_string(my_string, file).unwrap();
44/// ```
45pub fn write_svg_from_hashed_string<T: Write>(
46    string: String,
47    target: T,
48) -> Result<(), io::Error> {
49    let hash = hash(string);
50    let hash_gen = HashGen::new(hash);
51    let fields = Fields::new(hash_gen);
52
53    fields.write_as_svg(target)
54}
55
56/// Creates a random icon and writes it as svg data.
57/// 
58/// # Examples
59/// 
60/// ```
61/// use random_icon_lib::write_random_svg;
62/// 
63/// let mut file = std::fs::File::create("./tests/my_svg_file.svg")
64///     .unwrap()
65/// ;
66/// 
67/// write_random_svg(file).unwrap();
68#[cfg(feature = "rand")]
69pub fn write_random_svg<T: Write>(
70    target: T,
71) -> Result<(), io::Error> {
72    let fields = Fields::new(rand::rng());
73
74    fields.write_as_svg(target)
75}
76
77/// Convenience function to create a new file at the
78/// given path and write an icon from a hashed string
79/// to it.
80/// 
81/// # Errors
82/// 
83/// Among other tings returns an error if a file
84/// already exists at the given path
85/// 
86/// # Examples
87/// 
88/// ```
89/// use random_icon_lib::create_svg_file_from_hashed_string;
90/// 
91/// let my_string = "Lorem ipsum dolor sit amet...".to_string();
92/// let path_to_new_file = "./tests/new_svg_file.svg";
93/// 
94/// create_svg_file_from_hashed_string(
95///     my_string,
96///     path_to_new_file
97/// ).unwrap();
98/// ```
99pub fn create_svg_file_from_hashed_string<P: AsRef<Path>>(
100    string: String,
101    path: P
102) -> io::Result<()> {
103    let target = create_svg_file(
104        path.as_ref().into()
105    )?;
106
107    write_svg_from_hashed_string(string, target)?;
108
109    Ok(())
110}
111
112/// Convenience function to create a new file at the
113/// given path and write a random icon to it.
114/// 
115/// # Errors
116/// 
117/// Among other tings returns an error if a file
118/// already exists at the given path
119/// 
120/// # Examples
121/// 
122/// ```
123/// use random_icon_lib::create_random_svg_file;
124/// 
125/// let path_to_new_file = "./tests/new_random_svg_file.svg";
126/// 
127/// create_random_svg_file(
128///     path_to_new_file
129/// ).unwrap();
130/// ```
131#[cfg(feature = "rand")]
132pub fn create_random_svg_file<P: AsRef<Path>>(
133    path: P
134) -> io::Result<()> {
135    let target = create_svg_file(
136        path.as_ref().into()
137    )?;
138
139    write_random_svg(target)?;
140
141    Ok(())
142}
143
144fn create_svg_file(
145    mut path: PathBuf
146) -> io::Result<File> {
147    if path.extension() != Some(OsStr::new("svg")) {
148        path.set_extension("svg");
149    };
150
151    File::create_new(path)
152}