mirror of
https://github.com/Noratrieb/terustform.git
synced 2026-01-14 16:35:11 +01:00
Derive macro getting works
This commit is contained in:
parent
63bd32c3cd
commit
7d28815065
9 changed files with 245 additions and 41 deletions
|
|
@ -4,6 +4,8 @@ version = "0.1.0"
|
|||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
terustform-macros = { path = "../terustform-macros" }
|
||||
|
||||
base64 = "0.22.0"
|
||||
eyre = "0.6.12"
|
||||
prost = "0.12.4"
|
||||
|
|
|
|||
|
|
@ -2,12 +2,16 @@
|
|||
|
||||
pub mod datasource;
|
||||
pub mod provider;
|
||||
pub mod value;
|
||||
|
||||
use crate::values::{Value, ValueKind};
|
||||
|
||||
use self::datasource::DataSource;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Diagnostics {
|
||||
pub(crate) errors: Vec<String>,
|
||||
pub(crate) attr: Option<AttrPath>,
|
||||
// note: lol this cannot contain warnings that would be fucked oops
|
||||
}
|
||||
|
||||
pub type DResult<T> = Result<T, Diagnostics>;
|
||||
|
|
@ -16,8 +20,18 @@ impl Diagnostics {
|
|||
pub fn error_string(msg: String) -> Self {
|
||||
Self {
|
||||
errors: vec![msg],
|
||||
attr: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_path(mut self, path: AttrPath) -> Self {
|
||||
self.attr = Some(path);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn has_errors(&self) -> bool {
|
||||
!self.errors.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: std::error::Error + std::fmt::Debug> From<E> for Diagnostics {
|
||||
|
|
@ -25,3 +39,75 @@ impl<E: std::error::Error + std::fmt::Debug> From<E> for Diagnostics {
|
|||
Self::error_string(format!("{:?}", value))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: this could probably be a clever 0-alloc &-based linked list!
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct AttrPath(Vec<AttrPathSegment>);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum AttrPathSegment {
|
||||
AttributeName(String),
|
||||
ElementKeyString(String),
|
||||
ElementKeyInt(i64),
|
||||
}
|
||||
|
||||
impl AttrPath {
|
||||
pub fn root() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn append_attribute_name(&self, name: String) -> Self {
|
||||
let mut p = self.clone();
|
||||
p.0.push(AttrPathSegment::AttributeName(name));
|
||||
p
|
||||
}
|
||||
}
|
||||
|
||||
pub type StringValue = BaseValue<String>;
|
||||
pub type I64Value = BaseValue<i64>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum BaseValue<T> {
|
||||
Unknown,
|
||||
Null,
|
||||
Known(T),
|
||||
}
|
||||
|
||||
impl<T> BaseValue<T> {
|
||||
fn map<U>(self, f: impl FnOnce(T) -> U) -> BaseValue<U> {
|
||||
self.try_map(|v| Ok(f(v))).unwrap()
|
||||
}
|
||||
|
||||
fn try_map<U>(self, f: impl FnOnce(T) -> DResult<U>) -> DResult<BaseValue<U>> {
|
||||
Ok(match self {
|
||||
Self::Unknown => BaseValue::Unknown,
|
||||
Self::Null => BaseValue::Null,
|
||||
Self::Known(v) => BaseValue::Known(f(v)?),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ValueModel: Sized {
|
||||
fn from_value(v: Value, path: &AttrPath) -> DResult<Self>;
|
||||
|
||||
fn to_value(self) -> Value {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl ValueModel for StringValue {
|
||||
fn from_value(v: Value, path: &AttrPath) -> DResult<Self> {
|
||||
v.try_map(|v| match v {
|
||||
ValueKind::String(s) => Ok(s),
|
||||
_ => Err(Diagnostics::error_string(format!(
|
||||
"expected string, found {}",
|
||||
v.diagnostic_type_str()
|
||||
))
|
||||
.with_path(path.clone())),
|
||||
})
|
||||
}
|
||||
|
||||
fn to_value(self) -> Value {
|
||||
self.map(ValueKind::String)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +0,0 @@
|
|||
pub struct StringValue;
|
||||
|
|
@ -3,6 +3,8 @@ pub mod framework;
|
|||
mod server;
|
||||
pub mod values;
|
||||
|
||||
pub use terustform_macros::DataSourceModel;
|
||||
|
||||
use std::{env, path::PathBuf};
|
||||
|
||||
use base64::Engine;
|
||||
|
|
@ -48,16 +50,13 @@ pub async fn serve(provider: &dyn Provider) -> eyre::Result<()> {
|
|||
let server = tonic::transport::Server::builder()
|
||||
.tls_config(tls)
|
||||
.wrap_err("invalid TLS config")?
|
||||
.add_service(server::ProviderServer::new(
|
||||
server::ProviderHandler::new(shutdown.clone(), provider),
|
||||
))
|
||||
.add_service(
|
||||
server::GrpcControllerServer::new(
|
||||
server::Controller {
|
||||
shutdown: shutdown.clone(),
|
||||
},
|
||||
),
|
||||
)
|
||||
.add_service(server::ProviderServer::new(server::ProviderHandler::new(
|
||||
shutdown.clone(),
|
||||
provider,
|
||||
)))
|
||||
.add_service(server::GrpcControllerServer::new(server::Controller {
|
||||
shutdown: shutdown.clone(),
|
||||
}))
|
||||
.serve_with_incoming(uds_stream);
|
||||
|
||||
tokio::select! {
|
||||
|
|
@ -101,3 +100,13 @@ async fn init_handshake(server_cert: &rcgen::Certificate) -> Result<(tempfile::T
|
|||
|
||||
Ok((tmpdir, socket))
|
||||
}
|
||||
|
||||
/// Private, only for use for with the derive macro.
|
||||
#[doc(hidden)]
|
||||
pub mod __derive_private {
|
||||
pub use crate::framework::{
|
||||
AttrPath, AttrPathSegment, BaseValue, DResult, Diagnostics, ValueModel,
|
||||
};
|
||||
pub use crate::values::{Value, ValueKind};
|
||||
pub use {Clone, Result::Err, Option::Some, ToOwned};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ use std::{
|
|||
io::{self, Read},
|
||||
};
|
||||
|
||||
use crate::framework::{DResult, Diagnostics};
|
||||
use crate::framework::{BaseValue, DResult, Diagnostics};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Type {
|
||||
|
|
@ -79,12 +79,7 @@ impl Type {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Value {
|
||||
Known(ValueKind),
|
||||
Unknown,
|
||||
Null,
|
||||
}
|
||||
pub type Value = BaseValue<ValueKind>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ValueKind {
|
||||
|
|
@ -98,6 +93,21 @@ pub enum ValueKind {
|
|||
Object(BTreeMap<String, Value>),
|
||||
}
|
||||
|
||||
impl ValueKind {
|
||||
pub fn diagnostic_type_str(&self) -> &'static str {
|
||||
match self {
|
||||
ValueKind::String(_) => "string",
|
||||
ValueKind::Number(_) => "number",
|
||||
ValueKind::Bool(_) => "bool",
|
||||
ValueKind::List(_) => "list",
|
||||
ValueKind::Set(_) => "set",
|
||||
ValueKind::Map(_) => "map",
|
||||
ValueKind::Tuple(_) => "tuple",
|
||||
ValueKind::Object(_) => "object",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// marshal msg pack
|
||||
// tftypes/value.go:MarshalMsgPack
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue