photorg 0.1.0

Fast cross-platform CLI that organizes photos into date- and location-structured folders
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! Path template parsing and rendering.
//!
//! `--group` and `--location` are sugar that expand into templates, so grouping
//! and `--template` share exactly one renderer.

use std::fmt::Write as _;
use std::path::PathBuf;

use chrono::{Datelike, NaiveDate};

use crate::error::TemplateError;
use crate::sanitize;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Var {
    Year,
    Month,
    MonthName,
    Day,
    IsoYear,
    IsoWeek,
    Country,
    Region,
    City,
    CameraMake,
    CameraModel,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Class {
    Date,
    Location,
    Camera,
}

impl Var {
    fn parse(name: &str) -> Option<Var> {
        Some(match name {
            "year" => Var::Year,
            "month" => Var::Month,
            "month_name" => Var::MonthName,
            "day" => Var::Day,
            "iso_year" => Var::IsoYear,
            "iso_week" => Var::IsoWeek,
            "country" => Var::Country,
            "region" => Var::Region,
            "city" => Var::City,
            "camera_make" => Var::CameraMake,
            "camera_model" => Var::CameraModel,
            _ => return None,
        })
    }

    pub fn class(self) -> Class {
        match self {
            Var::Year | Var::Month | Var::MonthName | Var::Day | Var::IsoYear | Var::IsoWeek => {
                Class::Date
            }
            Var::Country | Var::Region | Var::City => Class::Location,
            Var::CameraMake | Var::CameraModel => Class::Camera,
        }
    }

    fn is_numeric(self) -> bool {
        matches!(
            self,
            Var::Year | Var::Month | Var::Day | Var::IsoYear | Var::IsoWeek
        )
    }
}

#[derive(Debug, Clone)]
enum Piece {
    Literal(String),
    Var { var: Var, pad: usize },
}

/// One path component's worth of template.
#[derive(Debug, Clone)]
struct Segment {
    pieces: Vec<Piece>,
}

impl Segment {
    fn vars(&self) -> impl Iterator<Item = Var> + '_ {
        self.pieces.iter().filter_map(|p| match p {
            Piece::Var { var, .. } => Some(*var),
            Piece::Literal(_) => None,
        })
    }

    fn classes(&self) -> Vec<Class> {
        let mut cs: Vec<Class> = self.vars().map(Var::class).collect();
        cs.dedup();
        cs
    }
}

#[derive(Debug, Clone)]
pub struct Template {
    segments: Vec<Segment>,
    source: String,
}

/// Everything a template can interpolate for one photo.
#[derive(Debug, Default, Clone)]
pub struct RenderVars<'a> {
    pub date: Option<NaiveDate>,
    pub location: Option<&'a Location>,
    pub camera_make: Option<&'a str>,
    pub camera_model: Option<&'a str>,
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Location {
    pub country: Option<String>,
    pub region: Option<String>,
    pub city: Option<String>,
}

const MONTH_NAMES: [&str; 12] = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December",
];

