Better subnet

This commit is contained in:
Álvaro González 2026-03-09 11:00:35 +01:00
parent 547a651106
commit 0e46950338
2 changed files with 16 additions and 4 deletions

View file

@ -16,14 +16,27 @@ The network address is calculated by applying the subnet mask to zero out the ho
],
"output": {"name": "network", "unit": "string"},
"d4rtCode": r"""
hostIP = hostIP.toString();
var parts = hostIP.split('/');
if( parts.length != 2 ) {
signal('Invalid input format. Expected format: IP/MASK');
}
var ip = parts[0];
var mask = int.parse(parts[1]);
var octets = ip.split('.').map((e) => int.parse(e)).toList();
var mask = int.tryParse(parts[1]);
if( mask == null || mask < 0 || mask > 32 ) {
signal('Invalid subnet mask. Must be an integer between 0 and 32.');
}
var octets = ip.split('.').map((e) => int.tryParse(e)).toList();
if( octets.length != 4 ) {
signal('Invalid IP address format. Expected 4 octets separated by dots.');
}
var hostBits = 32 - mask;
var shiftAmount = hostBits;
var networkValue = 0;
for (var i = 0; i < 4; i++) {
if (octets[i] == null || octets[i] < 0 || octets[i] > 255) {
signal('Invalid IP address. Each octet must be between 0 and 255.');
}
networkValue = (networkValue << 8) | octets[i];
}
networkValue = (networkValue >> shiftAmount) << shiftAmount;

View file

@ -135,7 +135,6 @@ class _FormulaScreenState extends State<FormulaScreen> {
errorHandler.notify(e, stack);
setState(() {
_errorMessage = e.toString();
_isErrorExpanded = true; // Auto-expand on error
_result = null;
});
}
@ -254,7 +253,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
color: Theme.of(context).colorScheme.errorContainer,
child: ExpansionTile(
title: Text(
'⚠️ There were an error. Show details...',
'⚠️ There was an error. Show details...',
style: Theme.of(context).textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.bold,
color: Theme.of(context).colorScheme.onErrorContainer,