Skip to content

Commit

Permalink
feat: add document_tree::ValueType.
Browse files Browse the repository at this point in the history
  • Loading branch information
yassun7010 committed Jan 18, 2025
1 parent de2d540 commit 0837eaa
Show file tree
Hide file tree
Showing 22 changed files with 322 additions and 50 deletions.
8 changes: 8 additions & 0 deletions crates/document-tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod error;
mod key;
pub mod support;
mod value;
mod value_type;

pub use error::Error;
pub use key::{Key, KeyKind};
Expand All @@ -12,6 +13,7 @@ pub use value::{
Array, ArrayKind, Boolean, Float, Integer, IntegerKind, LocalDate, LocalDateTime, LocalTime,
OffsetDateTime, String, StringKind, Table, TableKind, Value,
};
pub use value_type::ValueType;

#[derive(Debug, Clone, PartialEq)]
pub struct Root(Table);
Expand All @@ -36,6 +38,12 @@ enum RootItem {
KeyValue(Table),
}

pub trait ValueImpl {
fn value_type(&self) -> ValueType;

fn range(&self) -> text::Range;
}

pub trait TryIntoDocumentTree<T> {
fn try_into_document_tree(self, toml_version: TomlVersion) -> Result<T, Vec<crate::Error>>;
}
Expand Down
21 changes: 21 additions & 0 deletions crates/document-tree/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ impl Value {
}
}

