Skip to content

Commit

Permalink
feat: implement ProxmoxClient for generated structs
Browse files Browse the repository at this point in the history
  • Loading branch information
datdenkikniet committed Apr 20, 2024
1 parent 37e6eef commit 18d99ff
Show file tree
Hide file tree
Showing 389 changed files with 5,592 additions and 10 deletions.
49 changes: 41 additions & 8 deletions generator/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,24 @@ impl Generator {
let fn_name_str = method.to_ascii_lowercase();
let fn_name = Ident::new(&fn_name_str, quote!().span());

let (signature, defer_signature) = if let Some(TypeDef::Struct(strt)) = &parameters
{
let params = if let Some(TypeDef::Struct(strt)) = &parameters {
let name = Ident::new(&strt.name(), quote! {}.span());
(quote!(&self, params: #name), quote!(&path, &params))
} else if parameters.is_some() {
panic!("Cannot handle non-struct parameters!");
quote!(#name)
} else if parameters.is_none() {
quote!(())
} else {
panic!("Cannot handle non-struct, non-empty parameters!");
};

let base_signature = quote!(&self, params: #params);

let (signature, defer_signature) = if parameters.is_some() {
(base_signature.clone(), quote!(&path, &params))
} else {
(quote!(&self), quote!(&path, &()))
};

let (returns, call) = if let Some(ret) = info.returns.as_ref() {
let (output_ty, returns, call) = if let Some(ret) = info.returns.as_ref() {
let output = ret.type_def("", &format!("{method}Output"));

if let Some(output) = output {
Expand All @@ -152,15 +159,24 @@ impl Generator {

let call = Self::to_call(def.primitive(), &fn_name, defer_signature);

(quote! { -> Result<#name, T::Error> }, call)
(
Some(name.clone()),
quote! { -> Result<#name, T::Error> },
call,
)
} else {
(
None,
quote!( -> Result<(), T::Error> ),
quote!(self.client.#fn_name(#defer_signature)),
)
}
} else {
(quote!(), quote!(self.client.#fn_name(#defer_signature)))
(
None,
quote!(),
quote!(self.client.#fn_name(#defer_signature)),
)
};

let doc = if let Some(doc) = &info.description {
Expand All @@ -180,11 +196,28 @@ impl Generator {
};

let client = proxmox_api(quote!(client::Client));
let action = proxmox_api(quote!(proxmox_client::ProxmoxClientAction));

let params_ty = params.clone();
let params = parameters.as_ref().map(|_| quote!(params)).into_iter();
let output_ty = output_ty.unwrap_or_else(|| quote!(()));
let method = Ident::new(&method, quote!().span());
let method = proxmox_api(quote!(client::Method::#method));
let method_ty = proxmox_api(quote!(client::Method));

let block = quote! {
impl<T> #client_name<T> where T: #client {
#fn_definition
}

impl<T> #action<#params_ty, #output_ty, T::Error>
for &#client_name<T> where T: #client {
const METHOD: #method_ty = #method;

fn exec(#base_signature) -> Result<#output_ty, T::Error> {
self.#fn_name(#(#params)*)
}
}
};

block
Expand Down
10 changes: 10 additions & 0 deletions proxmox-api/src/generated/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), Vec<GetOutputItems>, T::Error>
for &AccessClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<Vec<GetOutputItems>, T::Error> {
self.get()
}
}
impl GetOutputItems {
pub fn new(subdir: String) -> Self {
Self {
Expand Down
19 changes: 19 additions & 0 deletions proxmox-api/src/generated/access/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), Vec<GetOutputItems>, T::Error>
for &AclClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<Vec<GetOutputItems>, T::Error> {
self.get()
}
}
impl<T> AclClient<T>
where
T: crate::client::Client,
Expand All @@ -44,6 +54,15 @@ where
self.client.put(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PutParams, (), T::Error> for &AclClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Put;
fn exec(&self, params: PutParams) -> Result<(), T::Error> {
self.put(params)
}
}
impl GetOutputItems {
pub fn new(path: String, roleid: String, ty: Type, ugid: String) -> Self {
Self {
Expand Down
19 changes: 19 additions & 0 deletions proxmox-api/src/generated/access/domains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), Vec<GetOutputItems>, T::Error>
for &DomainsClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<Vec<GetOutputItems>, T::Error> {
self.get()
}
}
impl<T> DomainsClient<T>
where
T: crate::client::Client,
Expand All @@ -45,6 +55,15 @@ where
self.client.post(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PostParams, (), T::Error> for &DomainsClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Post;
fn exec(&self, params: PostParams) -> Result<(), T::Error> {
self.post(params)
}
}
impl GetOutputItems {
pub fn new(realm: String, ty: String) -> Self {
Self {
Expand Down
27 changes: 27 additions & 0 deletions proxmox-api/src/generated/access/domains/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ where
self.client.delete(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), (), T::Error> for &RealmClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Delete;
fn exec(&self, params: ()) -> Result<(), T::Error> {
self.delete()
}
}
impl<T> RealmClient<T>
where
T: crate::client::Client,
Expand All @@ -45,6 +54,15 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), (), T::Error> for &RealmClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<(), T::Error> {
self.get()
}
}
impl<T> RealmClient<T>
where
T: crate::client::Client,
Expand All @@ -56,6 +74,15 @@ where
self.client.put(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PutParams, (), T::Error> for &RealmClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Put;
fn exec(&self, params: PutParams) -> Result<(), T::Error> {
self.put(params)
}
}
#[derive(Clone, Debug, :: serde :: Serialize, :: serde :: Deserialize, Default)]
pub struct PutParams {
#[serde(rename = "acr-values")]
Expand Down
9 changes: 9 additions & 0 deletions proxmox-api/src/generated/access/domains/realm/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ where
self.client.post(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PostParams, String, T::Error> for &SyncClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Post;
fn exec(&self, params: PostParams) -> Result<String, T::Error> {
self.post(params)
}
}
#[derive(Clone, Debug, :: serde :: Serialize, :: serde :: Deserialize, Default)]
pub struct PostParams {
#[serde(rename = "dry-run")]
Expand Down
19 changes: 19 additions & 0 deletions proxmox-api/src/generated/access/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), Vec<GetOutputItems>, T::Error>
for &GroupsClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<Vec<GetOutputItems>, T::Error> {
self.get()
}
}
impl<T> GroupsClient<T>
where
T: crate::client::Client,
Expand All @@ -45,6 +55,15 @@ where
self.client.post(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PostParams, (), T::Error> for &GroupsClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Post;
fn exec(&self, params: PostParams) -> Result<(), T::Error> {
self.post(params)
}
}
impl GetOutputItems {
pub fn new(groupid: String) -> Self {
Self {
Expand Down
27 changes: 27 additions & 0 deletions proxmox-api/src/generated/access/groups/groupid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ where
self.client.delete(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), (), T::Error> for &GroupidClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Delete;
fn exec(&self, params: ()) -> Result<(), T::Error> {
self.delete()
}
}
impl<T> GroupidClient<T>
where
T: crate::client::Client,
Expand All @@ -44,6 +53,15 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), GetOutput, T::Error> for &GroupidClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<GetOutput, T::Error> {
self.get()
}
}
impl<T> GroupidClient<T>
where
T: crate::client::Client,
Expand All @@ -55,6 +73,15 @@ where
self.client.put(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PutParams, (), T::Error> for &GroupidClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Put;
fn exec(&self, params: PutParams) -> Result<(), T::Error> {
self.put(params)
}
}
impl GetOutput {
pub fn new(members: Vec<String>) -> Self {
Self {
Expand Down
10 changes: 10 additions & 0 deletions proxmox-api/src/generated/access/openid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ where
self.client.get(&path, &())
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<(), Vec<GetOutputItems>, T::Error>
for &OpenidClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: ()) -> Result<Vec<GetOutputItems>, T::Error> {
self.get()
}
}
impl GetOutputItems {
pub fn new(subdir: String) -> Self {
Self {
Expand Down
10 changes: 10 additions & 0 deletions proxmox-api/src/generated/access/openid/auth_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ where
self.client.post(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PostParams, String, T::Error>
for &AuthUrlClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Post;
fn exec(&self, params: PostParams) -> Result<String, T::Error> {
self.post(params)
}
}
impl PostParams {
pub fn new(realm: String, redirect_url: String) -> Self {
Self {
Expand Down
9 changes: 9 additions & 0 deletions proxmox-api/src/generated/access/openid/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ where
self.client.post(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PostParams, (), T::Error> for &LoginClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Post;
fn exec(&self, params: PostParams) -> Result<(), T::Error> {
self.post(params)
}
}
impl PostParams {
pub fn new(code: String, redirect_url: String, state: String) -> Self {
Self {
Expand Down
9 changes: 9 additions & 0 deletions proxmox-api/src/generated/access/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ where
self.client.put(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<PutParams, (), T::Error> for &PasswordClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Put;
fn exec(&self, params: PutParams) -> Result<(), T::Error> {
self.put(params)
}
}
impl PutParams {
pub fn new(password: String, userid: String) -> Self {
Self {
Expand Down
10 changes: 10 additions & 0 deletions proxmox-api/src/generated/access/permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ where
self.client.get(&path, &params)
}
}
impl<T> crate::proxmox_client::ProxmoxClientAction<GetParams, GetOutput, T::Error>
for &PermissionsClient<T>
where
T: crate::client::Client,
{
const METHOD: crate::client::Method = crate::client::Method::Get;
fn exec(&self, params: GetParams) -> Result<GetOutput, T::Error> {
self.get(params)
}
}
#[derive(Clone, Debug, :: serde :: Serialize, :: serde :: Deserialize, Default)]
pub struct GetOutput {
#[serde(
Expand Down
Loading

0 comments on commit 18d99ff

Please sign in to comment.