Skip to content

Commit

Permalink
Updated ext generator for rust
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyaxod committed Sep 22, 2023
1 parent b1483d9 commit 3917bda
Show file tree
Hide file tree
Showing 6 changed files with 505 additions and 80 deletions.
30 changes: 30 additions & 0 deletions extension/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ package generator

import (
"bytes"
"encoding/hex"

"github.com/loopholelabs/scale/extension"
"github.com/loopholelabs/scale/extension/generator/golang"
"github.com/loopholelabs/scale/extension/generator/rust"
)

type GuestRegistryPackage struct {
Expand Down Expand Up @@ -62,6 +64,12 @@ type Options struct {
}

func GenerateGuestLocal(options *Options) (*GuestLocalPackage, error) {
hash, err := options.Extension.Hash()
if err != nil {
return nil, err
}
hashString := hex.EncodeToString(hash)

golangTypes, err := golang.GenerateTypes(options.Extension, options.GolangPackageName)
if err != nil {
return nil, err
Expand Down Expand Up @@ -89,8 +97,30 @@ func GenerateGuestLocal(options *Options) (*GuestLocalPackage, error) {
NewFile("go.mod", "go.mod", modfile),
}

rustTypes, err := rust.GenerateTypes(options.Extension, options.RustPackageName)
if err != nil {
return nil, err
}

rustGuest, err := rust.GenerateGuest(options.Extension, hashString, options.RustPackageName)
if err != nil {
return nil, err
}

cargofile, err := rust.GenerateCargofile(options.RustPackageName, options.RustPackageVersion)
if err != nil {
return nil, err
}

rustFiles := []File{
NewFile("types.rs", "types.rs", rustTypes),
NewFile("guest.rs", "guest.rs", rustGuest),
NewFile("Cargo.toml", "Cargo.toml", cargofile),
}

return &GuestLocalPackage{
GolangFiles: golangFiles,
RustFiles: rustFiles,
}, nil
}

Expand Down
331 changes: 331 additions & 0 deletions extension/generator/rust/generated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
// Code generated by scale-extension 0.4.1, DO NOT EDIT.
// output: types

#![allow(dead_code)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(unused_mut)]
use std::io::Cursor;
use polyglot_rs::{DecodingError, Encoder, Decoder, Kind};
use num_enum::TryFromPrimitive;
use std::convert::TryFrom;
use std::collections::HashMap;
use regex::Regex;
pub trait Encode {
fn encode<'a>(
a: Option<&Self>,
b: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>>
where
Self: Sized;
}
trait EncodeSelf {
fn encode_self<'a, 'b>(
&'b self,
b: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>>;
}
pub trait Decode {
fn decode(
b: &mut Cursor<&mut Vec<u8>>,
) -> Result<Option<Self>, Box<dyn std::error::Error>>
where
Self: Sized;
}
#[derive(Clone, Debug, PartialEq)]
pub struct HttpConfig {
pub timeout: i32,
}
impl HttpConfig {
pub fn new() -> Self {
Self { timeout: 60 }
}
}
impl Encode for HttpConfig {
fn encode<'a>(
a: Option<&HttpConfig>,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
a.encode_self(e)
}
}
impl EncodeSelf for HttpConfig {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
e.encode_i32(self.timeout)?;
Ok(e)
}
}
impl EncodeSelf for Option<&HttpConfig> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl EncodeSelf for Option<HttpConfig> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl Decode for HttpConfig {
fn decode(
d: &mut Cursor<&mut Vec<u8>>,
) -> Result<Option<HttpConfig>, Box<dyn std::error::Error>> {
if d.decode_none() {
return Ok(None);
}
if let Ok(error) = d.decode_error() {
return Err(error);
}
let mut x = HttpConfig::new();
x.timeout = d.decode_i32()?;
Ok(Some(x))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct HttpResponse {
pub headers: HashMap<String, StringList>,
pub status_code: i32,
pub body: Vec<u8>,
}
impl HttpResponse {
pub fn new() -> Self {
Self {
headers: HashMap::new(),
status_code: 0,
body: Vec::with_capacity(0),
}
}
}
impl Encode for HttpResponse {
fn encode<'a>(
a: Option<&HttpResponse>,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
a.encode_self(e)
}
}
impl EncodeSelf for HttpResponse {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
e.encode_map(self.headers.len(), Kind::String, Kind::Any)?;
for (k, v) in &self.headers {
e.encode_string(&k)?;
v.encode_self(e)?;
}
e.encode_i32(self.status_code)?;
e.encode_bytes(&self.body)?;
Ok(e)
}
}
impl EncodeSelf for Option<&HttpResponse> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl EncodeSelf for Option<HttpResponse> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl Decode for HttpResponse {
fn decode(
d: &mut Cursor<&mut Vec<u8>>,
) -> Result<Option<HttpResponse>, Box<dyn std::error::Error>> {
if d.decode_none() {
return Ok(None);
}
if let Ok(error) = d.decode_error() {
return Err(error);
}
let mut x = HttpResponse::new();
let size_headers = d.decode_map(Kind::String, Kind::Any)?;
for _ in 0..size_headers {
let k = d.decode_string()?;
let v = StringList::decode(d)?.ok_or(DecodingError::InvalidMap)?;
x.headers.insert(k, v);
}
x.status_code = d.decode_i32()?;
x.body = d.decode_bytes()?;
Ok(Some(x))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct StringList {
pub values: Vec<String>,
}
impl StringList {
pub fn new() -> Self {
Self {
values: Vec::with_capacity(0),
}
}
}
impl Encode for StringList {
fn encode<'a>(
a: Option<&StringList>,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
a.encode_self(e)
}
}
impl EncodeSelf for StringList {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
e.encode_array(self.values.len(), Kind::String)?;
for a in &self.values {
e.encode_string(&a)?;
}
Ok(e)
}
}
impl EncodeSelf for Option<&StringList> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl EncodeSelf for Option<StringList> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl Decode for StringList {
fn decode(
d: &mut Cursor<&mut Vec<u8>>,
) -> Result<Option<StringList>, Box<dyn std::error::Error>> {
if d.decode_none() {
return Ok(None);
}
if let Ok(error) = d.decode_error() {
return Err(error);
}
let mut x = StringList::new();
let size_values = d.decode_array(Kind::String)?;
for _ in 0..size_values {
x.values.push(d.decode_string()?);
}
Ok(Some(x))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct ConnectionDetails {
pub url: String,
}
impl ConnectionDetails {
pub fn new() -> Self {
Self {
url: "https://google.com".to_string(),
}
}
}
impl Encode for ConnectionDetails {
fn encode<'a>(
a: Option<&ConnectionDetails>,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
a.encode_self(e)
}
}
impl EncodeSelf for ConnectionDetails {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
e.encode_string(&self.url)?;
Ok(e)
}
}
impl EncodeSelf for Option<&ConnectionDetails> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl EncodeSelf for Option<ConnectionDetails> {
fn encode_self<'a, 'b>(
&'b self,
e: &'a mut Cursor<Vec<u8>>,
) -> Result<&'a mut Cursor<Vec<u8>>, Box<dyn std::error::Error>> {
if let Some(x) = self {
x.encode_self(e)?;
} else {
e.encode_none()?;
}
Ok(e)
}
}
impl Decode for ConnectionDetails {
fn decode(
d: &mut Cursor<&mut Vec<u8>>,
) -> Result<Option<ConnectionDetails>, Box<dyn std::error::Error>> {
if d.decode_none() {
return Ok(None);
}
if let Ok(error) = d.decode_error() {
return Err(error);
}
let mut x = ConnectionDetails::new();
x.url = d.decode_string()?;
Ok(Some(x))
}
}
Loading

0 comments on commit 3917bda

Please sign in to comment.