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

use pretty::{BoxAllocator, DocAllocator};

use crate::{
    defpart,
    pretty::{string, Part},
};

defpart! {
    #[lowlevel]
    for string;

    impl Part(|low_level, s| low_level.append(s))
}

defpart! {
    #[lowlevel]
    for String;

    impl Part(|low_level, s| low_level.append(s))
}

defpart! {
    #[lowlevel]
    struct Text {
        text: String,
    }

    constructor(text: impl Display) {
        Text { text: text.to_string() }
    }

    impl Part(|low_level, Text { text }| low_level.append(text))
}

#[allow(non_snake_case)]
pub fn DebugText(text: impl Debug + Display) -> Text {
    Text {
        text: format!("{:?}", text),
    }
}

pub const SP: Whitespace = Whitespace::Space;
#[allow(unused)]
pub const BR: Whitespace = Whitespace::Break;

defpart! {
    #[lowlevel]
    struct Opaque {
        tag: String,
    }

    constructor(text: impl Display) {
        Opaque { tag: text.to_string() }
    }

    impl Part(|low_level, Opaque { tag }| low_level.append(tag))
}

#[derive(Debug, Copy, Clone)]
pub enum Whitespace {
    /// A space is a newline when the surrounding group is broken, or a single
    /// space when it's not.
    Space,
    #[allow(unused)]
    /// A break is a newline when the surrounding group is broken, or nothing
    /// when it's not.
    Break,
}

// impl PartTrait for Whitespace {
//     fn append_to(self, builder: Part) -> Part {
//         builder.low_level(|pretty| match self {
//             Whitespace::Break => pretty.append(BoxAllocator.line_()),
//             Whitespace::Space => pretty.append(BoxAllocator.line()),
//         })
//     }
// }

defpart! {
    #[lowlevel]
    for Whitespace;

    impl Part(|low_level, whitespace| match whitespace {
        Whitespace::Space => low_level.append(BoxAllocator.line()),
        Whitespace::Break => low_level.append(BoxAllocator.line_()),
    })
}