Skip to content

Commit

Permalink
Generated models and tests: Should be done now
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathOP committed Dec 29, 2024
1 parent 68f0551 commit ab01f2a
Show file tree
Hide file tree
Showing 61 changed files with 5,830 additions and 38 deletions.
78 changes: 51 additions & 27 deletions packages/hurl_parser_rust/README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
<!--
This README describes the package. If you publish this package to pub.dev,
this README's contents appear on the landing page for your package.
# hurl_parser_rust

For information about how to write a good package README, see the guide for
[writing package pages](https://dart.dev/tools/pub/writing-package-pages).
For general information about developing packages, see the Dart guide for
[creating packages](https://dart.dev/guides/libraries/create-packages)
and the Flutter guide for
[developing packages and plugins](https://flutter.dev/to/develop-packages).
-->

TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
A Dart package that provides Hurl file parsing using a Rust backend for high performance.

## Features

TODO: List what your package can do. Maybe include images, gifs, or videos.

## Getting started

TODO: List prerequisites and provide or point to information on how to
start using the package.
- Parse Hurl files into structured Dart objects
- High-performance Rust-based parsing
- Support for all Hurl features:
- HTTP methods (GET, POST, PUT, DELETE, etc.)
- Headers
- Query parameters
- Form parameters
- Basic authentication
- Cookies
- Options
- Captures
- Assertions
- JSON/XML bodies

## Installation

```yaml
dependencies:
hurl_parser_rust: ^0.1.0
```
## Usage
TODO: Include short and useful examples for package users. Add longer examples
to `/example` folder.

```dart
const like = 'sample';
import 'package:hurl_parser_rust/hurl_parser_rust.dart';

void main() async {
// Initialize the parser
final parser = await HurlParser.getInstance();

// Parse Hurl content
final hurlFile = parser.parse('''
GET http://api.example.com/users
Authorization: Bearer token123
Accept: application/json

HTTP/1.1 200
[Captures]
user_id: jsonpath "$.users[0].id"
[Asserts]
header "Content-Type" == "application/json"
''');
// Access the parsed data
final request = hurlFile.entries.first.request;
print('Method: ${request.method}');
print('URL: ${request.url}');
}
```

## Additional information

TODO: Tell users more about the package: where to find more information, how to
contribute to the package, how to file issues, what response they can expect
from the package authors, and more.
- [Hurl Documentation](https://hurl.dev)

## License

This project is licensed under the MIT License - see the LICENSE file for details.
4 changes: 4 additions & 0 deletions packages/hurl_parser_rust/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

analyzer:
errors:
invalid_annotation_target: ignore
16 changes: 16 additions & 0 deletions packages/hurl_parser_rust/lib/hurl_parser_rust.dart
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
export 'package:hurl_parser_rust/src/hurl_parser.dart';
export 'src/models/basic_auth.dart';
export 'src/models/capture.dart';
export 'src/models/cookie.dart';
export 'src/models/form_param.dart';
export 'src/models/header.dart';
export 'src/models/hurl_assert.dart';
export 'src/models/hurl_entry.dart';
export 'src/models/hurl_file.dart';
export 'src/models/hurl_request.dart';
export 'src/models/hurl_response.dart';
export 'src/models/multipart_form_data.dart';
export 'src/models/predicate.dart';
export 'src/models/query.dart';
export 'src/models/query_param.dart';
export 'src/models/request_body.dart';
export 'src/models/request_option.dart';
42 changes: 32 additions & 10 deletions packages/hurl_parser_rust/lib/src/hurl_parser.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// lib/src/hurl_parser.dart

import 'dart:convert';
import 'package:hurl_parser_rust/src/rust/frb_generated.dart';
import 'package:hurl_parser_rust/src/rust/api/simple.dart';
import 'models/hurl_file.dart';

/// A Dart wrapper for the Rust-based Hurl parser
/// Main class for parsing Hurl files
class HurlParser {
static HurlParser? _instance;
static bool _initialized = false;

// Private constructor
HurlParser._();

/// Gets the singleton instance of HurlParser, initializing if necessary
/// Gets the singleton instance of HurlParser
static Future<HurlParser> getInstance() async {
if (_instance == null) {
_instance = HurlParser._();
Expand All @@ -21,21 +24,40 @@ class HurlParser {
return _instance!;
}

/// Parses a Hurl file content and returns its JSON representation
/// Parses a Hurl file content into a [HurlFile] object
///
/// Args:
/// input: String containing the Hurl file content
/// content: The Hurl file content as a string
///
/// Returns:
/// A JSON string representing the parsed Hurl content
/// A [HurlFile] object representing the parsed content
///
/// Throws:
/// StateError if parser is not initialized
/// String if parsing fails
String parseHurl(String input) {
/// [StateError] if parser is not initialized
/// [FormatException] if parsing fails
HurlFile parse(String content) {
if (!_initialized) {
throw StateError('HurlParser not initialized. Call getInstance() first.');
}
return parseHurlToJson(content: input);

final jsonString = parseHurlToJson(content: content);
return HurlFile.fromJson(jsonDecode(jsonString));
}

/// Parses a Hurl file content and returns the raw JSON string
///
/// This is useful if you want to handle the JSON parsing yourself
///
/// Args:
/// content: The Hurl file content as a string
///
/// Returns:
/// A JSON string representing the parsed content
String parseToJson(String content) {
if (!_initialized) {
throw StateError('HurlParser not initialized. Call getInstance() first.');
}

return parseHurlToJson(content: content);
}
}
16 changes: 16 additions & 0 deletions packages/hurl_parser_rust/lib/src/models/basic_auth.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// basic_auth.dart
import 'package:freezed_annotation/freezed_annotation.dart';

part 'basic_auth.freezed.dart';
part 'basic_auth.g.dart';

@freezed
class BasicAuth with _$BasicAuth {
const factory BasicAuth({
required String username,
required String password,
}) = _BasicAuth;

factory BasicAuth.fromJson(Map<String, dynamic> json) =>
_$BasicAuthFromJson(json);
}
183 changes: 183 additions & 0 deletions packages/hurl_parser_rust/lib/src/models/basic_auth.freezed.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'basic_auth.dart';

// **************************************************************************
// FreezedGenerator
// **************************************************************************

T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

BasicAuth _$BasicAuthFromJson(Map<String, dynamic> json) {
return _BasicAuth.fromJson(json);
}

/// @nodoc
mixin _$BasicAuth {
String get username => throw _privateConstructorUsedError;
String get password => throw _privateConstructorUsedError;

/// Serializes this BasicAuth to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;

/// Create a copy of BasicAuth
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$BasicAuthCopyWith<BasicAuth> get copyWith =>
throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $BasicAuthCopyWith<$Res> {
factory $BasicAuthCopyWith(BasicAuth value, $Res Function(BasicAuth) then) =
_$BasicAuthCopyWithImpl<$Res, BasicAuth>;
@useResult
$Res call({String username, String password});
}

/// @nodoc
class _$BasicAuthCopyWithImpl<$Res, $Val extends BasicAuth>
implements $BasicAuthCopyWith<$Res> {
_$BasicAuthCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

/// Create a copy of BasicAuth
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? username = null,
Object? password = null,
}) {
return _then(_value.copyWith(
username: null == username
? _value.username
: username // ignore: cast_nullable_to_non_nullable
as String,
password: null == password
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as String,
) as $Val);
}
}

/// @nodoc
abstract class _$$BasicAuthImplCopyWith<$Res>
implements $BasicAuthCopyWith<$Res> {
factory _$$BasicAuthImplCopyWith(
_$BasicAuthImpl value, $Res Function(_$BasicAuthImpl) then) =
__$$BasicAuthImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({String username, String password});
}

/// @nodoc
class __$$BasicAuthImplCopyWithImpl<$Res>
extends _$BasicAuthCopyWithImpl<$Res, _$BasicAuthImpl>
implements _$$BasicAuthImplCopyWith<$Res> {
__$$BasicAuthImplCopyWithImpl(
_$BasicAuthImpl _value, $Res Function(_$BasicAuthImpl) _then)
: super(_value, _then);

/// Create a copy of BasicAuth
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? username = null,
Object? password = null,
}) {
return _then(_$BasicAuthImpl(
username: null == username
? _value.username
: username // ignore: cast_nullable_to_non_nullable
as String,
password: null == password
? _value.password
: password // ignore: cast_nullable_to_non_nullable
as String,
));
}
}

/// @nodoc
@JsonSerializable()
class _$BasicAuthImpl implements _BasicAuth {
const _$BasicAuthImpl({required this.username, required this.password});

factory _$BasicAuthImpl.fromJson(Map<String, dynamic> json) =>
_$$BasicAuthImplFromJson(json);

@override
final String username;
@override
final String password;

@override
String toString() {
return 'BasicAuth(username: $username, password: $password)';
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$BasicAuthImpl &&
(identical(other.username, username) ||
other.username == username) &&
(identical(other.password, password) ||
other.password == password));
}

@JsonKey(includeFromJson: false, includeToJson: false)
@override
int get hashCode => Object.hash(runtimeType, username, password);

/// Create a copy of BasicAuth
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$BasicAuthImplCopyWith<_$BasicAuthImpl> get copyWith =>
__$$BasicAuthImplCopyWithImpl<_$BasicAuthImpl>(this, _$identity);

@override
Map<String, dynamic> toJson() {
return _$$BasicAuthImplToJson(
this,
);
}
}

abstract class _BasicAuth implements BasicAuth {
const factory _BasicAuth(
{required final String username,
required final String password}) = _$BasicAuthImpl;

factory _BasicAuth.fromJson(Map<String, dynamic> json) =
_$BasicAuthImpl.fromJson;

@override
String get username;
@override
String get password;

/// Create a copy of BasicAuth
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$BasicAuthImplCopyWith<_$BasicAuthImpl> get copyWith =>
throw _privateConstructorUsedError;
}
Loading

0 comments on commit ab01f2a

Please sign in to comment.