Skip to main content

socketry_markdown/
message.rs

1// Released under the MIT License.
2// Copyright, 2024-2025, by Titus Wormer.
3// Copyright, 2026, by Samuel Williams.
4
5use crate::unist::{Point, Position};
6use alloc::{boxed::Box, fmt, string::String};
7
8#[derive(Clone, Debug, PartialEq)]
9pub struct Message {
10    /// Place of message.
11    pub place: Option<Box<Place>>,
12    /// Reason for message (should use markdown).
13    pub reason: String,
14    /// Category of message.
15    pub rule_id: Box<String>,
16    /// Namespace of message.
17    pub source: Box<String>,
18}
19
20impl fmt::Display for Message {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        if let Some(ref place) = self.place {
23            write!(f, "{place}: ")?;
24        }
25
26        write!(f, "{} ({}:{})", self.reason, self.source, self.rule_id)
27    }
28}
29
30/// Somewhere.
31#[derive(Clone, Debug, PartialEq)]
32pub enum Place {
33    /// Between two points.
34    Position(Position),
35    /// At a point.
36    Point(Point),
37}
38
39impl fmt::Display for Place {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Place::Position(position) => write!(
43                f,
44                "{}:{}-{}:{}",
45                position.start.line, position.start.column, position.end.line, position.end.column
46            ),
47            Place::Point(point) => write!(f, "{}:{}", point.line, point.column),
48        }
49    }
50}