Page MenuHomeDevCentral

D2752.id6988.diff
No OneTemporary

D2752.id6988.diff

diff --git a/fantoir-datasource/Cargo.toml b/fantoir-datasource/Cargo.toml
--- a/fantoir-datasource/Cargo.toml
+++ b/fantoir-datasource/Cargo.toml
@@ -13,6 +13,7 @@
lazy_static = "~1.4.0"
opendatasoft-explore-api = { version = "0.1.0", path = "../opendatasoft-explore-api" }
oxrdf = "~0.1.1"
+regex = "~1.7.1"
sparesults = "~0.1.3"
[dependencies.async-scoped]
diff --git a/fantoir-datasource/src/commands/query.rs b/fantoir-datasource/src/commands/query.rs
--- a/fantoir-datasource/src/commands/query.rs
+++ b/fantoir-datasource/src/commands/query.rs
@@ -3,6 +3,7 @@
use sqlx::PgPool;
use crate::db::connect_to_db;
+use crate::fantoir::looks_like_canonical_fantoir_code;
use crate::QueryArgs;
use crate::services::query::*;
@@ -26,7 +27,12 @@
exit(EXIT_CODE_NO_RESULT_FOUND);
}
- if args.libelle.len() > 0 {
+ if args.expression.len() > 0 {
+ if let Some(code) = pick_fantoir_code_from_args(&args.expression) {
+ search_one_row(&pool, &code).await;
+ return;
+ }
+
search_libelle(&pool, args).await;
return;
}
@@ -46,7 +52,7 @@
}
async fn search_libelle(pool: &PgPool, args: QueryArgs) {
- let expression = args.libelle.join(" ");
+ let expression = args.expression.join(" ");
let mut found = false;
@@ -74,3 +80,11 @@
return true;
}
+
+fn pick_fantoir_code_from_args (expressions: &Vec<String>) -> Option<String> {
+ if expressions.len() == 1 && looks_like_canonical_fantoir_code(&expressions[0]) {
+ Some(expressions[0].clone())
+ } else {
+ None
+ }
+}
diff --git a/fantoir-datasource/src/fantoir.rs b/fantoir-datasource/src/fantoir.rs
--- a/fantoir-datasource/src/fantoir.rs
+++ b/fantoir-datasource/src/fantoir.rs
@@ -5,6 +5,7 @@
use chrono::NaiveDate;
use lazy_static::lazy_static;
+use regex::Regex;
use sqlx::PgPool;
lazy_static! {
@@ -15,6 +16,10 @@
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
+
+ static ref RE_FANTOIR: Regex = Regex::new(
+ "^[0-9][0-9A-Z][0-9][0-9][0-9][0-9][0-9A-Z][0-9][0-9][0-9][A-Z]$"
+ ).unwrap();
}
/// A voie in the FANTOIR database
@@ -258,6 +263,16 @@
return RIVOLI_STRING[index as usize];
}
+/// Determines if the specified expression looks like a FANTOIR code,
+/// as used by DGFiP official FANTOIR file.
+///
+/// The IGN or OpenStreetMap format variants will return false.
+///
+/// This method does NOT check the RIVOLI key, only the format.
+pub fn looks_like_canonical_fantoir_code (expression: &str) -> bool {
+ RE_FANTOIR.is_match(expression)
+}
+
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
@@ -318,7 +333,6 @@
assert_eq!(expected, fix_fantoir_code("92002_5130"), "As code direction can't be computed, this code should be to search");
}
-
#[test]
pub fn test_compute_rivoli_key() {
assert_eq!('W', compute_rivoli_key("380003B001"));
@@ -329,4 +343,16 @@
pub fn test_compute_rivoli_key_with_type_voie_zero() {
assert_eq!('C', compute_rivoli_key("9722230261"));
}
+
+ #[test]
+ pub fn test_looks_like_canonical_fantoir_code () {
+ assert!(looks_like_canonical_fantoir_code("770246B015C"));
+ }
+
+ #[test]
+ pub fn test_looks_like_canonical_fantoir_code_for_variants () {
+ assert!(!looks_like_canonical_fantoir_code("770246B015"));
+ assert!(!looks_like_canonical_fantoir_code("77246_B015"));
+ assert!(!looks_like_canonical_fantoir_code("77246B015C"));
+ }
}
diff --git a/fantoir-datasource/src/main.rs b/fantoir-datasource/src/main.rs
--- a/fantoir-datasource/src/main.rs
+++ b/fantoir-datasource/src/main.rs
@@ -89,8 +89,8 @@
#[arg(long)]
code_voie: Option<String>,
- /// Expression to search
- libelle: Vec<String>,
+ /// Expression to search, FANTOIR code or label of the voie
+ expression: Vec<String>,
}
#[tokio::main]

File Metadata

Mime Type
text/plain
Expires
Sun, Nov 24, 04:22 (20 h, 59 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2259256
Default Alt Text
D2752.id6988.diff (3 KB)

Event Timeline