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
use std::borrow::Cow;
use std::fmt::{self, Display};

use crate::CMakeListsTokens;

mod add_compile_options;

pub trait TextNode<'tn>: Display {
    fn text_node<T>(bytes: T) -> Self
    where
        T: Into<Cow<'tn, [u8]>>;
}

pub struct Doc<TN> {
    commands: Vec<Command<TN>>,
}

impl<'tn, TN: TextNode<'tn>> Doc<TN> {
    pub fn commands(&self) -> &[Command<TN>] {
        &self.commands
    }
}

impl<'tn, TN: TextNode<'tn>> IntoIterator for Doc<TN> {
    type Item = Command<TN>;

    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.commands.into_iter()
    }
}

impl<'tn, TN: TextNode<'tn>> From<&'tn CMakeListsTokens<'tn>> for Doc<TN> {
    fn from(value: &'tn CMakeListsTokens<'tn>) -> Self {
        let commands = value
            .command_invocations()
            .filter_map(|ci| match ci.identifier {
                b"add_compile_options" => Some(Command::AddCompileOptions(ci.into())),
                _ => None,
            })
            .collect();
        Self { commands }
    }
}

pub type Utf8Doc<'doc> = Doc<Utf8TextNode<'doc>>;

/// CMake command.
///
/// Reference: <https://cmake.org/cmake/help/v3.0/manual/cmake-commands.7.html>
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Command<TN> {
    AddCompileOptions(add_compile_options::AddCompileOptions<TN>),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Utf8TextNode<'a> {
    bytes: Cow<'a, [u8]>,
}

impl<'a> TextNode<'a> for Utf8TextNode<'a> {
    fn text_node<T>(bytes: T) -> Self
    where
        T: Into<Cow<'a, [u8]>>,
    {
        Utf8TextNode {
            bytes: bytes.into(),
        }
    }
}

impl<'a> Display for Utf8TextNode<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", String::from_utf8_lossy(&self.bytes))
    }
}