mg_settings/position.rs
1/*
2 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22use std::fmt::{Display, Error, Formatter};
23
24#[derive(Clone, Debug, PartialEq)]
25pub struct Pos {
26 pub column: usize,
27 pub line: usize,
28}
29
30impl Pos {
31 pub fn new(line: usize, column: usize) -> Pos {
32 Pos {
33 column: column,
34 line: line,
35 }
36 }
37
38 pub fn newline(&mut self) {
39 self.line += 1;
40 self.column = 1;
41 }
42}
43
44impl Display for Pos {
45 fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
46 write!(formatter, "line {}, column {}", self.line, self.column)
47 }
48}
49
50#[derive(Debug)]
51pub struct WithPos<T> {
52 pub node: T,
53 pub pos: Pos,
54}
55
56impl<T> WithPos<T> {
57 pub fn new(node: T, pos: Pos) -> WithPos<T> {
58 WithPos {
59 node: node,
60 pos: pos,
61 }
62 }
63}
64
65impl<T: Display> Display for WithPos<T> {
66 fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
67 write!(formatter, "{}", self.node)
68 }
69}