Skip to main content

oak_csv/
lib.rs

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