use std::fs::File; use std::io::{BufReader, BufRead}; use rusqlite; fn main() -> std::io::Result<()> { let fantoir_path = std::env::args().nth(1).unwrap(); let connection = rusqlite::Connection::open("fantoir.sqlite").unwrap(); connection.execute( " CREATE TABLE IF NOT EXISTS streets ( id INTEGER PRIMARY KEY, insee TEXT NOT NULL, rivoli TEXT NOT NULL, libelle TEXT NOT NULL ); ", [] ).unwrap(); let mut insert_statement = connection .prepare("INSERT INTO streets (insee, rivoli, libelle) values (?,?,?)") .unwrap(); let file = match File::open(&fantoir_path) { Err(err) => panic!("Cannot read file {}: {}", fantoir_path, err), Ok(file) => file, }; let reader = BufReader::new(file); let mut full_insee = String::with_capacity(5); for line in reader.lines() { let l = line.unwrap(); if l.chars().nth(3) == Some(' ') { // Enregistrement Département } else if l.chars().nth(6) == Some(' ') { // Enregistrement Commune } else { full_insee = String::from(l.get(0..2).unwrap()); full_insee.push_str(l.get(3..6).unwrap()); let rivoli_with_key = l.get(6..11).unwrap(); let libelle = l.get(15..41).unwrap(); insert_statement.execute(rusqlite::params![&full_insee, rivoli_with_key, libelle]).unwrap(); /* A priori on peut tout parser. Il nous faut au moins: - Libellé voie (index 15 a 41) - code insee (index 3 a 5) - code rivoli (??) */ } }; Ok(()) }