refactor(tvix/cli): add helper method for strong string coercion
This is repetitive and error prone (e.g. switching around to_string/as_str has drastic consequences) due to the ToString overloads. Change-Id: I9b16a2e0e05e4c21e83f43e9f603746eb42e53f7 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7947 Reviewed-by: flokli <flokli@flokli.de> Tested-by: BuildkiteCI Autosubmit: tazjin <tazjin@tvl.su>
This commit is contained in:
		
							parent
							
								
									91146b204f
								
							
						
					
					
						commit
						124af9e5d5
					
				
					 1 changed files with 28 additions and 28 deletions
				
			
		| 
						 | 
					@ -173,26 +173,22 @@ fn populate_output_configuration(
 | 
				
			||||||
        (Some(hash), Some(algo), hash_mode) => match drv.outputs.get_mut("out") {
 | 
					        (Some(hash), Some(algo), hash_mode) => match drv.outputs.get_mut("out") {
 | 
				
			||||||
            None => return Err(Error::ConflictingOutputTypes.into()),
 | 
					            None => return Err(Error::ConflictingOutputTypes.into()),
 | 
				
			||||||
            Some(out) => {
 | 
					            Some(out) => {
 | 
				
			||||||
                let algo = algo
 | 
					                let algo = strong_coerce_to_string(
 | 
				
			||||||
                    .force(vm)?
 | 
					                    vm,
 | 
				
			||||||
                    .coerce_to_string(CoercionKind::Strong, vm)?
 | 
					                    &algo,
 | 
				
			||||||
                    .as_str()
 | 
					                    "evaluating outputHashAlgo of a derivation",
 | 
				
			||||||
                    .to_string();
 | 
					                )?;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let digest_str = hash
 | 
					                let digest_str =
 | 
				
			||||||
                    .force(vm)?
 | 
					                    strong_coerce_to_string(vm, &hash, "evaluating outputHash of a derivation")?;
 | 
				
			||||||
                    .coerce_to_string(CoercionKind::Strong, vm)?
 | 
					 | 
				
			||||||
                    .as_str()
 | 
					 | 
				
			||||||
                    .to_string();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                let hash_mode = match hash_mode {
 | 
					                let hash_mode = match hash_mode {
 | 
				
			||||||
                    None => None,
 | 
					                    None => None,
 | 
				
			||||||
                    Some(mode) => Some(
 | 
					                    Some(mode) => Some(strong_coerce_to_string(
 | 
				
			||||||
                        mode.force(vm)?
 | 
					                        vm,
 | 
				
			||||||
                            .coerce_to_string(CoercionKind::Strong, vm)?
 | 
					                        &mode,
 | 
				
			||||||
                            .as_str()
 | 
					                        "evaluating outputHashMode of a derivation",
 | 
				
			||||||
                            .to_string(),
 | 
					                    )?),
 | 
				
			||||||
                    ),
 | 
					 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // construct out.hash
 | 
					                // construct out.hash
 | 
				
			||||||
| 
						 | 
					@ -227,13 +223,11 @@ fn handle_derivation_parameters(
 | 
				
			||||||
        "args" => {
 | 
					        "args" => {
 | 
				
			||||||
            let args = value.to_list()?;
 | 
					            let args = value.to_list()?;
 | 
				
			||||||
            for arg in args {
 | 
					            for arg in args {
 | 
				
			||||||
                drv.arguments.push(
 | 
					                drv.arguments.push(strong_coerce_to_string(
 | 
				
			||||||
                    arg.force(vm)?
 | 
					                    vm,
 | 
				
			||||||
                        .coerce_to_string(CoercionKind::Strong, vm)
 | 
					                    &arg,
 | 
				
			||||||
                        .context("handling command-line builder arguments")?
 | 
					                    "handling command-line builder arguments",
 | 
				
			||||||
                        .as_str()
 | 
					                )?);
 | 
				
			||||||
                        .to_string(),
 | 
					 | 
				
			||||||
                );
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // The arguments do not appear in the environment.
 | 
					            // The arguments do not appear in the environment.
 | 
				
			||||||
| 
						 | 
					@ -264,6 +258,16 @@ fn handle_derivation_parameters(
 | 
				
			||||||
    Ok(true)
 | 
					    Ok(true)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn strong_coerce_to_string(vm: &mut VM, val: &Value, ctx: &str) -> Result<String, ErrorKind> {
 | 
				
			||||||
 | 
					    Ok(val
 | 
				
			||||||
 | 
					        .force(vm)
 | 
				
			||||||
 | 
					        .context(ctx)?
 | 
				
			||||||
 | 
					        .coerce_to_string(CoercionKind::Strong, vm)
 | 
				
			||||||
 | 
					        .context(ctx)?
 | 
				
			||||||
 | 
					        .as_str()
 | 
				
			||||||
 | 
					        .to_string())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[builtins(state = "Rc<RefCell<KnownPaths>>")]
 | 
					#[builtins(state = "Rc<RefCell<KnownPaths>>")]
 | 
				
			||||||
mod derivation_builtins {
 | 
					mod derivation_builtins {
 | 
				
			||||||
    use super::*;
 | 
					    use super::*;
 | 
				
			||||||
| 
						 | 
					@ -316,11 +320,7 @@ mod derivation_builtins {
 | 
				
			||||||
                continue;
 | 
					                continue;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            let val_str = value
 | 
					            let val_str = strong_coerce_to_string(vm, &value, "evaluating derivation attributes")?;
 | 
				
			||||||
                .force(vm)?
 | 
					 | 
				
			||||||
                .coerce_to_string(CoercionKind::Strong, vm)?
 | 
					 | 
				
			||||||
                .as_str()
 | 
					 | 
				
			||||||
                .to_string();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // handle_derivation_parameters tells us whether the
 | 
					            // handle_derivation_parameters tells us whether the
 | 
				
			||||||
            // argument should be added to the environment; continue
 | 
					            // argument should be added to the environment; continue
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue