Added record struct, de- &serializes and to modify target url.

This commit is contained in:
2025-06-05 02:26:49 +02:00
parent 49eab01537
commit 0a7d72825f
3 changed files with 153 additions and 2 deletions
+53 -2
View File
@@ -1,3 +1,54 @@
fn main() {
println!("Hello, world!");
use std::fs::File;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug)]
struct Record {
source_url: String,
target_url: Option<String>,
http_code: Option<String>,
enabled: Option<String>,
query_params_handling: Option<String>,
sales_channel_id: Option<String>,
}
fn main() -> Result<(), csv::Error> {
let file = File::open("seo_url.csv")?;
let mut rdr = csv::Reader::from_reader(file);
let mut records: Vec<Record> = Vec::new();
for result in rdr.deserialize() {
let mut record: Record = result?;
//println!("{:?}", record);
let mut temp_target_url: Vec<String> = record.source_url.split("/").map(|s| s.to_string()).collect();
// Modify to new target url
temp_target_url.remove(temp_target_url.len() - 2); // Remove number before product name
// Combine to new target url
let target_url = temp_target_url.join("/");
record.target_url = Some(target_url);
record.http_code = Some("301".to_string());
record.enabled = Some("1".to_string());
record.query_params_handling = Some("0".to_string());
record.sales_channel_id = Some("".to_string());
records.push(record)
}
let new_csv = File::create("redirects.csv")?;
let mut wtr = csv::Writer::from_writer(new_csv);
for record in records {
wtr.serialize(record)?;
}
wtr.flush()?;
Ok(())
}