impl crate::ValueImpl for Value {
fn value_type(&self) -> crate::ValueType {
match self {
Value::Boolean(boolean) => boolean.value_type(),
Value::Integer(integer) => integer.value_type(),
Value::Float(float) => float.value_type(),
Value::String(string) => string.value_type(),
Value::OffsetDateTime(offset_date_time) => offset_date_time.value_type(),
Value::LocalDateTime(local_date_time) => local_date_time.value_type(),
Value::LocalDate(local_date) => local_date.value_type(),
Value::LocalTime(local_time) => local_time.value_type(),
Value::Array(array) => array.value_type(),
Value::Table(table) => table.value_type(),
}
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryIntoDocumentTree<Value> for ast::Value {
fn try_into_document_tree(
self,
Expand Down
12 changes: 11 additions & 1 deletion crates/document-tree/src/value/array.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ast::AstNode;

use crate::{support::comment::try_new_comment, TryIntoDocumentTree, Value};
use crate::{support::comment::try_new_comment, TryIntoDocumentTree, Value, ValueImpl, ValueType};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ArrayKind {
Expand Down Expand Up @@ -153,6 +153,16 @@ impl Array {
}
}

impl ValueImpl for Array {
fn value_type(&self) -> ValueType {
ValueType::Array
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryIntoDocumentTree<Array> for ast::Array {
fn try_into_document_tree(
self,
Expand Down
12 changes: 12 additions & 0 deletions crates/document-tree/src/value/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::{ValueImpl, ValueType};

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Boolean {
value: bool,
Expand Down Expand Up @@ -26,6 +28,16 @@ impl Boolean {
}
}

impl ValueImpl for Boolean {
fn value_type(&self) -> ValueType {
ValueType::Boolean
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryFrom<ast::Boolean> for Boolean {
type Error = Vec<crate::Error>;

Expand Down
42 changes: 41 additions & 1 deletion crates/document-tree/src/value/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
support::chrono::{
try_new_local_date, try_new_local_date_time, try_new_local_time, try_new_offset_date_time,
},
TryIntoDocumentTree,
TryIntoDocumentTree, ValueImpl, ValueType,
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -119,6 +119,46 @@ impl LocalTime {
}
}

impl ValueImpl for OffsetDateTime {
fn value_type(&self) -> ValueType {
ValueType::OffsetDateTime
}

fn range(&self) -> text::Range {
self.range()
}
}

impl ValueImpl for LocalDateTime {
fn value_type(&self) -> ValueType {
ValueType::LocalDateTime
}

fn range(&self) -> text::Range {
self.range()
}
}

impl ValueImpl for LocalDate {
fn value_type(&self) -> ValueType {
ValueType::LocalDate
}

fn range(&self) -> text::Range {
self.range()
}
}

impl ValueImpl for LocalTime {
fn value_type(&self) -> ValueType {
ValueType::LocalTime
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryIntoDocumentTree<OffsetDateTime> for ast::OffsetDateTime {
fn try_into_document_tree(
self,
Expand Down
12 changes: 11 additions & 1 deletion crates/document-tree/src/value/float.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::support::float::try_from_float;
use crate::{support::float::try_from_float, ValueImpl, ValueType};

#[derive(Debug, Clone, PartialEq)]
pub struct Float {
Expand Down Expand Up @@ -28,6 +28,16 @@ impl Float {
}
}

impl ValueImpl for Float {
fn value_type(&self) -> ValueType {
ValueType::Float
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryFrom<ast::Float> for Float {
type Error = Vec<crate::Error>;

Expand Down
15 changes: 13 additions & 2 deletions crates/document-tree/src/value/integer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::support::integer::{
try_from_binary, try_from_decimal, try_from_hexadecimal, try_from_octal,
use crate::{
support::integer::{try_from_binary, try_from_decimal, try_from_hexadecimal, try_from_octal},
ValueImpl, ValueType,
};

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -45,6 +46,16 @@ impl Integer {
}
}

impl ValueImpl for Integer {
fn value_type(&self) -> ValueType {
ValueType::Integer
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryFrom<ast::IntegerBin> for Integer {
type Error = Vec<crate::Error>;

Expand Down
12 changes: 11 additions & 1 deletion crates/document-tree/src/value/string.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use toml_version::TomlVersion;

use crate::TryIntoDocumentTree;
use crate::{TryIntoDocumentTree, ValueImpl, ValueType};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StringKind {
Expand Down Expand Up @@ -84,6 +84,16 @@ impl String {
}
}

impl ValueImpl for String {
fn value_type(&self) -> ValueType {
ValueType::String
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryIntoDocumentTree<String> for ast::BasicString {
fn try_into_document_tree(
self,
Expand Down
14 changes: 13 additions & 1 deletion crates/document-tree/src/value/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use indexmap::IndexMap;
use itertools::Itertools;
use toml_version::TomlVersion;

use crate::{support::comment::try_new_comment, Array, Key, TryIntoDocumentTree, Value};
use crate::{
support::comment::try_new_comment, Array, Key, TryIntoDocumentTree, Value, ValueImpl, ValueType,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TableKind {
Expand Down Expand Up @@ -266,6 +268,16 @@ impl From<Table> for IndexMap<Key, Value> {
}
}

impl ValueImpl for Table {
fn value_type(&self) -> ValueType {
ValueType::Table
}

fn range(&self) -> text::Range {
self.range()
}
}

impl TryIntoDocumentTree<Table> for ast::Table {
fn try_into_document_tree(self, toml_version: TomlVersion) -> Result<Table, Vec<crate::Error>> {
let mut table = Table::new_table(&self);
Expand Down
19 changes: 19 additions & 0 deletions crates/document-tree/src/value_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValueType {
Boolean,
Integer,
Float,
String,
OffsetDateTime,
LocalDateTime,
LocalDate,
LocalTime,
Array,
Table,
}

impl std::fmt::Display for ValueType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
6 changes: 2 additions & 4 deletions crates/linter/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use schema_store::ValueType;

#[derive(thiserror::Error, Debug)]
pub enum ErrorKind {
#[error("An empty quoted key is allowed, but it is not recommended")]
Expand All @@ -13,8 +11,8 @@ pub enum ErrorKind {

#[error("Expected a value of type {expected}, but found {actual}")]
TypeMismatch {
expected: ValueType,
actual: ValueType,
expected: schema_store::ValueType,
actual: document_tree::ValueType,
},

#[error("The value must be one of [{}], but found {actual}", .expected.join(", "))]
Expand Down
Loading

0 comments on commit 0837eaa

Please sign in to comment.