summaryrefslogtreecommitdiffstats
path: root/letmein-conf/src/parse_items.rs
blob: dd3276a14ff5fb544c07c90174f23ac5029fb9ee (plain)
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
// -*- coding: utf-8 -*-
//
// Copyright (C) 2024 Michael Büsch <m@bues.ch>
//
// Licensed under the Apache License version 2.0
// or the MIT license, at your option.
// SPDX-License-Identifier: Apache-2.0 OR MIT

use anyhow::{self as ah, format_err as err};
use std::str::FromStr;

pub enum MapItem {
    KeyValue(String, String),
    KeyValues(String, Vec<String>),
    Values(Vec<String>),
}

pub struct Map {
    items: Vec<MapItem>,
}

impl FromStr for Map {
    type Err = ah::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut items = Vec::with_capacity(8);

        for item in s.split('/') {
            let item = if let Some(idx) = item.find(':') {
                let chlen = ':'.len_utf8();
                if idx < chlen {
                    return Err(err!("Invalid item key."));
                }
                let key = item[..=(idx - chlen)].trim();
                if key.is_empty() {
                    return Err(err!("Invalid item key."));
                }
                let value = &item[idx + chlen..];
                if value.find(',').is_some() {
                    MapItem::KeyValues(
                        key.to_string(),
                        value.split(',').map(|v| v.trim().to_string()).collect(),
                    )
                } else {
                    MapItem::KeyValue(key.to_string(), value.trim().to_string())
                }
            } else {
                let values = item.split(',');
                let values: Vec<String> = values.map(|s| s.trim().to_string()).collect();
                MapItem::Values(values)
            };
            items.push(item);
        }

        Ok(Map { items })
    }
}

impl Map {
    pub fn items(&self) -> &[MapItem] {
        &self.items
    }
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface