1#![doc = include_str!("../README.md")]
4#![doc(
5 html_favicon_url = "https://kura.pro/sitemap-gen/images/favicon.ico",
6 html_logo_url = "https://kura.pro/sitemap-gen/images/logos/sitemap-gen.svg",
7 html_root_url = "https://docs.rs/sitemap-gen"
8)]
9#![crate_name = "sitemap_gen"]
10#![crate_type = "lib"]
11
12pub mod error;
22
23pub mod sitemap;
28
29pub mod utils;
31
32pub use error::SitemapError;
34pub use sitemap::{
35 convert_date_format, create_site_map_data, ChangeFreq, SiteMapData,
36 Sitemap,
37};
38
39pub type SitemapResult<T> = Result<T, SitemapError>;
41
42pub mod prelude {
44 pub use crate::error::SitemapError;
45 pub use crate::sitemap::{ChangeFreq, SiteMapData, Sitemap};
46 pub use crate::SitemapResult;
47}
48
49#[cfg(test)]
50mod tests {
51 use url::Url;
52
53 use super::*;
54 use crate::error::SitemapError;
55 use crate::sitemap::{ChangeFreq, SiteMapData, Sitemap};
56 use crate::SitemapResult;
57
58 #[test]
59 fn test_create_sitemap() {
60 let mut sitemap = Sitemap::new();
62
63 let entry = SiteMapData {
65 loc: Url::parse("http://example.com")
66 .expect("Failed to parse URL"),
67 lastmod: "2024-10-08".to_string(),
68 changefreq: ChangeFreq::Daily,
69 };
70
71 sitemap.add_entry(entry).expect("Failed to add entry");
73
74 assert_eq!(sitemap.len(), 1);
76 assert!(!sitemap.is_empty());
77 }
78
79 #[test]
80 fn test_serialize_sitemap() {
81 let mut sitemap = Sitemap::new();
83 let entry = SiteMapData {
84 loc: Url::parse("http://example.com")
85 .expect("Failed to parse URL"),
86 lastmod: "2024-10-08".to_string(),
87 changefreq: ChangeFreq::Daily,
88 };
89
90 sitemap.add_entry(entry).expect("Failed to add entry");
91
92 let serialized =
94 sitemap.to_xml().expect("Failed to serialize sitemap");
95
96 assert!(serialized.contains("<url>"));
98 assert!(serialized.contains("<loc>http://example.com/</loc>")); assert!(serialized.contains("<changefreq>daily</changefreq>"));
100 assert!(serialized.contains("<lastmod>2024-10-08</lastmod>"));
101 }
102
103 #[test]
104 fn test_invalid_url_error() {
105 let mut sitemap = Sitemap::new();
107
108 let invalid_url = Url::parse("invalid-url");
109 let result = match invalid_url {
110 Ok(valid_url) => sitemap.add_entry(SiteMapData {
111 loc: valid_url,
112 lastmod: "2024-10-08".to_string(),
113 changefreq: ChangeFreq::Daily,
114 }),
115 Err(e) => Err(SitemapError::UrlError(e)),
116 };
117
118 assert!(matches!(result, Err(SitemapError::UrlError(_))));
120 }
121
122 #[test]
123 fn test_convert_date_format() {
124 let date = "2024-10-08T00:00:00Z";
126 let converted = convert_date_format(date);
127 assert_eq!(converted, "2024-10-08");
128 }
129
130 #[test]
131 fn test_change_freq_enum() {
132 assert_eq!(ChangeFreq::Daily.to_string(), "daily");
134 assert_eq!(ChangeFreq::Monthly.to_string(), "monthly");
135 }
136
137 #[test]
138 fn test_sitemap_data_creation() {
139 let sitemap_entry = SiteMapData {
141 loc: Url::parse("http://example.com")
142 .expect("Failed to parse URL"),
143 lastmod: "2024-10-08".to_string(),
144 changefreq: ChangeFreq::Daily,
145 };
146
147 let mut sitemap = Sitemap::new();
149 sitemap
150 .add_entry(sitemap_entry)
151 .expect("Failed to add entry");
152
153 assert_eq!(sitemap.len(), 1);
155 }
156
157 #[test]
158 fn test_sitemap_error_handling() {
159 let url_error: SitemapError =
161 SitemapError::UrlError(url::ParseError::EmptyHost);
162 let io_error: SitemapError =
163 SitemapError::IoError(std::io::Error::new(
164 std::io::ErrorKind::NotFound,
165 "File not found",
166 ));
167
168 assert!(matches!(url_error, SitemapError::UrlError(_)));
169 assert!(matches!(io_error, SitemapError::IoError(_)));
170 }
171
172 #[test]
173 fn test_sitemap_result() {
174 let success: SitemapResult<&str> = Ok("Success");
176 let failure: SitemapResult<&str> =
177 Err(SitemapError::UrlError(url::ParseError::EmptyHost));
178
179 assert!(success.is_ok());
180 assert!(failure.is_err());
181 }
182 #[test]
183 fn test_valid_url_addition() {
184 let mut sitemap = Sitemap::new();
186
187 let valid_url = Url::parse("http://example.com")
189 .expect("Failed to parse valid URL");
190
191 let result = sitemap.add_entry(SiteMapData {
192 loc: valid_url,
193 lastmod: "2024-10-08".to_string(),
194 changefreq: ChangeFreq::Daily,
195 });
196
197 assert!(result.is_ok(), "Failed to add valid URL to sitemap");
199
200 assert_eq!(sitemap.len(), 1);
202 assert!(!sitemap.is_empty());
203 }
204}