Skip to main content

oak_xml/
lib.rs

1#![doc = include_str!("readme.md")]
2#![feature(new_range_api)]
3#![warn(missing_docs)]
4#![doc(html_logo_url = "https://raw.githubusercontent.com/ygg-lang/oaks/refs/heads/dev/documents/logo.svg")]
5#![doc(html_favicon_url = "https://raw.githubusercontent.com/ygg-lang/oaks/refs/heads/dev/documents/logo.svg")]
6//! Xml support for the Oak language framework.
7
8/// AST module.
9pub mod ast;
10/// Builder module.
11pub mod builder;
12
13// pub mod formatter;
14// pub mod highlighter;
15/// Type definitions module.
16/// Language configuration module.
17pub mod language;
18/// Lexer module.
19pub mod lexer;
20/// LSP module.
21#[cfg(any(feature = "lsp", feature = "oak-highlight", feature = "oak-pretty-print"))]
22pub mod lsp;
23
24// pub mod mcp;
25/// Parser module.
26pub mod parser;
27/// Serde serialization module.
28#[cfg(feature = "serde")]
29pub mod serde;
30
31pub use crate::{
32    ast::{XmlNode, XmlValue},
33    language::XmlLanguage,
34    lexer::{XmlLexer, token_type::XmlTokenType},
35    parser::XmlParser,
36};
37
38// #[cfg(feature = "oak-highlight")]
39// pub use crate::lsp::highlighter::XmlHighlighter;
40
41// /// LSP implementation.
42#[cfg(feature = "lsp")]
43pub use crate::lsp::XmlLanguageService;
44// #[cfg(feature = "lsp")]
45// pub use crate::lsp::formatter::XmlFormatter;
46
47#[cfg(feature = "serde")]
48pub use crate::serde::{from_value, to_value};
49
50#[cfg(feature = "serde")]
51pub fn to_string<T: ::serde::Serialize>(value: &T) -> Result<String, String> {
52    let xml_value = to_value(value).map_err(String::from)?;
53    Ok(xml_value.to_string())
54}
55
56#[cfg(feature = "serde")]
57pub fn from_str<T: ::serde::de::DeserializeOwned>(s: &str) -> Result<T, String> {
58    let xml_value = parse(s)?;
59    from_value(xml_value).map_err(String::from)
60}
61
62pub fn parse(xml: &str) -> Result<XmlValue, String> {
63    use crate::builder::XmlBuilder;
64    use oak_core::{Builder, parser::session::ParseSession, source::SourceText};
65    let builder = XmlBuilder::new();
66    let source = SourceText::new(xml.to_string());
67    let mut cache = ParseSession::default();
68    let result = builder.build(&source, &[], &mut cache);
69    result.result.map(|root| root.value).map_err(|e| format!("Parse failed: {:?}, diagnostics: {:?}", e, result.diagnostics))
70}
71
72pub use parser::element_type::XmlElementType;