From 07a3b6c0bc7f3d7018828a7a0c203555cc1a51bf Mon Sep 17 00:00:00 2001 From: _cry64 Date: Sun, 29 Mar 2026 18:29:17 +1000 Subject: [PATCH] remove old value.rs + valuetype.rs --- nixide/src/expr/value.rs | 237 ----------------------------------- nixide/src/expr/valuetype.rs | 68 ---------- 2 files changed, 305 deletions(-) delete mode 100644 nixide/src/expr/value.rs delete mode 100644 nixide/src/expr/valuetype.rs diff --git a/nixide/src/expr/value.rs b/nixide/src/expr/value.rs deleted file mode 100644 index 6d6bff8..0000000 --- a/nixide/src/expr/value.rs +++ /dev/null @@ -1,237 +0,0 @@ -use std::fmt::{Debug, Display, Formatter, Result as FmtResult}; -use std::ptr::NonNull; - -use super::{EvalState, ValueType}; -use crate::errors::{ErrorContext, NixideResult}; -use crate::sys; -use crate::util::wrappers::AsInnerPtr; -use crate::util::{panic_issue_call_failed, wrap}; - -pub use crate::expr::values::{ - NixAttrs, NixBool, NixExternal, NixFloat, NixFunction, NixInt, NixList, NixNull, NixPath, - NixString, NixThunk, NixValue, -}; - -/// A Nix value -/// -/// This represents any value in the Nix language, including primitives, -/// collections, and functions. -/// -/// ```cpp -/// pub const ValueType_NIX_TYPE_THUNK: ValueType = 0; -/// pub const ValueType_NIX_TYPE_INT: ValueType = 1; -/// pub const ValueType_NIX_TYPE_FLOAT: ValueType = 2; -/// pub const ValueType_NIX_TYPE_BOOL: ValueType = 3; -/// pub const ValueType_NIX_TYPE_STRING: ValueType = 4; -/// pub const ValueType_NIX_TYPE_PATH: ValueType = 5; -/// pub const ValueType_NIX_TYPE_NULL: ValueType = 6; -/// pub const ValueType_NIX_TYPE_ATTRS: ValueType = 7; -/// pub const ValueType_NIX_TYPE_LIST: ValueType = 8; -/// pub const ValueType_NIX_TYPE_FUNCTION: ValueType = 9; -/// pub const ValueType_NIX_TYPE_EXTERNAL: ValueType = 10; -/// ``` -pub enum Value { - /// Unevaluated expression - /// - /// Thunks often contain an expression and closure, but may contain other - /// representations too. - /// - /// Their state is mutable, unlike that of the other types. - Thunk(NixThunk), - Int(NixInt), - Float(NixFloat), - Bool(NixBool), - String(NixString), - Path(NixPath), - Null(NixNull), - Attrs(NixAttrs), - List(NixList), - Function(NixFunction), - External(NixExternal), -} - -impl Value { - /// Forces the evaluation of a Nix value. - /// - /// The Nix interpreter is lazy, and not-yet-evaluated values can be - /// of type NIX_TYPE_THUNK instead of their actual value. - /// - /// This function mutates such a `nix_value`, so that, if successful, it has its final type. - /// - /// @param[out] context Optional, stores error information - /// @param[in] state The state of the evaluation. - /// @param[in,out] value The Nix value to force. - /// @post value is not of type NIX_TYPE_THUNK - /// @return NIX_OK if the force operation was successful, an error code - /// otherwise. - pub fn force(&mut self) -> NixideResult<()> { - // XXX: TODO: move force and force_deep to the EvalState - wrap::nix_fn!(|ctx: &ErrorContext| unsafe { - sys::nix_value_force(ctx.as_ptr(), self.state.as_ptr(), self.as_ptr()); - }) - } - - /// Force deep evaluation of this value. - /// - /// This forces evaluation of the value and all its nested components. - /// - /// # Errors - /// - /// Returns an error if evaluation fails. - pub fn force_deep(&mut self) -> NixideResult<()> { - // XXX: TODO: move force and force_deep to the EvalState - wrap::nix_fn!(|ctx: &ErrorContext| unsafe { - sys::nix_value_force_deep(ctx.as_ptr(), self.state.as_ptr(), self.as_ptr()); - }) - } - - /// Get the type of this value. - #[must_use] - pub fn value_type(&self) -> ValueType { - // NOTE: an error here only occurs if `nix_get_type` catches an error, - // NOTE: which in turn only happens if the `sys::nix_value*` is a null pointer - // NOTE: or points to an uninitialised `nix_value` struct. - ValueType::from({ - wrap::nix_fn!(|ctx: &ErrorContext| unsafe { - sys::nix_get_type(ctx.as_ptr(), self.as_ptr()) - }) - .unwrap_or_else(|err| panic_issue_call_failed!("{}", err)) - }) - } - - // XXX: TODO: rewrite `expr/value.rs` to make this redundant - // fn expect_type(&self, expected: ValueType) -> NixideResult<()> { - // let got = self.value_type(); - // if got != expected { - // return Err(new_nixide_error!( - // InvalidType, - // expected.to_string(), - // got.to_string() - // )); - // } - // Ok(()) - // } - - /// Format this value as Nix syntax. - /// - /// This provides a string representation that matches Nix's own syntax, - /// making it useful for debugging and displaying values to users. - /// - /// # Errors - /// - /// Returns an error if the value cannot be converted to a string - /// representation. - pub fn to_nix_string(&self) -> NixideResult { - match self.value_type() { - | ValueType::Int => Ok(self.as_int()?.to_string()), - | ValueType::Float => Ok(self.as_float()?.to_string()), - | ValueType::Bool => Ok(if self.as_bool()? { - "true".to_string() - } else { - "false".to_string() - }), - | ValueType::String => Ok(format!("\"{}\"", self.as_string()?.replace('"', "\\\""))), - | ValueType::Null => Ok("null".to_string()), - | ValueType::Attrs => Ok("{ }".to_string()), - | ValueType::List => Ok("[ ]".to_string()), - | ValueType::Function => Ok("".to_string()), - | ValueType::Path => Ok("".to_string()), - | ValueType::Thunk => Ok("".to_string()), - | ValueType::External => Ok("".to_string()), - } - } -} - -impl Drop for Value { - fn drop(&mut self) { - let ctx = ErrorContext::new(); - unsafe { - sys::nix_value_decref(ctx.as_ptr(), self.as_ptr()); - } - } -} - -impl Display for Value { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - match self.value_type() { - | ValueType::Int => { - if let Ok(val) = self.as_int() { - write!(f, "{val}") - } else { - write!(f, "") - } - }, - | ValueType::Float => { - if let Ok(val) = self.as_float() { - write!(f, "{val}") - } else { - write!(f, "") - } - }, - | ValueType::Bool => { - if let Ok(val) = self.as_bool() { - write!(f, "{val}") - } else { - write!(f, "") - } - }, - | ValueType::String => { - if let Ok(val) = self.as_string() { - write!(f, "{val}") - } else { - write!(f, "") - } - }, - | ValueType::Null => write!(f, "null"), - | ValueType::Attrs => write!(f, "{{ }}"), - | ValueType::List => write!(f, "[ ]"), - | ValueType::Function => write!(f, ""), - | ValueType::Path => write!(f, ""), - | ValueType::Thunk => write!(f, ""), - | ValueType::External => write!(f, ""), - } - } -} - -impl Debug for Value { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - let value_type = self.value_type(); - match value_type { - | ValueType::Int => { - if let Ok(val) = self.as_int() { - write!(f, "Value::Int({val})") - } else { - write!(f, "Value::Int()") - } - }, - | ValueType::Float => { - if let Ok(val) = self.as_float() { - write!(f, "Value::Float({val})") - } else { - write!(f, "Value::Float()") - } - }, - | ValueType::Bool => { - if let Ok(val) = self.as_bool() { - write!(f, "Value::Bool({val})") - } else { - write!(f, "Value::Bool()") - } - }, - | ValueType::String => { - if let Ok(val) = self.as_string() { - write!(f, "Value::String({val:?})") - } else { - write!(f, "Value::String()") - } - }, - | ValueType::Null => write!(f, "Value::Null"), - | ValueType::Attrs => write!(f, "Value::Attrs({{ }})"), - | ValueType::List => write!(f, "Value::List([ ])"), - | ValueType::Function => write!(f, "Value::Function()"), - | ValueType::Path => write!(f, "Value::Path()"), - | ValueType::Thunk => write!(f, "Value::Thunk()"), - | ValueType::External => write!(f, "Value::External()"), - } - } -} diff --git a/nixide/src/expr/valuetype.rs b/nixide/src/expr/valuetype.rs deleted file mode 100644 index ad7b194..0000000 --- a/nixide/src/expr/valuetype.rs +++ /dev/null @@ -1,68 +0,0 @@ -use std::fmt::{Display, Formatter, Result as FmtResult}; - -use crate::sys; - -/// Nix value types. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum ValueType { - /// Thunk (unevaluated expression). - Thunk, - /// Integer value. - Int, - /// Float value. - Float, - /// Boolean value. - Bool, - /// String value. - String, - /// Path value. - Path, - /// Null value. - Null, - /// Attribute set. - Attrs, - /// List. - List, - /// Function. - Function, - /// External value. - External, -} - -impl From for ValueType { - fn from(value_type: sys::ValueType) -> Self { - match value_type { - sys::ValueType_NIX_TYPE_THUNK => ValueType::Thunk, - sys::ValueType_NIX_TYPE_INT => ValueType::Int, - sys::ValueType_NIX_TYPE_FLOAT => ValueType::Float, - sys::ValueType_NIX_TYPE_BOOL => ValueType::Bool, - sys::ValueType_NIX_TYPE_STRING => ValueType::String, - sys::ValueType_NIX_TYPE_PATH => ValueType::Path, - sys::ValueType_NIX_TYPE_NULL => ValueType::Null, - sys::ValueType_NIX_TYPE_ATTRS => ValueType::Attrs, - sys::ValueType_NIX_TYPE_LIST => ValueType::List, - sys::ValueType_NIX_TYPE_FUNCTION => ValueType::Function, - sys::ValueType_NIX_TYPE_EXTERNAL => ValueType::External, - _ => unreachable!("call to `nixide::ValueType::from_c` failed: please open an issue on https://github.com/cry128/nixide"), - } - } -} - -impl Display for ValueType { - fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { - let name = match self { - ValueType::Thunk => "thunk", - ValueType::Int => "int", - ValueType::Float => "float", - ValueType::Bool => "bool", - ValueType::String => "string", - ValueType::Path => "path", - ValueType::Null => "null", - ValueType::Attrs => "attrs", - ValueType::List => "list", - ValueType::Function => "function", - ValueType::External => "external", - }; - write!(f, "{name}") - } -}