Skip to main content

brooks_lib/
common.rs

1// brooks, Copyright 2026, Will Hawkins
2//
3// This file is part of brooks.
4
5// This file is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use std::fmt::Debug;
19use std::fmt::Display;
20
21pub trait Location: Display + Debug {}
22
23#[derive(Debug, Clone, Default, PartialEq)]
24pub struct TextLocation {
25    pub start: usize,
26    pub extent: usize,
27}
28
29impl Location for TextLocation {}
30
31impl Display for TextLocation {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{} to {}", self.start, self.start + self.extent)
34    }
35}
36
37pub trait Formatter<T> {
38    fn format(&self, value: &T) -> String;
39}
40
41pub trait Pretty {
42    fn pretty(&self, formatter: &dyn Formatter<Self>) -> String;
43}
44
45impl<I> Pretty for I {
46    fn pretty(&self, formatter: &dyn Formatter<I>) -> String {
47        formatter.format(self)
48    }
49}