Skip to content

Commit

Permalink
t2
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarTawfik committed Nov 24, 2024
1 parent eb950ef commit de1bde2
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,20 @@ use std::rc::Rc;

use semver::Version;

use crate::parser::{ParseOutput, Parser, ParserInitializationError};
use crate::compilation::file::File;
use crate::parser::{Parser, ParserInitializationError};

pub enum FileEntry {
Resolved {
id: String,

parse_output: ParseOutput,
},
Unresolved,
}

#[derive(thiserror::Error, Debug)]
pub enum CompilationInitializationError {
#[error("Unsupported language version '{0}'.")]
UnsupportedLanguageVersion(Version),
}

pub struct Compilation {
pub struct CompilationCore {
parser: Parser,
files: BTreeMap<String, FileEntry>,

files: BTreeMap<String, Rc<File>>,
}

impl Compilation {
impl CompilationCore {
pub fn create(version: Version) -> std::result::Result<Self, CompilationInitializationError> {
let parser = match Parser::create(version) {
Ok(parser) => parser,
Err(ParserInitializationError::UnsupportedLanguageVersion(version)) => {
return Err(CompilationInitializationError::UnsupportedLanguageVersion(
version,
));
}
Err(error) => return Err(error.into()),
};

Ok(Self {
Expand All @@ -47,11 +30,26 @@ impl Compilation {
}

pub fn add_file(&mut self, id: String, contents: &str) {


let parse_output = self.parser.parse(Parser::ROOT_KIND, contents);

self.files
.insert(id.clone(), File(Rc::new(FileData { id, parse_output })));
let file = File::new(id.clone(), parse_output);

self.files.insert(id, Rc::new(file));
}
}

#[derive(thiserror::Error, Debug)]
pub enum CompilationInitializationError {
#[error("Unsupported language version '{0}'.")]
UnsupportedLanguageVersion(Version),
}

impl From<ParserInitializationError> for CompilationInitializationError {
fn from(value: ParserInitializationError) -> Self {
match value {
ParserInitializationError::UnsupportedLanguageVersion(version) => {
Self::UnsupportedLanguageVersion(version)
}
}
}
}
21 changes: 21 additions & 0 deletions crates/codegen/runtime/cargo/crate/src/runtime/compilation/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::parser::ParseOutput;

pub struct File {
id: String,

parse_output: ParseOutput,
}

impl File {
pub fn new(id: String, parse_output: ParseOutput) -> Self {
Self { id, parse_output }
}

pub fn id(&self) -> &String {
&self.id
}

pub fn parse_output(&self) -> &ParseOutput {
&self.parse_output
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod internal;
mod core;
mod file;

/// Only intended to be used internally by the TypeScript comilation API.
pub use internal::Compilation;
pub use core::{CompilationCore, CompilationInitializationError};

pub use file::File;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit de1bde2

Please sign in to comment.