Skip to content

Commit 2fea8fa

Browse files
authored
Merge pull request #42 from pushkarm029/feat-CompilerService
feat: integrate CompilerService for Rust code compilation and testing
2 parents a3d2eee + a187dd7 commit 2fea8fa

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

apps/frontend/src/app/app.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ApplicationConfig, provideBrowserGlobalErrorListeners, provideZonelessChangeDetection, NgZone } from '@angular/core';
22
import { provideRouter } from '@angular/router';
3+
import { provideHttpClient, withFetch } from '@angular/common/http';
34
import { MONACO_PATH, MonacoEditorLoaderService } from '@materia-ui/ngx-monaco-editor';
45

56
import { routes } from './app.routes';
@@ -9,7 +10,8 @@ export const appConfig: ApplicationConfig = {
910
providers: [
1011
provideBrowserGlobalErrorListeners(),
1112
provideZonelessChangeDetection(),
12-
provideRouter(routes),
13+
provideRouter(routes),
14+
provideHttpClient(withFetch()),
1315
provideClientHydration(withEventReplay()),
1416
{
1517
provide: MONACO_PATH,

apps/frontend/src/app/components/editor/editor.component.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CommonModule, isPlatformBrowser } from '@angular/common';
33
import { FormsModule } from '@angular/forms';
44
import { MonacoEditorModule } from '@materia-ui/ngx-monaco-editor';
55
import { PLATFORM_ID, inject } from '@angular/core';
6+
import { CompilerService } from '../../services/compiler';
67

78

89
const DEFAULT_RUST_CODE = `// Welcome to Soroban Smart Contract Editor
@@ -33,6 +34,7 @@ export class EditorComponent implements OnDestroy {
3334
private timeoutIds = new Set<number>();
3435
isLoading = false;
3536
isBrowser = isPlatformBrowser(inject(PLATFORM_ID));
37+
private compilerService = inject(CompilerService);
3638

3739
// Validation and output properties
3840
errorMessage: string = '';
@@ -114,6 +116,21 @@ export class EditorComponent implements OnDestroy {
114116
this.outputType = 'info';
115117
console.log('Compiling Rust smart contract code:', this.code);
116118

119+
this.compilerService.compile(this.code).subscribe({
120+
next: (response) => {
121+
this.isLoading = false;
122+
this.outputMessage = 'Compilation completed successfully!';
123+
this.outputType = 'success';
124+
console.log('Compilation response:', response);
125+
},
126+
error: (error) => {
127+
this.isLoading = false;
128+
this.errorMessage = 'Compilation failed: ' + (error.message || error);
129+
this.outputType = 'error';
130+
console.error('Compilation error:', error);
131+
}
132+
});
133+
117134
// TODO: Implement API call to backend compiler
118135
const timeoutId = setTimeout(() => {
119136
this.isLoading = false;
@@ -140,6 +157,21 @@ export class EditorComponent implements OnDestroy {
140157
this.outputType = 'info';
141158
console.log('Testing Rust smart contract code:', this.code);
142159

160+
this.compilerService.test(this.code).subscribe({
161+
next: (response) => {
162+
this.isLoading = false;
163+
this.outputMessage = 'All tests passed successfully!';
164+
this.outputType = 'success';
165+
console.log('Test response:', response);
166+
},
167+
error: (error) => {
168+
this.isLoading = false;
169+
this.errorMessage = 'Tests failed: ' + (error.message || error);
170+
this.outputType = 'error';
171+
console.error('Test error:', error);
172+
}
173+
});
174+
143175
// TODO: Implement API call to backend test runner
144176
const timeoutId = setTimeout(() => {
145177
this.isLoading = false;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { Compiler } from './compiler';
4+
5+
describe('Compiler', () => {
6+
let service: Compiler;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(Compiler);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Injectable, inject } from '@angular/core';
2+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
3+
import { Observable, throwError } from 'rxjs';
4+
import { catchError, map } from 'rxjs/operators';
5+
6+
// Define interfaces for type safety
7+
export interface CompileRequest {
8+
code: string;
9+
}
10+
11+
export interface CompileResponse {
12+
output: string;
13+
success?: boolean;
14+
error?: string;
15+
}
16+
17+
export interface TestResponse {
18+
output: string;
19+
success?: boolean;
20+
error?: string;
21+
}
22+
23+
@Injectable({
24+
providedIn: 'root'
25+
})
26+
export class CompilerService {
27+
private readonly API_BASE_URL = 'http://localhost:3000/api';
28+
private http = inject(HttpClient);
29+
30+
/**
31+
* Compile Rust smart contract code
32+
*/
33+
compile(code: string): Observable<CompileResponse> {
34+
const request: CompileRequest = { code };
35+
36+
return this.http.post<CompileResponse>(`${this.API_BASE_URL}/compile`, request)
37+
.pipe(
38+
map(response => ({
39+
...response,
40+
success: true
41+
})),
42+
catchError(this.handleError)
43+
);
44+
}
45+
46+
/**
47+
* Test Rust smart contract code
48+
*/
49+
test(code: string): Observable<TestResponse> {
50+
const request: CompileRequest = { code };
51+
52+
return this.http.post<TestResponse>(`${this.API_BASE_URL}/test`, request)
53+
.pipe(
54+
map(response => ({
55+
...response,
56+
success: true
57+
})),
58+
catchError(this.handleError)
59+
);
60+
}
61+
62+
/**
63+
* Handle HTTP errors
64+
*/
65+
private handleError(error: HttpErrorResponse): Observable<never> {
66+
let errorMessage = 'An unknown error occurred';
67+
68+
if (error.error instanceof ErrorEvent) {
69+
// Client-side error
70+
errorMessage = `Error: ${error.error.message}`;
71+
} else {
72+
// Server-side error
73+
errorMessage = error.error?.message ||
74+
error.error?.output ||
75+
`Server Error: ${error.status} - ${error.statusText}`;
76+
}
77+
78+
console.error('CompilerService Error:', errorMessage);
79+
return throwError(() => ({
80+
output: errorMessage,
81+
success: false,
82+
error: errorMessage
83+
}));
84+
}
85+
}

0 commit comments

Comments
 (0)