Skip to main content

oak_csv/
lib.rs

1#![doc = include_str!("readme.md")]
2#![feature(new_range_api)]
3#![warn(missing_docs)]
4
5/// The AST nodes for CSV.
6pub mod ast;
7/// The builder for CSV.
8pub mod builder;
9/// The language configuration and marker.
10pub mod language;
11/// The lexer for CSV.
12pub mod lexer;
13/// Language service implementation for CSV.
14#[cfg(feature = "lsp")]
15pub mod lsp;
16/// The parser for CSV.
17pub mod parser;
18
19pub use crate::{
20    ast::{CsvField, CsvRecord, CsvRoot},
21    builder::CsvBuilder,
22    language::{CSV_LANG, CsvLanguage},
23    lexer::CsvLexer,
24    parser::CsvParser,
25};
26
27/// A CSV root node.
28pub type CsvRootNode = crate::ast::CsvRoot;
29
30/// Serializes the given value to a CSV string.
31#[cfg(feature = "serde")]
32/// Serializes the given value to a CSV string.
33pub fn to_string<T: ::serde::Serialize>(value: &T) -> Result<String, oak_core::OakError> {
34    oak_dsv::to_string_with_config::<CSV_LANG, T>(value)
35}
36
37/// Deserializes a CSV string into a value of type `T`.
38#[cfg(feature = "serde")]
39/// Deserializes a CSV string into a value of type `T`.
40pub fn from_str<T: ::serde::de::DeserializeOwned>(s: &str) -> Result<T, oak_core::OakError> {
41    oak_dsv::from_str_with_config::<CSV_LANG, T>(s)
42}
43
44/// Returns the default CSV configuration.
45pub fn language() -> oak_dsv::DsvLanguage {
46    CSV_LANG
47}
48
49/// Parses a CSV string into a `CsvRoot` AST.
50pub fn parse(csv: &str) -> Result<crate::ast::CsvRoot, oak_core::OakError> {
51    use oak_core::{Builder, parser::session::ParseSession, source::SourceText};
52    let builder = CsvBuilder::new();
53    let source = SourceText::new(csv.to_string());
54    let mut cache = ParseSession::default();
55    let result = builder.build(&source, &[], &mut cache);
56    result.result
57}
58
59/// Language service implementation for CSV.
60#[cfg(feature = "lsp")]
61pub use crate::lsp::CsvLanguageService;