impl Template {
    pub fn parse(input: &str) -> Result<Template, TemplateError> {
        if input.trim().is_empty() {
            return Err(TemplateError::Empty);
        }
        if input.starts_with('/') || input.starts_with('\\') {
            return Err(TemplateError::Absolute);
        }
        if input.len() >= 2 && input.as_bytes()[1] == b':' {
            return Err(TemplateError::Absolute);
        }

        let mut segments = Vec::new();
        let mut pieces: Vec<Piece> = Vec::new();
        let mut literal = String::new();
        let bytes = input.as_bytes();
        let mut i = 0;

        while i < bytes.len() {
            match bytes[i] {
                b'{' => {
                    if !literal.is_empty() {
                        pieces.push(Piece::Literal(std::mem::take(&mut literal)));
                    }
                    let end = input[i..]
                        .find('}')
                        .map(|off| i + off)
                        .ok_or(TemplateError::UnclosedBrace(i))?;
                    let body = &input[i + 1..end];
                    let (name, pad) = match body.split_once(':') {
                        Some((name, spec)) => {
                            let width = spec
                                .strip_prefix('0')
                                .and_then(|w| w.parse::<usize>().ok())
                                .filter(|w| *w <= 9)
                                .ok_or_else(|| TemplateError::BadPadding(spec.to_string()))?;
                            (name, width)
                        }
                        None => (body, 0),
                    };
                    let var = Var::parse(name.trim())
                        .ok_or_else(|| TemplateError::UnknownVariable(name.trim().to_string()))?;
                    if pad > 0 && !var.is_numeric() {
                        return Err(TemplateError::BadPadding(format!("{name} is not numeric")));
                    }
                    pieces.push(Piece::Var { var, pad });
                    i = end + 1;
                }
                b'}' => return Err(TemplateError::StrayBrace(i)),
                b'/' | b'\\' => {
                    if !literal.is_empty() {
                        pieces.push(Piece::Literal(std::mem::take(&mut literal)));
                    }
                    if !pieces.is_empty() {
                        segments.push(Segment {
                            pieces: std::mem::take(&mut pieces),
                        });
                    }
                    i += 1;
                }
                _ => {
                    let ch = input[i..].chars().next().expect("byte index on boundary");
                    literal.push(ch);
                    i += ch.len_utf8();
                }
            }
        }
        if !literal.is_empty() {
            pieces.push(Piece::Literal(literal));
        }
        if !pieces.is_empty() {
            segments.push(Segment { pieces });
        }
        if segments.is_empty() {
            return Err(TemplateError::Empty);
        }

        // `..` is rejected on raw literal text; a rendered value can never
        // produce it, because the sanitizer strips leading dots.
        for seg in &segments {
            let all_literal: String = seg
                .pieces
                .iter()
                .map(|p| match p {
                    Piece::Literal(s) => s.as_str(),
                    Piece::Var { .. } => "",
                })
                .collect();
            if all_literal.trim() == ".." {
                return Err(TemplateError::ParentEscape);
            }
        }

        Ok(Template {
            segments,
            source: input.to_string(),
        })
    }

    /// Does rendering this template need a reverse-geocode lookup?
    pub fn uses_location(&self) -> bool {
        self.segments
            .iter()
            .flat_map(Segment::vars)
            .any(|v| v.class() == Class::Location)
    }

    pub fn source(&self) -> &str {
        &self.source
    }

    /// Render to a relative directory path, sanitizing every component.
    ///
    /// A segment whose variables are all unresolved location fields is dropped;
    /// unresolved dates collapse to a single `unknown-date` component.
    pub fn render(&self, vars: &RenderVars<'_>) -> PathBuf {
        let mut out = PathBuf::new();
        let mut last_was_unknown_date = false;

        for seg in &self.segments {
            let classes = seg.classes();

            if !classes.is_empty()
                && classes.iter().all(|c| *c == Class::Location)
                && seg
                    .vars()
                    .all(|v| location_value(vars.location, v).is_none())
            {
                continue;
            }

            if classes.contains(&Class::Date) && vars.date.is_none() {
                if !last_was_unknown_date {
                    out.push(sanitize::UNKNOWN_DATE);
                    last_was_unknown_date = true;
                }
                continue;
            }
            last_was_unknown_date = false;

            let mut rendered = String::new();
            for piece in &seg.pieces {
                match piece {
                    Piece::Literal(s) => rendered.push_str(s),
                    Piece::Var { var, pad } => match value_of(*var, vars) {
                        Some(v) if *pad > 0 => {
                            let _ = write!(rendered, "{:0>width$}", v, width = *pad);
                        }
                        Some(v) => rendered.push_str(&v),
                        None => rendered.push_str(placeholder_for(*var)),
                    },
                }
            }
            out.push(sanitize::component(&rendered));
        }

        if out.as_os_str().is_empty() {
            out.push(sanitize::UNKNOWN_DATE);
        }
        out
    }
}

fn placeholder_for(var: Var) -> &'static str {
    match var.class() {
        Class::Date => sanitize::UNKNOWN_DATE,
        Class::Location => sanitize::UNKNOWN_LOCATION,
        Class::Camera => "unknown-camera",
    }
}

fn location_value(loc: Option<&Location>, var: Var) -> Option<String> {
    let loc = loc?;
    match var {
        Var::Country => loc.country.clone(),
        Var::Region => loc.region.clone(),
        Var::City => loc.city.clone(),
        _ => None,
    }
}

