bind-conf/dns_zone/src/context.rs

62 lines
1.2 KiB
Rust

use std::ops::Deref;
use std::fmt::{self, Display};
#[derive(Debug, PartialEq, Clone, Default)]
pub struct Context {
pub line: u32,
pub column: u32,
pub index: u32,
// TODO: add context length
}
impl Display for Context {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
#[derive(Debug, PartialEq, Clone)]
pub struct CtxData<T: Clone> {
pub value: T,
pub context: Context
}
impl<T> Deref for CtxData<T> where T: Clone {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl From<&str> for CtxData<String> {
fn from(value: &str) -> Self {
let start = if value.len() == 0 { 0 } else { 1 };
CtxData {
value: value.into(),
context: Context {
line: start,
column: start,
index: start,
}
}
}
}
impl<T> CtxData<T> where T: Clone {
pub fn new(value: T, line: u32, column: u32, index: u32) -> Self {
CtxData {
value,
context: Context {
line,
column,
index,
}
}
}
}
pub type CtxString = CtxData<String>;