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
//! Convert a `Graph` into its Rust code equivalent.
//!
//! This allows one to generate a `Graph` at build time and then insert it
//! into the binary. This allows large graphs to be used in embedded systems
//! with next to no cost.
//!
//! ## Example
//!
//! ```no_run
//! use intern_str::{Graph, Segmentable};
//! use intern_str::builder::{Builder, Utf8Graph};
//! use intern_str_codegen::generate;
//! use std::{fs::File, io::{prelude::*, BufWriter}};
//!
//! # fn main() -> std::io::Result<()> {
//! let mut builder = Builder::<_, Utf8Graph>::new();
//!
//! builder.add("hello", 1).unwrap();
//! builder.add("world", 2).unwrap();
//!
//! let mut buffer = Vec::new();
//! let graph = builder.build(&mut buffer);
//!
//! // Convert to string.
//! let code = generate(
//!     &graph,
//!     "&'static str",
//!     "usize",
//!     |f, out| write!(f, "{}", out),
//! );
//!
//! let mut out = BufWriter::new(File::create("graph.rs").unwrap());
//! writeln!(
//!     out,
//!     "const GRAPH: intern_str::Graph<'static, 'static, &'static str, usize> = {}",
//!     code,
//! )?;
//! # Ok(()) }
//! ```

#![no_std]
#![forbid(
    unsafe_code,
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unused_import_braces,
    unused_qualifications,
    future_incompatible,
    rust_2018_idioms
)]

extern crate alloc;

use alloc::string::String;
use core::fmt::{self, Write};
use core::{write, writeln};

use intern_str::{CaseInsensitive, Graph, Segmentable};

/// The whole point.
///
/// See the crate documentation for more information.
pub fn generate<Input: Key, Output>(
    graph: &Graph<'_, '_, Input, Output>,
    input_type: &str,
    output_type: &str,
    mut write_output: impl FnMut(&mut dyn Write, &Output) -> fmt::Result,
) -> String {
    let mut out = String::new();

    writeln!(out, "{{").ok();

    // Write the nodes.
    writeln!(
        out,
        "{}const NODES: &[intern_str::Node<'static, {}, {}>] = &[",
        Indent(4),
        input_type,
        output_type
    )
    .ok();

    for node in graph.nodes().iter() {
        writeln!(out, "{}intern_str::Node::new(", Indent(8)).ok();

        writeln!(out, "{}&[", Indent(12)).ok();

        for (input, next) in node.inputs() {
            writeln!(
                out,
                "{}({}, {}),",
                Indent(16),
                WriteKey(input),
                Index(*next),
            )
            .ok();
        }

        writeln!(out, "{}],", Indent(12)).ok();

        write!(out, "{}", Indent(12)).ok();
        write_output(&mut out, node.output()).ok();
        writeln!(out, ",").ok();

        writeln!(out, "{}{},", Indent(12), Index(node.default()),).ok();

        writeln!(out, "{}{},", Indent(12), node.amount(),).ok();

        writeln!(out, "{}),", Indent(8)).ok();
    }

    writeln!(out, "{}];", Indent(4)).ok();

    // Write the graph.
    writeln!(
        out,
        "{}const GRAPH: intern_str::Graph<'static, 'static, {}, {}> = intern_str::Graph::new(NODES, {});",
        Indent(4),
        input_type,
        output_type,
        graph.start(),
    ).ok();

    writeln!(out, "{}GRAPH", Indent(4)).ok();

    writeln!(out, "}}").ok();

    out
}

/// An item that can be used as a key.
pub trait Key: Segmentable {
    /// Format the key as a Rust expression.
    fn format(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result;
}

impl<'a> Key for &'a str {
    fn format(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "\"{}\"", self)
    }
}

impl<'a, T: fmt::Debug + Ord> Key for &'a [T] {
    fn format(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "&[")?;

        for (i, item) in self.iter().enumerate() {
            if i != 0 {
                write!(f, ", ")?;
            }

            write!(f, "{:?}", item)?;
        }

        write!(f, "]")
    }
}

impl<T: AsRef<[u8]> + Key> Key for CaseInsensitive<T> {
    fn format(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "intern_str::CaseInsensitive({})", WriteKey(&self.0))
    }
}

struct WriteKey<'a, T>(&'a T);

impl<'a, T: Key> fmt::Display for WriteKey<'a, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.format(f)
    }
}

struct Indent(usize);

impl fmt::Display for Indent {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for _ in 0..self.0 {
            write!(f, " ")?;
        }

        Ok(())
    }
}

struct Index(usize);

impl fmt::Display for Index {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0 == core::usize::MAX {
            f.write_str("core::usize::MAX")
        } else {
            fmt::Display::fmt(&self.0, f)
        }
    }
}