fn value_of(var: Var, vars: &RenderVars<'_>) -> Option<String> {
    match var {
        Var::Year => vars.date.map(|d| d.year().to_string()),
        Var::Month => vars.date.map(|d| d.month().to_string()),
        Var::MonthName => vars
            .date
            .map(|d| MONTH_NAMES[(d.month() - 1) as usize].to_string()),
        Var::Day => vars.date.map(|d| d.day().to_string()),
        // ISO year and week must come from the same call: 2026-12-31 is week 1
        // of ISO year 2027.
        Var::IsoYear => vars.date.map(|d| d.iso_week().year().to_string()),
        Var::IsoWeek => vars.date.map(|d| d.iso_week().week().to_string()),
        Var::Country | Var::Region | Var::City => location_value(vars.location, var),
        Var::CameraMake => vars.camera_make.map(str::to_string),
        Var::CameraModel => vars.camera_model.map(str::to_string),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn date(y: i32, m: u32, d: u32) -> Option<NaiveDate> {
        NaiveDate::from_ymd_opt(y, m, d)
    }

    fn render(t: &str, vars: &RenderVars<'_>) -> String {
        Template::parse(t)
            .unwrap()
            .render(vars)
            .to_string_lossy()
            .replace('\\', "/")
    }

    #[test]
    fn renders_month_preset() {
        let vars = RenderVars {
            date: date(2026, 8, 19),
            ..Default::default()
        };
        assert_eq!(
            render("{year}/{month:02}-{month_name}", &vars),
            "2026/08-August"
        );
    }

    #[test]
    fn iso_week_uses_iso_year() {
        // 2024-12-30 is a Monday and belongs to ISO week 1 of 2025.
        let vars = RenderVars {
            date: date(2024, 12, 30),
            ..Default::default()
        };
        assert_eq!(
            render("{iso_year}/{iso_year}-W{iso_week:02}", &vars),
            "2025/2025-W01"
        );
        // The calendar year must not leak into an ISO path.
        assert_eq!(render("{year}", &vars), "2024");

        // A year that starts on a Thursday has 53 ISO weeks, and its last day
        // stays in its own ISO year.
        let vars = RenderVars {
            date: date(2026, 12, 31),
            ..Default::default()
        };
        assert_eq!(render("{iso_year}-W{iso_week:02}", &vars), "2026-W53");
    }

    #[test]
    fn missing_date_collapses_to_one_component() {
        let vars = RenderVars::default();
        assert_eq!(render("{year}/{month:02}/{day:02}", &vars), "unknown-date");
    }

    #[test]
    fn missing_location_segments_are_dropped() {
        let vars = RenderVars {
            date: date(2026, 8, 19),
            ..Default::default()
        };
        assert_eq!(render("{year}/{country}/{region}", &vars), "2026");
    }

    #[test]
    fn location_components_are_sanitized() {
        let loc = Location {
            country: Some("Croatia".into()),
            region: Some("A/B: north".into()),
            city: None,
        };
        let vars = RenderVars {
            date: date(2026, 8, 19),
            location: Some(&loc),
            ..Default::default()
        };
        assert_eq!(
            render("{year}/{country}/{region}", &vars),
            "2026/Croatia/A-B- north"
        );
    }

    #[test]
    fn rejects_bad_templates() {
        assert_eq!(
            Template::parse("{year").unwrap_err(),
            TemplateError::UnclosedBrace(0)
        );
        assert!(matches!(
            Template::parse("{nope}").unwrap_err(),
            TemplateError::UnknownVariable(_)
        ));
        assert_eq!(
            Template::parse("/abs/{year}").unwrap_err(),
            TemplateError::Absolute
        );
        assert_eq!(
            Template::parse("C:/{year}").unwrap_err(),
            TemplateError::Absolute
        );
        assert_eq!(
            Template::parse("../{year}").unwrap_err(),
            TemplateError::ParentEscape
        );
        assert_eq!(Template::parse("").unwrap_err(), TemplateError::Empty);
        assert!(matches!(
            Template::parse("{country:02}").unwrap_err(),
            TemplateError::BadPadding(_)
        ));
    }

    #[test]
    fn literal_only_template_is_allowed() {
        assert_eq!(render("photos/all", &RenderVars::default()), "photos/all");
    }
}